Skip to content

Commit d8d45b6

Browse files
committed
Make whoami an optional feature for postgres
Make the `whoami` dependency a feature gate (`whoami`) on `sqlx-postgres` so it can be disabled by users who don't need OS username fallback. When disabled, the default username falls back to "unknown" if `PGUSER` is not set. The feature is included in the top-level `sqlx` crate's default features, so existing behavior is preserved. Follows up on #1571 and #2319.
1 parent f5cdf33 commit d8d45b6

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ features = ["_unstable-docs"]
6060
rustdoc-args = ["--cfg", "docsrs"]
6161

6262
[features]
63-
default = ["any", "macros", "migrate", "json"]
63+
default = ["any", "macros", "migrate", "json", "whoami"]
6464

6565
derive = ["sqlx-macros/derive"]
6666
macros = ["derive", "sqlx-macros/macros", "sqlx-core/offline", "sqlx-mysql?/offline", "sqlx-postgres?/offline", "sqlx-sqlite?/offline"]
@@ -159,6 +159,7 @@ rust_decimal = ["sqlx-core/rust_decimal", "sqlx-macros?/rust_decimal", "sqlx-mys
159159
time = ["sqlx-core/time", "sqlx-macros?/time", "sqlx-mysql?/time", "sqlx-postgres?/time", "sqlx-sqlite?/time"]
160160
uuid = ["sqlx-core/uuid", "sqlx-macros?/uuid", "sqlx-mysql?/uuid", "sqlx-postgres?/uuid", "sqlx-sqlite?/uuid"]
161161
regexp = ["sqlx-sqlite?/regexp"]
162+
whoami = ["sqlx-postgres?/whoami"]
162163
bstr = ["sqlx-core/bstr"]
163164

164165
[workspace.dependencies]

sqlx-postgres/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ any = ["sqlx-core/any"]
1414
json = ["dep:serde", "dep:serde_json", "sqlx-core/json"]
1515
migrate = ["sqlx-core/migrate"]
1616
offline = ["json", "sqlx-core/offline", "smallvec/serde"]
17+
whoami = ["dep:whoami"]
1718

1819
# Type Integration features
1920
bigdecimal = ["dep:bigdecimal", "dep:num-bigint", "sqlx-core/bigdecimal"]
@@ -65,7 +66,7 @@ num-bigint = { version = "0.4.3", optional = true }
6566
smallvec = { version = "1.7.0" }
6667
stringprep = "0.1.2"
6768
tracing = { version = "0.1.37", features = ["log"] }
68-
whoami = { version = "2.0.2", default-features = false }
69+
whoami = { version = "2.0.2", default-features = false, optional = true }
6970

7071
dotenvy.workspace = true
7172
thiserror.workspace = true

sqlx-postgres/src/options/mod.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,7 @@ impl PgConnectOptions {
6464
.or_else(|| var("PGHOST").ok())
6565
.unwrap_or_else(|| default_host(port));
6666

67-
let username = if let Ok(username) = var("PGUSER") {
68-
username
69-
} else if let Ok(username) = whoami::username() {
70-
username
71-
} else {
72-
// keep the same fallback as previous version
73-
"unknown".to_string()
74-
};
67+
let username = var("PGUSER").ok().unwrap_or_else(default_username);
7568

7669
let database = var("PGDATABASE").ok();
7770

@@ -582,6 +575,16 @@ impl PgConnectOptions {
582575
}
583576
}
584577

578+
fn default_username() -> String {
579+
#[cfg(feature = "whoami")]
580+
if let Ok(username) = whoami::username() {
581+
return username;
582+
}
583+
584+
// keep the same fallback as previous version
585+
"unknown".to_string()
586+
}
587+
585588
fn default_host(port: u16) -> String {
586589
// try to check for the existence of a unix socket and uses that
587590
let socket = format!(".s.PGSQL.{port}");

0 commit comments

Comments
 (0)