@@ -238,13 +238,21 @@ impl DatabaseSection {
238
238
/// environment variables) as Synapse normally runs, then the connection
239
239
/// options may not be valid.
240
240
///
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 > {
245
246
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
+ ) ;
247
254
}
255
+
248
256
let mut opts = PgConnectOptions :: new ( ) . application_name ( "syn2mas-synapse" ) ;
249
257
250
258
if let Some ( host) = & self . args . host {
@@ -256,14 +264,17 @@ impl DatabaseSection {
256
264
if let Some ( dbname) = & self . args . dbname {
257
265
opts = opts. database ( dbname) ;
258
266
}
267
+ if let Some ( database) = & self . args . database {
268
+ opts = opts. database ( database) ;
269
+ }
259
270
if let Some ( user) = & self . args . user {
260
271
opts = opts. username ( user) ;
261
272
}
262
273
if let Some ( password) = & self . args . password {
263
274
opts = opts. password ( password) ;
264
275
}
265
276
266
- Some ( opts)
277
+ Ok ( opts)
267
278
}
268
279
}
269
280
@@ -275,6 +286,8 @@ pub struct DatabaseArgsSuboption {
275
286
pub user : Option < String > ,
276
287
pub password : Option < String > ,
277
288
pub dbname : Option < String > ,
289
+ // This is a deperecated way of specifying the database name.
290
+ pub database : Option < String > ,
278
291
pub host : Option < String > ,
279
292
pub port : Option < u16 > ,
280
293
}
@@ -357,7 +370,24 @@ mod test {
357
370
args: DatabaseArgsSuboption :: default ( ) ,
358
371
}
359
372
. 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( )
361
391
) ;
362
392
363
393
assert_eq_options (
@@ -374,6 +404,7 @@ mod test {
374
404
user : Some ( "synapse_user" . to_owned ( ) ) ,
375
405
password : Some ( "verysecret" . to_owned ( ) ) ,
376
406
dbname : Some ( "synapse_db" . to_owned ( ) ) ,
407
+ database : None ,
377
408
host : Some ( "synapse-db.example.com" . to_owned ( ) ) ,
378
409
port : Some ( 42 ) ,
379
410
} ,
0 commit comments