From 0bcaa756c60e55dc04ff42e09461ad3540325952 Mon Sep 17 00:00:00 2001 From: scribu Date: Fri, 5 Apr 2013 21:21:20 +0300 Subject: [PATCH 1/3] add --defaults-file=/dev/null to mysql commands --- php/commands/db.php | 33 ++++++++++++++++----------------- php/utils.php | 17 +++++++++++------ 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/php/commands/db.php b/php/commands/db.php index fbcf636aac..300b82740b 100644 --- a/php/commands/db.php +++ b/php/commands/db.php @@ -1,5 +1,7 @@ get_file_name( $args ); - self::run( \WP_CLI\Utils\create_cmd( - 'mysqldump %s --user=%s --host=%s --result-file %s', + self::run( 'mysqldump', Utils\create_cmd( + '%s --user=%s --host=%s --result-file %s', DB_NAME, DB_USER, DB_HOST, $result_file ) ); WP_CLI::success( sprintf( 'Exported to %s', $result_file ) ); @@ -113,8 +115,8 @@ function export( $args, $assoc_args ) { function import( $args, $assoc_args ) { $result_file = $this->get_file_name( $args ); - self::run( \WP_CLI\Utils\create_cmd( - 'mysql %s --user=%s --host=%s < %s', + self::run( 'mysql', Utils\create_cmd( + '%s --user=%s --host=%s < %s', DB_NAME, DB_USER, DB_HOST, $result_file ) ); WP_CLI::success( sprintf( 'Imported from %s', $result_file ) ); @@ -128,20 +130,17 @@ private function get_file_name( $args ) { } private static function run_query( $query ) { - return \WP_CLI\Utils\run_mysql_query( $query, array( + Utils\run_mysql_query( $query, array( 'host' => DB_HOST, 'user' => DB_USER, 'pass' => DB_PASSWORD, ) ); } - private static function run( $cmd ) { - $old_val = getenv( 'MYSQL_PWD' ); - - putenv( 'MYSQL_PWD=' . DB_PASSWORD ); - WP_CLI::launch( $cmd ); - putenv( 'MYSQL_PWD=' . $old_val ); + private static function run( $cmd, $args ) { + Utils\run_mysql_command( $cmd, $args, DB_PASSWORD ); } } WP_CLI::add_command( 'db', 'DB_Command' ); + diff --git a/php/utils.php b/php/utils.php index c54e15e6d1..73e89b9efd 100644 --- a/php/utils.php +++ b/php/utils.php @@ -362,17 +362,22 @@ function find_subcommand( $args ) { return $command; } -function run_mysql_query( $query, $args, $dry_run = false ) { +function run_mysql_query( $query, $args ) { // TODO: use PDO? - $cmd = \WP_CLI\Utils\create_cmd( 'mysql --host=%s --user=%s --execute=%s', - $args['host'], $args['user'], $query - ); + $arg_str = create_cmd( '--host=%s --user=%s --execute=%s', + $args['host'], $args['user'], $query ); + + run_mysql_command( 'mysql', $arg_str, $args['pass'] ); +} +function run_mysql_command( $cmd, $arg_str, $pass ) { $old_val = getenv( 'MYSQL_PWD' ); - putenv( 'MYSQL_PWD=' . $args['pass'] ); - \WP_CLI::launch( $cmd ); + $final_cmd = "$cmd --defaults-file=/dev/null $arg_str"; + + putenv( 'MYSQL_PWD=' . $pass ); + \WP_CLI::launch( $final_cmd ); putenv( 'MYSQL_PWD=' . $old_val ); } From 981542f3d8ca0f18fa8431252c7b1976487a13ce Mon Sep 17 00:00:00 2001 From: scribu Date: Fri, 5 Apr 2013 22:05:15 +0300 Subject: [PATCH 2/3] use run_mysql_query() in functional tests too --- features/bootstrap/FeatureContext.php | 19 +++++++++++-------- php/utils.php | 4 +++- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php index d70fd2be59..07038d3e8e 100644 --- a/features/bootstrap/FeatureContext.php +++ b/features/bootstrap/FeatureContext.php @@ -5,6 +5,8 @@ Behat\Behat\Context\BehatContext, Behat\Behat\Event\SuiteEvent; +use \WP_CLI\Utils; + require_once 'PHPUnit/Framework/Assert/Functions.php'; require_once __DIR__ . '/../../php/utils.php'; @@ -92,21 +94,22 @@ public function get_cache_path( $file ) { if ( !$path ) { $path = sys_get_temp_dir() . '/wp-cli-test-cache'; - system( \WP_CLI\Utils\create_cmd( 'mkdir -p %s', $path ) ); + system( Utils\create_cmd( 'mkdir -p %s', $path ) ); } return $path . '/' . $file; } public function download_file( $url, $path ) { - system( \WP_CLI\Utils\create_cmd( 'curl -sSL %s > %s', $url, $path ) ); + system( Utils\create_cmd( 'curl -sSL %s > %s', $url, $path ) ); } private static function run_sql( $sql ) { - putenv( 'MYSQL_PWD=' . self::$db_settings['dbpass'] ); - - system( \WP_CLI\Utils\create_cmd( 'mysql -u%s -e %s', - self::$db_settings['dbuser'], $sql ) ); + Utils\run_mysql_query( $sql, array( + 'host' => 'localhost', + 'user' => self::$db_settings['dbuser'], + 'pass' => self::$db_settings['dbpass'], + ) ); } public function create_db() { @@ -121,7 +124,7 @@ public function drop_db() { private function _run( $command, $assoc_args, $subdir = '' ) { if ( !empty( $assoc_args ) ) - $command .= \WP_CLI\Utils\assoc_args_to_str( $assoc_args ); + $command .= Utils\assoc_args_to_str( $assoc_args ); $subdir = $this->get_path( $subdir ); @@ -184,7 +187,7 @@ public function download_wordpress_files( $subdir = '' ) { if ( $subdir ) mkdir( $dest_dir ); - $cmd = \WP_CLI\Utils\create_cmd( "cp -r %s/* %s", $cache_dir, $dest_dir ); + $cmd = Utils\create_cmd( "cp -r %s/* %s", $cache_dir, $dest_dir ); system( $cmd ); } diff --git a/php/utils.php b/php/utils.php index 73e89b9efd..51f25b541d 100644 --- a/php/utils.php +++ b/php/utils.php @@ -377,7 +377,9 @@ function run_mysql_command( $cmd, $arg_str, $pass ) { $final_cmd = "$cmd --defaults-file=/dev/null $arg_str"; putenv( 'MYSQL_PWD=' . $pass ); - \WP_CLI::launch( $final_cmd ); + $r = proc_close( proc_open( $final_cmd, array( STDIN, STDOUT, STDERR ), $pipes ) ); putenv( 'MYSQL_PWD=' . $old_val ); + + if ( $r ) exit( $r ); } From e57c9ea38f243eefc1e17168f113bf960c63b17e Mon Sep 17 00:00:00 2001 From: scribu Date: Tue, 9 Apr 2013 20:39:44 +0300 Subject: [PATCH 3/3] tests: add IF NOT EXISTS to create_db() --- features/bootstrap/FeatureContext.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php index 07038d3e8e..fd2fdf2d9f 100644 --- a/features/bootstrap/FeatureContext.php +++ b/features/bootstrap/FeatureContext.php @@ -114,7 +114,7 @@ private static function run_sql( $sql ) { public function create_db() { $dbname = self::$db_settings['dbname']; - self::run_sql( "CREATE DATABASE $dbname" ); + self::run_sql( "CREATE DATABASE IF NOT EXISTS $dbname" ); } public function drop_db() {