Skip to content

Commit ae8b0b6

Browse files
committed
Merge branch 'main' into copilot/fix-wp-db-query-fallback
2 parents 41b410d + 24ed6d8 commit ae8b0b6

7 files changed

Lines changed: 131 additions & 31 deletions

File tree

‎.github/workflows/copilot-setup-steps.yml‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,29 @@ on:
99
paths:
1010
- .github/workflows/copilot-setup-steps.yml
1111

12+
permissions:
13+
contents: read
14+
1215
jobs:
1316
copilot-setup-steps:
17+
name: Setup environment
1418
runs-on: ubuntu-latest
1519
permissions:
1620
contents: read
1721

1822
steps:
1923
- name: Checkout code
20-
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
24+
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
25+
with:
26+
persist-credentials: false
2127

2228
- name: Check existence of composer.json file
2329
id: check_composer_file
2430
run: echo "files_exists=$(test -f composer.json && echo true || echo false)" >> "$GITHUB_OUTPUT"
2531

2632
- name: Set up PHP environment
2733
if: steps.check_composer_file.outputs.files_exists == 'true'
28-
uses: shivammathur/setup-php@accd6127cb78bee3e8082180cb391013d204ef9f # v2
34+
uses: shivammathur/setup-php@f3e473d116dcccaddc5834248c87452386958240 # v2
2935
with:
3036
php-version: 'latest'
3137
ini-values: zend.assertions=1, error_reporting=-1, display_errors=On
@@ -36,6 +42,6 @@ jobs:
3642

3743
- name: Install Composer dependencies & cache dependencies
3844
if: steps.check_composer_file.outputs.files_exists == 'true'
39-
uses: ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda # v4
45+
uses: ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda # 4.0.0
4046
env:
4147
COMPOSER_ROOT_VERSION: dev-${{ github.event.repository.default_branch }}

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ wp db export [<file>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>]
437437
**Alias:** `dump`
438438

439439
Runs `mysqldump` utility using `DB_HOST`, `DB_NAME`, `DB_USER` and
440-
`DB_PASSWORD` database credentials specified in wp-config.php. Accepts any valid `mysqldump` flags.
440+
`DB_PASSWORD` database credentials specified in wp-config.php. Accepts any valid [`mysqldump` flags](https://dev.mysql.com/doc/en/mysqldump.html#mysqldump-option-summary).
441441

442442
**OPTIONS**
443443

@@ -1038,7 +1038,7 @@ This project is licensed under the MIT License. See the [LICENSE](LICENSE) file
10381038

10391039
## Support
10401040

1041-
GitHub issues aren't for general support questions, but there are other venues you can try: https://wp-cli.org/#support
1041+
GitHub issues aren't for general support questions. For support resources and next steps, see the WP-CLI Support page: https://make.wordpress.org/cli/handbook/support/
10421042

10431043

10441044
*This README.md is generated dynamically from the project's codebase using `wp scaffold package-readme` ([doc](https://github.com/wp-cli/scaffold-package-command#wp-scaffold-package-readme)). To suggest changes, please submit a pull request against the corresponding part of the codebase.*

‎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

‎features/db-export.feature‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,16 @@ Feature: Export a WordPress database
147147

148148
When I try `wp db export --no-defaults --debug`
149149
Then STDERR should match #Debug \(db\): Running initial shell command: /usr/bin/env (mysqldump|mariadb-dump) --no-defaults#
150+
151+
@skip-sqlite
152+
@skip-windows
153+
Scenario: Export database when PHP exec() is disabled
154+
Given a WP install
155+
156+
When I try `{INVOKE_WP_CLI_WITH_PHP_ARGS--ddisable_functions=exec} db export wp_cli_test.sql --porcelain`
157+
Then the return code should be 0
158+
And STDOUT should contain:
159+
"""
160+
wp_cli_test.sql
161+
"""
162+
And the wp_cli_test.sql file should exist

‎features/db-import.feature‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,25 @@ Feature: Import a WordPress database
134134
Success: Imported from 'debug.sql'.
135135
"""
136136

137+
# Regression test for https://github.com/wp-cli/db-command/issues/218
138+
# The `--ssl` flag used to be silently dropped by `get_mysql_args()` because it
139+
# was missing from the list of allowed MySQL client options, so `wp db import
140+
# --ssl` connected without SSL. Assert the flag is now forwarded to the MySQL
141+
# command (visible in the debug output before the connection is attempted).
142+
# SQLite does not use the MySQL client, hence the tag.
143+
@require-mysql-or-mariadb
144+
Scenario: Import forwards the --ssl flag to the MySQL client
145+
Given a WP install
146+
147+
When I run `wp db export wp_cli_test.sql`
148+
Then the wp_cli_test.sql file should exist
149+
150+
When I try `wp db import wp_cli_test.sql --ssl --debug`
151+
Then STDERR should contain:
152+
"""
153+
--ssl
154+
"""
155+
137156
# For SQLite this would fail at the `wp db create` step
138157
# because of the missing plugin/drop-in.
139158
@require-mysql-or-mariadb
@@ -183,13 +202,13 @@ Feature: Import a WordPress database
183202
Then the wp_cli_test.sql file should exist
184203

185204
When I try `wp db import --defaults --debug`
186-
Then STDERR should match #Debug \(db\): Running shell command: /usr/bin/env (mysql|mariadb) --no-auto-rehash#
205+
Then STDERR should match #Debug \(db\): Running shell command: /([^/]+/)+(mysql|mariadb) --no-auto-rehash#
187206

188207
When I try `wp db import --debug`
189-
Then STDERR should match #Debug \(db\): Running shell command: /usr/bin/env (mysql|mariadb) --no-defaults --no-auto-rehash#
208+
Then STDERR should match #Debug \(db\): Running shell command: /([^/]+/)+(mysql|mariadb) --no-defaults --no-auto-rehash#
190209

191210
When I try `wp db import --no-defaults --debug`
192-
Then STDERR should match #Debug \(db\): Running shell command: /usr/bin/env (mysql|mariadb) --no-defaults --no-auto-rehash#
211+
Then STDERR should match #Debug \(db\): Running shell command: /([^/]+/)+(mysql|mariadb) --no-defaults --no-auto-rehash#
193212

194213
@require-mysql-or-mariadb
195214
Scenario: Import db that has emoji in post

‎features/db-query.feature‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ Feature: Query the database with WordPress' MySQL config
8383
Given a WP install
8484

8585
When I try `wp db query --defaults --debug`
86-
Then STDERR should match #Debug \(db\): Running shell command: /usr/bin/env (mysql|mariadb) --no-auto-rehash#
86+
Then STDERR should match #Debug \(db\): Running shell command: /([^/]+/)+(mysql|mariadb) --no-auto-rehash#
8787

8888
When I try `wp db query --debug`
89-
Then STDERR should match #Debug \(db\): Running shell command: /usr/bin/env (mysql|mariadb) --no-defaults --no-auto-rehash#
89+
Then STDERR should match #Debug \(db\): Running shell command: /([^/]+/)+(mysql|mariadb) --no-defaults --no-auto-rehash#
9090

9191
When I try `wp db query --no-defaults --debug`
92-
Then STDERR should match #Debug \(db\): Running shell command: /usr/bin/env (mysql|mariadb) --no-defaults --no-auto-rehash#
92+
Then STDERR should match #Debug \(db\): Running shell command: /([^/]+/)+(mysql|mariadb) --no-defaults --no-auto-rehash#
9393

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

‎src/DB_Command.php‎

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use WP_CLI\Formatter;
4+
use WP_CLI\Process;
45
use WP_CLI\Utils;
56
use cli\table\Column;
67

@@ -471,8 +472,8 @@ public function cli( $_, $assoc_args ) {
471472
}
472473

473474
$command = sprintf(
474-
'/usr/bin/env %s%s --no-auto-rehash',
475-
$this->get_mysql_command(),
475+
'%s%s --no-auto-rehash',
476+
Utils\get_mysql_binary_path(),
476477
$this->get_defaults_flag_string( $assoc_args )
477478
);
478479
WP_CLI::debug( "Running shell command: {$command}", 'db' );
@@ -611,8 +612,8 @@ public function query( $args, $assoc_args ) {
611612
}
612613

613614
$command = sprintf(
614-
'/usr/bin/env %s%s --no-auto-rehash',
615-
$this->get_mysql_command(),
615+
'%s%s --no-auto-rehash',
616+
Utils\get_mysql_binary_path(),
616617
$this->get_defaults_flag_string( $assoc_args )
617618
);
618619
WP_CLI::debug( "Running shell command: {$command}", 'db' );
@@ -654,7 +655,7 @@ public function query( $args, $assoc_args ) {
654655
* Exports the database to a file or to STDOUT.
655656
*
656657
* Runs `mysqldump` utility using `DB_HOST`, `DB_NAME`, `DB_USER` and
657-
* `DB_PASSWORD` database credentials specified in wp-config.php. Accepts any valid `mysqldump` flags.
658+
* `DB_PASSWORD` database credentials specified in wp-config.php. Accepts any valid [`mysqldump` flags](https://dev.mysql.com/doc/en/mysqldump.html#mysqldump-option-summary).
658659
*
659660
* ## OPTIONS
660661
*
@@ -766,7 +767,7 @@ public function export( $args, $assoc_args ) {
766767

767768
$mysqldump_binary = Utils\force_env_on_nix_systems( Utils\get_sql_dump_command() );
768769

769-
$support_column_statistics = exec( $mysqldump_binary . ' --help | grep "column-statistics"' );
770+
$support_column_statistics = $this->command_supports_option( $mysqldump_binary, 'column-statistics' );
770771

771772
/*
772773
* In case that `--default-character-set` is not given and `DB_CHARSET` is `utf8`,
@@ -855,7 +856,7 @@ private function get_posts_table_charset( $assoc_args ) {
855856
list( $stdout, $stderr, $exit_code ) = self::run(
856857
sprintf(
857858
'%s%s --no-auto-rehash --batch --skip-column-names',
858-
$this->get_mysql_command(),
859+
Utils\get_mysql_binary_path(),
859860
$this->get_defaults_flag_string( $assoc_args )
860861
),
861862
[ 'execute' => $query ],
@@ -878,6 +879,19 @@ private function get_posts_table_charset( $assoc_args ) {
878879
return $stdout;
879880
}
880881

882+
/**
883+
* Check whether a shell command advertises support for a specific option in `--help`.
884+
*
885+
* @param string $command Base shell command to inspect.
886+
* @param string $option Option name to look for.
887+
* @return bool Whether the option is listed in help output.
888+
*/
889+
private function command_supports_option( $command, $option ) {
890+
$result = Process::create( "{$command} --help" )->run();
891+
892+
return false !== strpos( $result->stdout . $result->stderr, $option );
893+
}
894+
881895
/**
882896
* Imports a database from a file or from STDIN.
883897
*
@@ -974,8 +988,8 @@ public function import( $args, $assoc_args ) {
974988
}
975989

976990
$command = sprintf(
977-
'/usr/bin/env %s%s --no-auto-rehash',
978-
$this->get_mysql_command(),
991+
'%s%s --no-auto-rehash',
992+
Utils\get_mysql_binary_path(),
979993
$this->get_defaults_flag_string( $assoc_args )
980994
);
981995
WP_CLI::debug( "Running shell command: {$command}", 'db' );
@@ -1986,7 +2000,7 @@ protected function run_query( $query, $assoc_args = [] ) {
19862000
self::run(
19872001
sprintf(
19882002
'%s%s --no-auto-rehash',
1989-
$this->get_mysql_command(),
2003+
Utils\get_mysql_binary_path(),
19902004
$this->get_defaults_flag_string( $assoc_args )
19912005
),
19922006
array_merge( [ 'execute' => $query ], $mysql_args )
@@ -2037,6 +2051,29 @@ private static function run( $cmd, $assoc_args = [], $send_to_shell = true, $int
20372051

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

2054+
// Filter out empty string values to avoid passing empty parameters to MySQL commands
2055+
// which can cause errors like "Character set '' is not a compiled character set".
2056+
// However, keep empty strings for credential options like 'user' and 'pass' so that
2057+
// an explicitly empty value is not silently converted into an omitted parameter.
2058+
$final_args = array_filter(
2059+
$final_args,
2060+
static function ( $value, $key ) {
2061+
// Always drop null values.
2062+
if ( null === $value ) {
2063+
return false;
2064+
}
2065+
2066+
// Preserve explicitly empty credential arguments.
2067+
if ( '' === $value && in_array( $key, [ 'user', 'pass' ], true ) ) {
2068+
return true;
2069+
}
2070+
2071+
// For all other options, filter out empty strings.
2072+
return '' !== $value;
2073+
},
2074+
ARRAY_FILTER_USE_BOTH
2075+
);
2076+
20402077
// Adapt ordering of arguments.
20412078
uksort(
20422079
$final_args,
@@ -2255,7 +2292,9 @@ private static function get_mysql_args( $assoc_args ) {
22552292
'skip-named-commands',
22562293
'skip-pager',
22572294
'skip-reconnect',
2295+
'skip-ssl',
22582296
'socket',
2297+
'ssl',
22592298
'ssl-ca',
22602299
'ssl-capath',
22612300
'ssl-cert',
@@ -2384,7 +2423,7 @@ protected function get_current_sql_modes( $assoc_args ) {
23842423
list( $stdout, $stderr, $exit_code ) = self::run(
23852424
sprintf(
23862425
'%s%s --no-auto-rehash --batch --skip-column-names',
2387-
$this->get_mysql_command(),
2426+
Utils\get_mysql_binary_path(),
23882427
$this->get_defaults_flag_string( $assoc_args )
23892428
),
23902429
array_merge( $args, [ 'execute' => 'SELECT @@SESSION.sql_mode' ] ),
@@ -2413,15 +2452,6 @@ protected function get_current_sql_modes( $assoc_args ) {
24132452
return $modes;
24142453
}
24152454

2416-
/**
2417-
* Returns the correct `mysql` command based on the detected database type.
2418-
*
2419-
* @return string The appropriate check command.
2420-
*/
2421-
private function get_mysql_command() {
2422-
return 'mariadb' === Utils\get_db_type() ? 'mariadb' : 'mysql';
2423-
}
2424-
24252455
/**
24262456
* Check if the mysql or mariadb binary is available.
24272457
*

0 commit comments

Comments
 (0)