Skip to content

Commit eec27f3

Browse files
committed
Run database migrations and do a config sync before syn2mas
1 parent 7019ad0 commit eec27f3

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

crates/cli/src/commands/syn2mas.rs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
use std::process::ExitCode;
1+
use std::{collections::HashMap, process::ExitCode};
22

33
use anyhow::Context;
44
use camino::Utf8PathBuf;
55
use clap::Parser;
66
use figment::Figment;
7-
use mas_config::{ConfigurationSection, ConfigurationSectionExt, DatabaseConfig, MatrixConfig};
7+
use mas_config::{
8+
ConfigurationSection, ConfigurationSectionExt, DatabaseConfig, MatrixConfig, SyncConfig,
9+
};
10+
use mas_storage::SystemClock;
11+
use mas_storage_pg::MIGRATOR;
812
use rand::thread_rng;
9-
use sqlx::{postgres::PgConnectOptions, Connection, Either, PgConnection};
13+
use sqlx::{postgres::PgConnectOptions, types::Uuid, Connection, Either, PgConnection};
1014
use syn2mas::{synapse_config, LockedMasDatabase, MasWriter, SynapseReader};
11-
use tracing::{error, warn};
15+
use tracing::{error, info_span, warn, Instrument};
1216

1317
use crate::util::database_connection_from_config;
1418

@@ -75,6 +79,7 @@ enum Subcommand {
7579
const NUM_WRITER_CONNECTIONS: usize = 8;
7680

7781
impl Options {
82+
#[allow(clippy::too_many_lines)]
7883
pub async fn run(self, figment: &Figment) -> anyhow::Result<ExitCode> {
7984
warn!("This version of the syn2mas tool is EXPERIMENTAL and INCOMPLETE. Do not use it, except for TESTING.");
8085
if !self.experimental_accepted {
@@ -107,6 +112,35 @@ impl Options {
107112

108113
let mut mas_connection = database_connection_from_config(&config).await?;
109114

115+
MIGRATOR
116+
.run(&mut mas_connection)
117+
.instrument(info_span!("db.migrate"))
118+
.await
119+
.context("could not run migrations")?;
120+
121+
if matches!(&self.subcommand, Subcommand::Migrate { .. }) {
122+
// First perform a config sync
123+
// This is crucial to ensure we register upstream OAuth providers
124+
// in the MAS database
125+
//
126+
let config = SyncConfig::extract(figment)?;
127+
let clock = SystemClock::default();
128+
let encrypter = config.secrets.encrypter();
129+
130+
crate::sync::config_sync(
131+
config.upstream_oauth2,
132+
config.clients,
133+
&mut mas_connection,
134+
&encrypter,
135+
&clock,
136+
// Don't prune — we don't want to be unnecessarily destructive
137+
false,
138+
// Not a dry run — we do want to create the providers in the database
139+
false,
140+
)
141+
.await?;
142+
}
143+
110144
let Either::Left(mut mas_connection) = LockedMasDatabase::try_new(&mut mas_connection)
111145
.await
112146
.context("failed to issue query to lock database")?

0 commit comments

Comments
 (0)