From a836e187e3c1a046846fe0f34a7e9eb78e9feab6 Mon Sep 17 00:00:00 2001 From: Markus Schirp Date: Sat, 19 Apr 2025 23:36:25 +0000 Subject: [PATCH] Add pg connection options creation without environment --- sqlx-postgres/src/options/mod.rs | 46 +++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/sqlx-postgres/src/options/mod.rs b/sqlx-postgres/src/options/mod.rs index 723721a97c..6835721b0a 100644 --- a/sqlx-postgres/src/options/mod.rs +++ b/sqlx-postgres/src/options/mod.rs @@ -68,11 +68,12 @@ impl PgConnectOptions { let database = var("PGDATABASE").ok(); + let ssl_mode = var("PGSSLMODE") + .ok() + .and_then(|v| v.parse().ok()) + .unwrap_or_default(); + PgConnectOptions { - port, - host, - socket: None, - username, password: var("PGPASSWORD").ok(), database, ssl_root_cert: var("PGSSLROOTCERT").ok().map(CertificateInput::from), @@ -81,15 +82,40 @@ impl PgConnectOptions { // `-----BEGIN CERTIFICATE-----` and so will not attempt to parse // a PEM-encoded private key. ssl_client_key: var("PGSSLKEY").ok().map(CertificateInput::from), - ssl_mode: var("PGSSLMODE") - .ok() - .and_then(|v| v.parse().ok()) - .unwrap_or_default(), - statement_cache_capacity: 100, application_name: var("PGAPPNAME").ok(), + options: var("PGOPTIONS").ok(), + ..Self::new_without_environment(host, port, username, ssl_mode) + } + } + + /// Create a minimal set of connection options _without_ applying any environment. + /// + /// To be used in situations the environment is not controlled enough to be relied upon. + /// Does not respect any `PG*` environment variables. + /// + /// See the type level-documentation for details. + pub fn new_without_environment( + host: String, + port: u16, + username: String, + ssl_mode: PgSslMode, + ) -> Self { + PgConnectOptions { + host, + port, + socket: None, + username, + password: None, + database: None, + ssl_mode, + ssl_root_cert: None, + ssl_client_cert: None, + ssl_client_key: None, + statement_cache_capacity: 100, + application_name: None, extra_float_digits: Some("2".into()), log_settings: Default::default(), - options: var("PGOPTIONS").ok(), + options: None, } }