From 76888590d46f4982a956452062f14e93f5ccdb37 Mon Sep 17 00:00:00 2001 From: Neil Bertram Date: Mon, 26 Mar 2018 12:19:29 +1300 Subject: [PATCH] [#3330] Fix logic leading to creation of PGPASSFILE when connecting to PostgreSQL with 'psql' --- includes/filesystem.inc | 4 ++++ src/Sql/SqlPgsql.php | 17 +++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/includes/filesystem.inc b/includes/filesystem.inc index 16c844b925..e72afdf424 100644 --- a/includes/filesystem.inc +++ b/includes/filesystem.inc @@ -216,6 +216,10 @@ function drush_mkdir($path) { function drush_program_exists($program) { if (drush_has_bash()) { $bucket = drush_bit_bucket(); + + // Remove environment variables (eg. PGPASSFILE=) before testing program. + $program = preg_replace('#^([A-Z0-9]+=.+? )+#', '', $program); + // Dont't use drush_op_system() since we don't want output during tests. system("command -v $program > $bucket 2>&1", $result_code); return $result_code === 0 ? TRUE : FALSE; diff --git a/src/Sql/SqlPgsql.php b/src/Sql/SqlPgsql.php index f8c805ca29..743374bfbb 100644 --- a/src/Sql/SqlPgsql.php +++ b/src/Sql/SqlPgsql.php @@ -15,9 +15,8 @@ class SqlPgsql extends SqlBase private function createPasswordFile() { - $password_file = null; $dbSpec = $this->getDbSpec(); - if (null !== ($this->getPasswordFile()) && isset($dbSpec['password'])) { + if (null == ($this->getPasswordFile()) && isset($dbSpec['password'])) { $pgpass_parts = [ empty($dbSpec['host']) ? 'localhost' : $dbSpec['host'], empty($dbSpec['port']) ? '5432' : $dbSpec['port'], @@ -34,10 +33,10 @@ private function createPasswordFile() $part = str_replace(['\\', ':'], ['\\\\', '\:'], $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() @@ -128,7 +127,13 @@ public function dumpCmd($table_selection) $data_only = $this->getOption('data-only'); $create_db = $this->getOption('create-db'); - $exec = 'pg_dump '; + + $environment = ""; + $pw_file = $this->createPasswordFile(); + if (isset($pw_file)) { + $environment = "PGPASSFILE={$pw_file} "; + } + $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)) {