Skip to content
Open
28 changes: 28 additions & 0 deletions features/db-check.feature
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,31 @@ Feature: Check the database
When I try `wp db check --no-defaults --debug`
Then STDERR should match #Debug \(db\): Running shell command: /usr/bin/env (mysqlcheck|mariadb-check) --no-defaults %s#

Scenario: Empty DB credentials should not cause empty parameter errors
Given a WP install
And a wp-config.php file:
"""
<?php
define( 'DB_NAME', 'wp_cli_test' );
define( 'DB_USER', 'wp_cli_test' );
define( 'DB_PASSWORD', 'password1' );
define( 'DB_HOST', '' );
define( 'DB_CHARSET', 'utf8' );
$table_prefix = 'wp_';
require_once ABSPATH . 'wp-settings.php';
"""

When I try `wp db check --debug`
Then STDERR should contain:
"""
Debug (db): Final MySQL command:
"""
And STDERR should not contain:
"""
--host=''
"""
And STDERR should not contain:
"""
--host=
"""

23 changes: 23 additions & 0 deletions src/DB_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -1853,6 +1853,29 @@ private static function run( $cmd, $assoc_args = [], $send_to_shell = true, $int

$final_args = array_merge( $required, $assoc_args );

// Filter out empty string values to avoid passing empty parameters to MySQL commands
// which can cause errors like "Character set '' is not a compiled character set".
// However, keep empty strings for credential options like 'user' and 'pass' so that
// an explicitly empty value is not silently converted into an omitted parameter.
$final_args = array_filter(
$final_args,
static function ( $value, $key ) {
// Always drop null values.
if ( null === $value ) {
return false;
}

// Preserve explicitly empty credential arguments.
if ( '' === $value && in_array( $key, [ 'user', 'pass' ], true ) ) {
return true;
}

// For all other options, filter out empty strings.
return '' !== $value;
},
ARRAY_FILTER_USE_BOTH
);

// Adapt ordering of arguments.
uksort(
$final_args,
Expand Down
Loading