Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions features/db-query.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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 #Running shell command: .* --host=testhost.*--port=3307#

Scenario: SQL modes do not include any of the modes incompatible with WordPress
Given a WP install

Expand Down
23 changes: 18 additions & 5 deletions src/DB_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,9 @@
*/
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(),
Expand All @@ -535,7 +538,7 @@

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'] );
Expand Down Expand Up @@ -823,6 +826,9 @@
$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 ],
Expand All @@ -839,7 +845,7 @@
? '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 {
Expand Down Expand Up @@ -1161,7 +1167,7 @@
$size_key = floor( log( (float) $row['Size'] ) / log( 1000 ) );
$sizes = [ 'B', 'KB', 'MB', 'GB', 'TB' ];

$size_format = isset( $sizes[ $size_key ] ) ? $sizes[ $size_key ] : $sizes[0];

Check failure on line 1170 in src/DB_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Invalid array key type float.

Check failure on line 1170 in src/DB_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Invalid array key type float.
}

// Display the database size as a number.
Expand Down Expand Up @@ -1783,8 +1789,11 @@
* @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' );

Expand Down Expand Up @@ -2075,6 +2084,7 @@
'ssl-fips-mode',
'ssl-key',
'ssl-mode',
'ssl-verify-server-cert',
'syslog',
'table',
'tee',
Expand Down Expand Up @@ -2185,8 +2195,11 @@
static $modes = null;

// Make sure the provided arguments don't interfere with the expected
// output here.
$args = [];
// output here, while preserving all valid MySQL connection arguments
// (including --defaults, --host, --port, --ssl-* options) so that SQL
// mode discovery connects to the database with the same configuration
// as the main query.
$args = self::get_mysql_args( $assoc_args );

if ( null === $modes ) {
$modes = [];
Expand Down
Loading