Skip to content

Commit 2ecf8a3

Browse files
CopilotswissspidyCopilot
authored
Filter empty database parameters to prevent MySQL command failures (#308)
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascalb@google.com>
1 parent b39251c commit 2ecf8a3

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

features/db-check.feature

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,38 @@ Feature: Check the database
162162
When I try `wp db check --no-defaults --debug`
163163
Then STDERR should match #Debug \(db\): Running shell command: /usr/bin/env (mysqlcheck|mariadb-check) --no-defaults %s#
164164

165+
@require-mysql-or-mariadb
166+
Scenario: Empty DB credentials should not cause empty parameter errors
167+
Given an empty directory
168+
And WP files
169+
170+
When I run `wp config create {CORE_CONFIG_SETTINGS} --dbcharset="" --skip-check`
171+
Then STDOUT should not be empty
172+
173+
When I run `cat wp-config.php`
174+
Then STDOUT should contain:
175+
"""
176+
define( 'DB_CHARSET', '' );
177+
"""
178+
179+
When I run `wp db create`
180+
Then STDOUT should not be empty
181+
182+
When I try `wp db check --debug`
183+
Then the return code should be 0
184+
And STDOUT should contain:
185+
"""
186+
Success: Database checked.
187+
"""
188+
And STDERR should not contain:
189+
"""
190+
--default-character-set=''
191+
"""
192+
And STDERR should not contain:
193+
"""
194+
--default-character-set=
195+
"""
196+
165197
@require-sqlite
166198
Scenario: SQLite commands that show warnings
167199
Given a WP install

src/DB_Command.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,6 +1990,29 @@ private static function run( $cmd, $assoc_args = [], $send_to_shell = true, $int
19901990

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

1993+
// Filter out empty string values to avoid passing empty parameters to MySQL commands
1994+
// which can cause errors like "Character set '' is not a compiled character set".
1995+
// However, keep empty strings for credential options like 'user' and 'pass' so that
1996+
// an explicitly empty value is not silently converted into an omitted parameter.
1997+
$final_args = array_filter(
1998+
$final_args,
1999+
static function ( $value, $key ) {
2000+
// Always drop null values.
2001+
if ( null === $value ) {
2002+
return false;
2003+
}
2004+
2005+
// Preserve explicitly empty credential arguments.
2006+
if ( '' === $value && in_array( $key, [ 'user', 'pass' ], true ) ) {
2007+
return true;
2008+
}
2009+
2010+
// For all other options, filter out empty strings.
2011+
return '' !== $value;
2012+
},
2013+
ARRAY_FILTER_USE_BOTH
2014+
);
2015+
19932016
// Adapt ordering of arguments.
19942017
uksort(
19952018
$final_args,

0 commit comments

Comments
 (0)