Skip to content

Commit 8ca173e

Browse files
authored
syn2mas: allow setting the db name via the database field (#4496)
2 parents b9c409f + 73cfc2c commit 8ca173e

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

crates/cli/src/commands/syn2mas.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl Options {
105105
synapse_config
106106
.database
107107
.to_sqlx_postgres()
108-
.context("Synapse configuration does not use Postgres, cannot migrate.")?
108+
.context("Synapse database configuration is invalid, cannot migrate.")?
109109
};
110110
let mut syn_conn = PgConnection::connect_with(&syn_connection_options)
111111
.await

crates/syn2mas/src/synapse_reader/config/mod.rs

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,21 @@ impl DatabaseSection {
238238
/// environment variables) as Synapse normally runs, then the connection
239239
/// options may not be valid.
240240
///
241-
/// Returns `None` if this database configuration is not configured for
242-
/// Postgres.
243-
#[must_use]
244-
pub fn to_sqlx_postgres(&self) -> Option<PgConnectOptions> {
241+
/// # Errors
242+
///
243+
/// Returns an error if this database configuration is invalid or
244+
/// unsupported.
245+
pub fn to_sqlx_postgres(&self) -> Result<PgConnectOptions, anyhow::Error> {
245246
if self.name != SYNAPSE_DATABASE_DRIVER_NAME_PSYCOPG2 {
246-
return None;
247+
anyhow::bail!("syn2mas does not support the {} database driver", self.name);
248+
}
249+
250+
if self.args.database.is_some() && self.args.dbname.is_some() {
251+
anyhow::bail!(
252+
"Only one of `database` and `dbname` may be specified in the Synapse database configuration, not both."
253+
);
247254
}
255+
248256
let mut opts = PgConnectOptions::new().application_name("syn2mas-synapse");
249257

250258
if let Some(host) = &self.args.host {
@@ -256,14 +264,17 @@ impl DatabaseSection {
256264
if let Some(dbname) = &self.args.dbname {
257265
opts = opts.database(dbname);
258266
}
267+
if let Some(database) = &self.args.database {
268+
opts = opts.database(database);
269+
}
259270
if let Some(user) = &self.args.user {
260271
opts = opts.username(user);
261272
}
262273
if let Some(password) = &self.args.password {
263274
opts = opts.password(password);
264275
}
265276

266-
Some(opts)
277+
Ok(opts)
267278
}
268279
}
269280

@@ -275,6 +286,8 @@ pub struct DatabaseArgsSuboption {
275286
pub user: Option<String>,
276287
pub password: Option<String>,
277288
pub dbname: Option<String>,
289+
// This is a deperecated way of specifying the database name.
290+
pub database: Option<String>,
278291
pub host: Option<String>,
279292
pub port: Option<u16>,
280293
}
@@ -357,7 +370,24 @@ mod test {
357370
args: DatabaseArgsSuboption::default(),
358371
}
359372
.to_sqlx_postgres()
360-
.is_none()
373+
.is_err()
374+
);
375+
376+
// Only one of `database` and `dbname` may be specified
377+
assert!(
378+
DatabaseSection {
379+
name: "psycopg2".to_owned(),
380+
args: DatabaseArgsSuboption {
381+
user: Some("synapse_user".to_owned()),
382+
password: Some("verysecret".to_owned()),
383+
dbname: Some("synapse_db".to_owned()),
384+
database: Some("synapse_db".to_owned()),
385+
host: Some("synapse-db.example.com".to_owned()),
386+
port: Some(42),
387+
},
388+
}
389+
.to_sqlx_postgres()
390+
.is_err()
361391
);
362392

363393
assert_eq_options(
@@ -374,6 +404,7 @@ mod test {
374404
user: Some("synapse_user".to_owned()),
375405
password: Some("verysecret".to_owned()),
376406
dbname: Some("synapse_db".to_owned()),
407+
database: None,
377408
host: Some("synapse-db.example.com".to_owned()),
378409
port: Some(42),
379410
},

0 commit comments

Comments
 (0)