Skip to content

Commit 918b42a

Browse files
committed
refactor: update psql to manage setting the PGPASSWORD environment variable when pg_password is set
1 parent 544a3c9 commit 918b42a

File tree

4 files changed

+42
-15
lines changed

4 files changed

+42
-15
lines changed

Cargo.lock

Lines changed: 10 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

postgresql_embedded/src/command/psql.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub struct PsqlBuilder {
4141
username: Option<OsString>,
4242
no_password: bool,
4343
password: bool,
44+
pg_password: Option<OsString>,
4445
}
4546

4647
impl PsqlBuilder {
@@ -267,6 +268,12 @@ impl PsqlBuilder {
267268
self.password = true;
268269
self
269270
}
271+
272+
/// database user name
273+
pub fn pg_password<S: AsRef<OsStr>>(mut self, pg_password: S) -> Self {
274+
self.pg_password = Some(pg_password.as_ref().to_os_string());
275+
self
276+
}
270277
}
271278

272279
impl CommandBuilder for PsqlBuilder {
@@ -440,6 +447,16 @@ impl CommandBuilder for PsqlBuilder {
440447

441448
args
442449
}
450+
451+
fn get_envs(&self) -> Vec<(OsString, OsString)> {
452+
let mut envs: Vec<(OsString, OsString)> = Vec::new();
453+
454+
if let Some(password) = &self.pg_password {
455+
envs.push(("PGPASSWORD".into(), password.into()));
456+
}
457+
458+
envs
459+
}
443460
}
444461

445462
#[cfg(test)]
@@ -496,10 +513,11 @@ mod tests {
496513
.username("postgres")
497514
.no_password()
498515
.password()
516+
.pg_password("password")
499517
.build();
500518

501519
assert_eq!(
502-
r#""psql" "--command" "SELECT * FROM test" "--dbname" "dbname" "--file" "test.sql" "--list" "--variable" "ON_ERROR_STOP=1" "--version" "--no-psqlrc" "--single-transaction" "--help" "options" "--echo-all" "--echo-errors" "--echo-queries" "--echo-hidden" "--log-file" "psql.log" "--no-readline" "--output" "output.txt" "--quiet" "--single-step" "--single-line" "--no-align" "--csv" "--field-separator" "|" "--html" "--pset" "border=1" "--record-separator" "\n" "--tuples-only" "--table-attr" "width=100" "--expanded" "--field-separator-zero" "--record-separator-zero" "--host" "localhost" "--port" "5432" "--username" "postgres" "--no-password" "--password""#,
520+
r#"PGPASSWORD="password" "psql" "--command" "SELECT * FROM test" "--dbname" "dbname" "--file" "test.sql" "--list" "--variable" "ON_ERROR_STOP=1" "--version" "--no-psqlrc" "--single-transaction" "--help" "options" "--echo-all" "--echo-errors" "--echo-queries" "--echo-hidden" "--log-file" "psql.log" "--no-readline" "--output" "output.txt" "--quiet" "--single-step" "--single-line" "--no-align" "--csv" "--field-separator" "|" "--html" "--pset" "border=1" "--record-separator" "\n" "--tuples-only" "--table-attr" "width=100" "--expanded" "--field-separator-zero" "--record-separator-zero" "--host" "localhost" "--port" "5432" "--username" "postgres" "--no-password" "--password""#,
503521
command.to_command_string()
504522
);
505523
}

postgresql_embedded/src/command/traits.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@ pub trait CommandBuilder {
2323
}
2424

2525
/// Get the arguments for the command
26-
fn get_args(&self) -> Vec<OsString>;
26+
fn get_args(&self) -> Vec<OsString> {
27+
vec![]
28+
}
29+
30+
/// Get the environment variables for the command
31+
fn get_envs(&self) -> Vec<(OsString, OsString)> {
32+
vec![]
33+
}
2734

2835
/// Build a standard Command
2936
fn build(self) -> std::process::Command
@@ -34,6 +41,7 @@ pub trait CommandBuilder {
3441
let mut command = std::process::Command::new(program_file);
3542

3643
command.args(self.get_args());
44+
command.envs(self.get_envs());
3745
command
3846
}
3947

@@ -47,6 +55,7 @@ pub trait CommandBuilder {
4755
let mut command = tokio::process::Command::new(program_file);
4856

4957
command.args(self.get_args());
58+
command.envs(self.get_envs());
5059
command
5160
}
5261
}

postgresql_embedded/src/postgresql.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ impl PostgreSQL {
338338
.host(&self.settings.host)
339339
.port(self.settings.port)
340340
.username(&self.settings.username)
341+
.pg_password(&self.settings.password)
341342
.no_psqlrc()
342343
.no_align()
343344
.tuples_only();
@@ -373,6 +374,7 @@ impl PostgreSQL {
373374
.host(&self.settings.host)
374375
.port(self.settings.port)
375376
.username(&self.settings.username)
377+
.pg_password(&self.settings.password)
376378
.no_psqlrc()
377379
.no_align()
378380
.tuples_only();
@@ -403,6 +405,7 @@ impl PostgreSQL {
403405
.host(&self.settings.host)
404406
.port(self.settings.port)
405407
.username(&self.settings.username)
408+
.pg_password(&self.settings.password)
406409
.no_psqlrc()
407410
.no_align()
408411
.tuples_only();
@@ -428,8 +431,6 @@ impl PostgreSQL {
428431
command_builder: B,
429432
) -> Result<(String, String)> {
430433
let mut command = command_builder.build();
431-
// TODO: move this into the command builder
432-
command.env("PGPASSWORD", &self.settings.password);
433434
command.execute(self.settings.timeout).await
434435
}
435436

@@ -440,8 +441,6 @@ impl PostgreSQL {
440441
command_builder: B,
441442
) -> Result<(String, String)> {
442443
let mut command = command_builder.build_tokio();
443-
// TODO: move this into the command builder
444-
command.env("PGPASSWORD", &self.settings.password);
445444
command.execute(self.settings.timeout).await
446445
}
447446
}

0 commit comments

Comments
 (0)