|
1 |
| -use std::process::ExitCode; |
| 1 | +use std::{collections::HashMap, process::ExitCode}; |
2 | 2 |
|
3 | 3 | use anyhow::Context;
|
4 | 4 | use camino::Utf8PathBuf;
|
5 | 5 | use clap::Parser;
|
6 | 6 | 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; |
8 | 12 | use rand::thread_rng;
|
9 |
| -use sqlx::{postgres::PgConnectOptions, Connection, Either, PgConnection}; |
| 13 | +use sqlx::{postgres::PgConnectOptions, types::Uuid, Connection, Either, PgConnection}; |
10 | 14 | use syn2mas::{synapse_config, LockedMasDatabase, MasWriter, SynapseReader};
|
11 |
| -use tracing::{error, warn}; |
| 15 | +use tracing::{error, info_span, warn, Instrument}; |
12 | 16 |
|
13 | 17 | use crate::util::database_connection_from_config;
|
14 | 18 |
|
@@ -75,6 +79,7 @@ enum Subcommand {
|
75 | 79 | const NUM_WRITER_CONNECTIONS: usize = 8;
|
76 | 80 |
|
77 | 81 | impl Options {
|
| 82 | + #[allow(clippy::too_many_lines)] |
78 | 83 | pub async fn run(self, figment: &Figment) -> anyhow::Result<ExitCode> {
|
79 | 84 | warn!("This version of the syn2mas tool is EXPERIMENTAL and INCOMPLETE. Do not use it, except for TESTING.");
|
80 | 85 | if !self.experimental_accepted {
|
@@ -107,6 +112,35 @@ impl Options {
|
107 | 112 |
|
108 | 113 | let mut mas_connection = database_connection_from_config(&config).await?;
|
109 | 114 |
|
| 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 | + |
110 | 144 | let Either::Left(mut mas_connection) = LockedMasDatabase::try_new(&mut mas_connection)
|
111 | 145 | .await
|
112 | 146 | .context("failed to issue query to lock database")?
|
|
0 commit comments