Skip to content

Commit de1513b

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

File tree

1 file changed

+10
-6
lines changed
  • sqlx-postgres/src/options

1 file changed

+10
-6
lines changed

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)