Skip to content

Commit

Permalink
Set PGPASSFILE also in the dump command for PostgreSQL (drush-ops#4022)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcnventura committed Apr 23, 2020
1 parent 6cf6e0f commit 123b44f
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/Drush/Sql/Sqlpgsql.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Sqlpgsql extends SqlBase {
private $password_file = NULL;

private function password_file() {
if (!isset($password_file) && isset($this->db_spec['password'])) {
if (!isset($this->password_file) && isset($this->db_spec['password'])) {
$pgpass_parts = array(
empty($this->db_spec['host']) ? 'localhost' : $this->db_spec['host'],
empty($this->db_spec['port']) ? '5432' : $this->db_spec['port'],
Expand All @@ -30,18 +30,23 @@ private function password_file() {
$part = str_replace(array('\\', ':'), array('\\\\', '\:'), $part);
});
$pgpass_contents = implode(':', $pgpass_parts);
$password_file = drush_save_data_to_temp_file($pgpass_contents);
chmod($password_file, 0600);
$this->password_file = drush_save_data_to_temp_file($pgpass_contents);
chmod($this->password_file, 0600);
}
return $password_file;
return $this->password_file;
}

public function command() {
private function setPgPassFile() {
$environment = "";
$pw_file = $this->password_file();
if (isset($pw_file)) {
$environment = "PGPASSFILE={$pw_file} ";
}
return $environment;
}

public function command() {
$environment = $this->setPgPassFile();
return "{$environment}psql -q";
}

Expand Down Expand Up @@ -115,7 +120,8 @@ public function dumpCmd($table_selection) {
$data_only = drush_get_option('data-only');

$create_db = drush_get_option('create-db');
$exec = 'pg_dump ';
$environment = $this->setPgPassFile();
$exec = "{$environment}pg_dump ";
// Unlike psql, pg_dump does not take a '--dbname=' before the database name.
$extra = str_replace('--dbname=', ' ', $this->creds());
if (isset($data_only)) {
Expand Down

0 comments on commit 123b44f

Please sign in to comment.