From 9289ce7658d7e2b13d4835eaf50525a2507ebb9e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 26 Jun 2025 15:33:39 +0000 Subject: [PATCH 1/3] Initial plan From 5389fcc663af5a18b0271859dea2ef942950fc51 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 26 Jun 2025 15:46:27 +0000 Subject: [PATCH 2/3] Fix --defaults flag issue in SQL mode discovery Co-authored-by: lkraav <147228+lkraav@users.noreply.github.com> --- features/db-query.feature | 10 ++++++++++ src/DB_Command.php | 15 ++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/features/db-query.feature b/features/db-query.feature index 5831a536..6f4d2b89 100644 --- a/features/db-query.feature +++ b/features/db-query.feature @@ -84,6 +84,16 @@ 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 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..0257cd9e 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' ); From 8d1e72fe022b596aed28be4ede31e3bc7b0101de Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 26 Jun 2025 16:21:55 +0000 Subject: [PATCH 3/3] Fix SQL mode discovery to preserve all MySQL connection arguments - Replace empty foreach loop in get_current_sql_modes() with proper MySQL argument preservation using get_mysql_args() - Add ssl-verify-server-cert to allowed MySQL options list - Add test case to verify connection arguments are preserved during SQL mode discovery - Fixes issue where connection flags like --ssl-verify-server-cert were lost during SQL mode discovery Co-authored-by: lkraav <147228+lkraav@users.noreply.github.com> --- features/db-query.feature | 6 ++++++ src/DB_Command.php | 10 +++------- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/features/db-query.feature b/features/db-query.feature index 6f4d2b89..853c949f 100644 --- a/features/db-query.feature +++ b/features/db-query.feature @@ -94,6 +94,12 @@ Feature: Query the database with WordPress' MySQL config 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 0257cd9e..d767f128 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -2052,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', @@ -2162,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 = [];