Skip to content

Try refactor wp db query --defaults handling, fix #237 #285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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 #Final MySQL command: .* --host=testhost.*--port=3307#

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

Expand Down
25 changes: 15 additions & 10 deletions src/DB_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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'] );
Expand Down Expand Up @@ -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 ],
Expand All @@ -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 {
Expand Down Expand Up @@ -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' );

Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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 = [];
Expand Down
Loading