Skip to content

Commit 6af24b3

Browse files
committed
Use username as default for database
1 parent 5d0f548 commit 6af24b3

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

sqlx-postgres/src/migrate.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ fn parse_for_maintenance(url: &str) -> Result<(PgConnectOptions, String), Error>
2121
let options = PgConnectOptions::from_str(url)?;
2222

2323
// pull out the name of the database to create
24-
let database = options
25-
.get_database()
26-
.unwrap_or(options.get_username())
27-
.to_owned();
24+
let database = options.get_database().to_owned();
2825

2926
// switch us to the maintenance database
3027
// use `postgres` _unless_ the database is postgres, in which case, use `template1`

sqlx-postgres/src/options/mod.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl PgConnectOptions {
9999
self.get_host(),
100100
self.get_port(),
101101
self.get_username(),
102-
self.get_database(),
102+
Some(self.get_database()),
103103
);
104104
}
105105

@@ -533,16 +533,20 @@ impl PgConnectOptions {
533533

534534
/// Get the current database name.
535535
///
536+
/// Defaults to username if not given.
537+
///
536538
/// # Example
537539
///
538540
/// ```rust
539541
/// # use sqlx_postgres::PgConnectOptions;
540-
/// let options = PgConnectOptions::new()
541-
/// .database("postgres");
542-
/// assert!(options.get_database().is_some());
542+
/// let options = PgConnectOptions::new().database("postgres");
543+
/// assert_eq!(options.get_database(), "postgres");
544+
///
545+
/// let options = PgConnectOptions::new().username("alice");
546+
/// assert_eq!(options.get_database(), "alice");
543547
/// ```
544-
pub fn get_database(&self) -> Option<&str> {
545-
self.database.as_deref()
548+
pub fn get_database(&self) -> &str {
549+
self.database.as_deref().unwrap_or(&self.username)
546550
}
547551

548552
/// Get the SSL mode.

0 commit comments

Comments
 (0)