Skip to content

Commit 5389fcc

Browse files
Copilotlkraav
andcommitted
Fix --defaults flag issue in SQL mode discovery
Co-authored-by: lkraav <[email protected]>
1 parent 9289ce7 commit 5389fcc

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

features/db-query.feature

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ Feature: Query the database with WordPress' MySQL config
8484
When I try `wp db query --no-defaults --debug`
8585
Then STDERR should match #Debug \(db\): Running shell command: /usr/bin/env (mysql|mariadb) --no-defaults --no-auto-rehash#
8686

87+
Scenario: SQL mode discovery respects --defaults flag
88+
Given a WP install
89+
90+
When I try `wp db query "SELECT 1;" --defaults --debug`
91+
Then STDERR should match #Running shell command: /usr/bin/env (mysql|mariadb) --no-auto-rehash#
92+
And STDERR should not match #Running shell command: /usr/bin/env (mysql|mariadb) --no-defaults#
93+
94+
When I try `wp db query "SELECT 1;" --debug`
95+
Then STDERR should match #Running shell command: /usr/bin/env (mysql|mariadb) --no-defaults --no-auto-rehash#
96+
8797
Scenario: SQL modes do not include any of the modes incompatible with WordPress
8898
Given a WP install
8999

src/DB_Command.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,9 @@ public function cli( $_, $assoc_args ) {
500500
*/
501501
public function query( $args, $assoc_args ) {
502502

503+
// Preserve the original defaults flag value before get_defaults_flag_string modifies $assoc_args.
504+
$original_assoc_args_for_sql_mode = $assoc_args;
505+
503506
$command = sprintf(
504507
'/usr/bin/env %s%s --no-auto-rehash',
505508
$this->get_mysql_command(),
@@ -516,7 +519,7 @@ public function query( $args, $assoc_args ) {
516519

517520
if ( isset( $assoc_args['execute'] ) ) {
518521
// Ensure that the SQL mode is compatible with WPDB.
519-
$assoc_args['execute'] = $this->get_sql_mode_query( $assoc_args ) . $assoc_args['execute'];
522+
$assoc_args['execute'] = $this->get_sql_mode_query( $original_assoc_args_for_sql_mode ) . $assoc_args['execute'];
520523
}
521524

522525
$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 ) {
805808
$result_file = sprintf( '%s.sql', DB_NAME );
806809
}
807810

811+
// Preserve the original defaults flag value before get_defaults_flag_string modifies $assoc_args.
812+
$original_assoc_args_for_sql_mode = $assoc_args;
813+
808814
// Process options to MySQL.
809815
$mysql_args = array_merge(
810816
[ 'database' => DB_NAME ],
@@ -821,7 +827,7 @@ public function import( $args, $assoc_args ) {
821827
? 'SOURCE %s;'
822828
: 'SET autocommit = 0; SET unique_checks = 0; SET foreign_key_checks = 0; SOURCE %s; COMMIT;';
823829

824-
$query = $this->get_sql_mode_query( $assoc_args ) . $query;
830+
$query = $this->get_sql_mode_query( $original_assoc_args_for_sql_mode ) . $query;
825831

826832
$mysql_args['execute'] = sprintf( $query, $result_file );
827833
} else {
@@ -1753,8 +1759,11 @@ private static function get_create_query() {
17531759
* @param array $assoc_args Optional. Associative array of arguments.
17541760
*/
17551761
protected function run_query( $query, $assoc_args = [] ) {
1762+
// Preserve the original defaults flag value before get_defaults_flag_string modifies $assoc_args.
1763+
$original_assoc_args_for_sql_mode = $assoc_args;
1764+
17561765
// Ensure that the SQL mode is compatible with WPDB.
1757-
$query = $this->get_sql_mode_query( $assoc_args ) . $query;
1766+
$query = $this->get_sql_mode_query( $original_assoc_args_for_sql_mode ) . $query;
17581767

17591768
WP_CLI::debug( "Query: {$query}", 'db' );
17601769

0 commit comments

Comments
 (0)