Skip to content

Commit a96ca89

Browse files
committed
syn2mas: allow setting the db name via the database field
This is supported (but deprecated) by psycopg2, which Synapse uses
1 parent b9c409f commit a96ca89

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-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: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,19 @@ 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);
247248
}
249+
250+
if self.args.database.is_some() && self.args.dbname.is_some() {
251+
anyhow::bail!("Only one of `database` and `dbname` may be specified in the Synapse database configuration, not both.");
252+
}
253+
248254
let mut opts = PgConnectOptions::new().application_name("syn2mas-synapse");
249255

250256
if let Some(host) = &self.args.host {
@@ -256,14 +262,17 @@ impl DatabaseSection {
256262
if let Some(dbname) = &self.args.dbname {
257263
opts = opts.database(dbname);
258264
}
265+
if let Some(database) = &self.args.database {
266+
opts = opts.database(database);
267+
}
259268
if let Some(user) = &self.args.user {
260269
opts = opts.username(user);
261270
}
262271
if let Some(password) = &self.args.password {
263272
opts = opts.password(password);
264273
}
265274

266-
Some(opts)
275+
Ok(opts)
267276
}
268277
}
269278

@@ -275,6 +284,8 @@ pub struct DatabaseArgsSuboption {
275284
pub user: Option<String>,
276285
pub password: Option<String>,
277286
pub dbname: Option<String>,
287+
// This is a deperecated way of specifying the database name.
288+
pub database: Option<String>,
278289
pub host: Option<String>,
279290
pub port: Option<u16>,
280291
}
@@ -357,7 +368,24 @@ mod test {
357368
args: DatabaseArgsSuboption::default(),
358369
}
359370
.to_sqlx_postgres()
360-
.is_none()
371+
.is_err()
372+
);
373+
374+
// Only one of `database` and `dbname` may be specified
375+
assert!(
376+
DatabaseSection {
377+
name: "psycopg2".to_owned(),
378+
args: DatabaseArgsSuboption {
379+
user: Some("synapse_user".to_owned()),
380+
password: Some("verysecret".to_owned()),
381+
dbname: Some("synapse_db".to_owned()),
382+
database: Some("synapse_db".to_owned()),
383+
host: Some("synapse-db.example.com".to_owned()),
384+
port: Some(42),
385+
},
386+
}
387+
.to_sqlx_postgres()
388+
.is_err()
361389
);
362390

363391
assert_eq_options(
@@ -374,6 +402,7 @@ mod test {
374402
user: Some("synapse_user".to_owned()),
375403
password: Some("verysecret".to_owned()),
376404
dbname: Some("synapse_db".to_owned()),
405+
database: None,
377406
host: Some("synapse-db.example.com".to_owned()),
378407
port: Some(42),
379408
},

0 commit comments

Comments
 (0)