diff --git a/features/db-query.feature b/features/db-query.feature index 5831a536..853c949f 100644 --- a/features/db-query.feature +++ b/features/db-query.feature @@ -84,6 +84,22 @@ Feature: Query the database with WordPress' MySQL config When I try `wp db query --no-defaults --debug` Then STDERR should match #Debug \(db\): Running shell command: /usr/bin/env (mysql|mariadb) --no-defaults --no-auto-rehash# + Scenario: SQL mode discovery respects --defaults flag + Given a WP install + + When I try `wp db query "SELECT 1;" --defaults --debug` + Then STDERR should match #Running shell command: /usr/bin/env (mysql|mariadb) --no-auto-rehash# + And STDERR should not match #Running shell command: /usr/bin/env (mysql|mariadb) --no-defaults# + + When I try `wp db query "SELECT 1;" --debug` + Then STDERR should match #Running shell command: /usr/bin/env (mysql|mariadb) --no-defaults --no-auto-rehash# + + Scenario: SQL mode discovery preserves MySQL connection arguments + Given a WP install + + When I try `wp db query "SELECT 1;" --host=testhost --port=3307 --debug` + Then STDERR should match #Final MySQL command: .* --host=testhost.*--port=3307# + Scenario: SQL modes do not include any of the modes incompatible with WordPress Given a WP install diff --git a/src/DB_Command.php b/src/DB_Command.php index fe4d0f49..d767f128 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -500,6 +500,9 @@ public function cli( $_, $assoc_args ) { */ public function query( $args, $assoc_args ) { + // Preserve the original defaults flag value before get_defaults_flag_string modifies $assoc_args. + $original_assoc_args_for_sql_mode = $assoc_args; + $command = sprintf( '/usr/bin/env %s%s --no-auto-rehash', $this->get_mysql_command(), @@ -516,7 +519,7 @@ public function query( $args, $assoc_args ) { if ( isset( $assoc_args['execute'] ) ) { // Ensure that the SQL mode is compatible with WPDB. - $assoc_args['execute'] = $this->get_sql_mode_query( $assoc_args ) . $assoc_args['execute']; + $assoc_args['execute'] = $this->get_sql_mode_query( $original_assoc_args_for_sql_mode ) . $assoc_args['execute']; } $is_row_modifying_query = isset( $assoc_args['execute'] ) && preg_match( '/\b(UPDATE|DELETE|INSERT|REPLACE|LOAD DATA)\b/i', $assoc_args['execute'] ); @@ -805,6 +808,9 @@ public function import( $args, $assoc_args ) { $result_file = sprintf( '%s.sql', DB_NAME ); } + // Preserve the original defaults flag value before get_defaults_flag_string modifies $assoc_args. + $original_assoc_args_for_sql_mode = $assoc_args; + // Process options to MySQL. $mysql_args = array_merge( [ 'database' => DB_NAME ], @@ -821,7 +827,7 @@ public function import( $args, $assoc_args ) { ? 'SOURCE %s;' : 'SET autocommit = 0; SET unique_checks = 0; SET foreign_key_checks = 0; SOURCE %s; COMMIT;'; - $query = $this->get_sql_mode_query( $assoc_args ) . $query; + $query = $this->get_sql_mode_query( $original_assoc_args_for_sql_mode ) . $query; $mysql_args['execute'] = sprintf( $query, $result_file ); } else { @@ -1753,8 +1759,11 @@ private static function get_create_query() { * @param array $assoc_args Optional. Associative array of arguments. */ protected function run_query( $query, $assoc_args = [] ) { + // Preserve the original defaults flag value before get_defaults_flag_string modifies $assoc_args. + $original_assoc_args_for_sql_mode = $assoc_args; + // Ensure that the SQL mode is compatible with WPDB. - $query = $this->get_sql_mode_query( $assoc_args ) . $query; + $query = $this->get_sql_mode_query( $original_assoc_args_for_sql_mode ) . $query; WP_CLI::debug( "Query: {$query}", 'db' ); @@ -2043,6 +2052,7 @@ private static function get_mysql_args( $assoc_args ) { 'ssl-fips-mode', 'ssl-key', 'ssl-mode', + 'ssl-verify-server-cert', 'syslog', 'table', 'tee', @@ -2153,13 +2163,8 @@ protected function get_current_sql_modes( $assoc_args ) { static $modes = null; // Make sure the provided arguments don't interfere with the expected - // output here. - $args = []; - foreach ( [] as $arg ) { - if ( isset( $assoc_args[ $arg ] ) ) { - $args[ $arg ] = $assoc_args[ $arg ]; - } - } + // output here. We need to preserve all valid MySQL arguments for connection. + $args = self::get_mysql_args( $assoc_args ); if ( null === $modes ) { $modes = [];