Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/cli/src/commands/syn2mas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl Options {
synapse_config
.database
.to_sqlx_postgres()
.context("Synapse configuration does not use Postgres, cannot migrate.")?
.context("Synapse database configuration is invalid, cannot migrate.")?
};
let mut syn_conn = PgConnection::connect_with(&syn_connection_options)
.await
Expand Down
45 changes: 38 additions & 7 deletions crates/syn2mas/src/synapse_reader/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,21 @@ impl DatabaseSection {
/// environment variables) as Synapse normally runs, then the connection
/// options may not be valid.
///
/// Returns `None` if this database configuration is not configured for
/// Postgres.
#[must_use]
pub fn to_sqlx_postgres(&self) -> Option<PgConnectOptions> {
/// # Errors
///
/// Returns an error if this database configuration is invalid or
/// unsupported.
pub fn to_sqlx_postgres(&self) -> Result<PgConnectOptions, anyhow::Error> {
if self.name != SYNAPSE_DATABASE_DRIVER_NAME_PSYCOPG2 {
return None;
anyhow::bail!("syn2mas does not support the {} database driver", self.name);
}

if self.args.database.is_some() && self.args.dbname.is_some() {
anyhow::bail!(
"Only one of `database` and `dbname` may be specified in the Synapse database configuration, not both."
);
}

let mut opts = PgConnectOptions::new().application_name("syn2mas-synapse");

if let Some(host) = &self.args.host {
Expand All @@ -256,14 +264,17 @@ impl DatabaseSection {
if let Some(dbname) = &self.args.dbname {
opts = opts.database(dbname);
}
if let Some(database) = &self.args.database {
opts = opts.database(database);
}
if let Some(user) = &self.args.user {
opts = opts.username(user);
}
if let Some(password) = &self.args.password {
opts = opts.password(password);
}

Some(opts)
Ok(opts)
}
}

Expand All @@ -275,6 +286,8 @@ pub struct DatabaseArgsSuboption {
pub user: Option<String>,
pub password: Option<String>,
pub dbname: Option<String>,
// This is a deperecated way of specifying the database name.
pub database: Option<String>,
pub host: Option<String>,
pub port: Option<u16>,
}
Expand Down Expand Up @@ -357,7 +370,24 @@ mod test {
args: DatabaseArgsSuboption::default(),
}
.to_sqlx_postgres()
.is_none()
.is_err()
);

// Only one of `database` and `dbname` may be specified
assert!(
DatabaseSection {
name: "psycopg2".to_owned(),
args: DatabaseArgsSuboption {
user: Some("synapse_user".to_owned()),
password: Some("verysecret".to_owned()),
dbname: Some("synapse_db".to_owned()),
database: Some("synapse_db".to_owned()),
host: Some("synapse-db.example.com".to_owned()),
port: Some(42),
},
}
.to_sqlx_postgres()
.is_err()
);

assert_eq_options(
Expand All @@ -374,6 +404,7 @@ mod test {
user: Some("synapse_user".to_owned()),
password: Some("verysecret".to_owned()),
dbname: Some("synapse_db".to_owned()),
database: None,
host: Some("synapse-db.example.com".to_owned()),
port: Some(42),
},
Expand Down
Loading