Skip to content

Commit 2566fc9

Browse files
committed
Apply SQL-mode compatibility via --init-command everywhere
Replace the removed SQL-mode probe with a single mechanism shared by `wp db query`, `wp db import` and the internal `run_query()`: the WordPress-compatible session SQL mode is applied through the MySQL client's `--init-command`, on the same connection, before any SQL runs. This keeps `wp db query` adapting the SQL mode as it did before, so DDL against WordPress's zero-date schema (`ALTER TABLE wp_blogs ...`, `CREATE TABLE ... AS SELECT` from a WordPress table) keeps working on strict servers, while still removing the second probe connection that ignored the caller's connection options and caused #311. The STDIN import path (#171) is covered too, since `--init-command` applies whether or not there is an executed batch. When the caller supplies their own `--init-command`, the compat statement is composed with it into a single multi-statement `--init-command` (compat first) instead of being dropped. `--init-command-add` is not used: it is rejected by the mysql 5.6/5.7/8.0 and mariadb clients, whereas a single multi-statement `--init-command` runs both statements on every CI-targeted client (mysql 5.6/5.7/8.0/8.4, mariadb 11.4). Add `--skip-sql-mode-compat` to `wp db query` as well, so a query can opt into the server's own SQL modes.
1 parent faf472b commit 2566fc9

3 files changed

Lines changed: 96 additions & 50 deletions

File tree

‎features/db-import.feature‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,16 +250,16 @@ Feature: Import a WordPress database
250250

251251
# SQLite does not use the MySQL client and has no concept of SQL modes.
252252
@require-mysql-or-mariadb
253-
Scenario: `wp db import` adapts the SQL mode in-band by default
253+
Scenario: `wp db import` adapts the SQL mode via --init-command by default
254254
Given a WP install
255255

256256
When I run `wp db export wp_cli_test.sql`
257257
Then the wp_cli_test.sql file should exist
258258

259259
# The WordPress-compatibility mode adaptation runs on the same import
260-
# connection (prepended to the executed statement for a file import), so it is
261-
# visible in the debug output and no separate mode probe runs (which is what
262-
# used to break with custom connection options).
260+
# connection via --init-command, so it is visible in the debug output and no
261+
# separate mode probe runs (which is what used to break with custom connection
262+
# options).
263263
When I try `wp db import wp_cli_test.sql --debug`
264264
Then the return code should be 0
265265
And STDERR should contain:
@@ -341,7 +341,8 @@ Feature: Import a WordPress database
341341
"""
342342
343343
# The compatibility statement must compose with a caller-supplied --init-command
344-
# rather than replace it. A file import prepends it to the executed batch, so the
344+
# rather than replace it. Both are sent as a single multi-statement
345+
# --init-command (compatibility statement first, caller's second), so the
345346
# caller's own init command still runs and the zero-date import still succeeds.
346347
@require-mysql-or-mariadb
347348
Scenario: `wp db import` keeps SQL-mode compatibility when the caller sets --init-command

‎features/db-query.feature‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,30 +91,44 @@ Feature: Query the database with WordPress' MySQL config
9191
When I try `wp db query --no-defaults --debug`
9292
Then STDERR should match #Debug \(db\): Running shell command: /([^/]+/)+(mysql|mariadb) --no-defaults --no-auto-rehash#
9393

94-
Scenario: `wp db query` runs under the server's own SQL modes without a separate mode probe
94+
# `wp db query` adapts the session SQL mode to be WordPress-compatible the same
95+
# way `wp db import` does: via --init-command on the query's own connection, with
96+
# no separate mode probe. This keeps statements against WordPress's zero-date
97+
# schema (e.g. `ALTER TABLE wp_blogs ...`, `CREATE TABLE ... AS SELECT` from a
98+
# WordPress table) working on servers whose default SQL mode is strict.
99+
@require-mysql-or-mariadb
100+
Scenario: `wp db query` adapts the SQL mode by default without a separate mode probe
95101
Given a WP install
96102

97-
# The previous implementation always opened a second `SELECT @@SESSION.sql_mode`
98-
# connection to decide whether to rewrite the query, then prepended a
99-
# `SET SESSION sql_mode=...` statement. Both are gone: `wp db query` now behaves
100-
# like the `mysql` client and runs under the server's own SQL modes.
101103
When I try `wp db query 'SELECT 1;' --debug`
102104
Then the return code should be 0
105+
And STDERR should contain:
106+
"""
107+
SET SESSION sql_mode
108+
"""
103109
And STDERR should not contain:
104110
"""
105-
@@SESSION.sql_mode
111+
Failed to get current SQL modes
106112
"""
113+
114+
@require-mysql-or-mariadb
115+
Scenario: `wp db query --skip-sql-mode-compat` runs under the server's own SQL modes
116+
Given a WP install
117+
118+
When I try `wp db query 'SELECT 1;' --skip-sql-mode-compat --debug`
119+
Then the return code should be 0
107120
And STDERR should not contain:
108121
"""
109122
SET SESSION sql_mode
110123
"""
111124

112125
# Regression test for https://github.com/wp-cli/db-command/issues/311
113126
# Passing connection options alongside an inline query used to fail, because the
114-
# SQL-mode probe opened a *second* connection that ignored those very options
127+
# old SQL-mode probe opened a *second* connection that ignored those very options
115128
# (custom --host, --defaults, SSL/TLS, sockets, ...) and then aborted the whole
116-
# command with "Failed to get current SQL modes". With the probe removed, the
117-
# inline query runs directly under the given options.
129+
# command with "Failed to get current SQL modes". The probe is gone -- the
130+
# compatibility mode is now applied via --init-command on the query's own
131+
# connection -- so the inline query runs directly under the given options.
118132
Scenario: `wp db query` with an inline query and connection options does not trigger a failing mode probe
119133
Given a WP install
120134

@@ -123,7 +137,3 @@ Feature: Query the database with WordPress' MySQL config
123137
"""
124138
Failed to get current SQL modes
125139
"""
126-
And STDERR should not contain:
127-
"""
128-
@@SESSION.sql_mode
129-
"""

‎src/DB_Command.php‎

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,13 @@ public function cli( $_, $assoc_args ) {
513513
* [--defaults]
514514
* : Loads the environment's MySQL option files. Default behavior is to skip loading them to avoid failures due to misconfiguration.
515515
*
516+
* [--skip-sql-mode-compat]
517+
* : Run the query under the server's own SQL modes instead of adapting them to
518+
* be WordPress-compatible. By default, the incompatible modes that WordPress
519+
* disables in `wpdb` (such as `NO_ZERO_DATE` and `STRICT_TRANS_TABLES`) are
520+
* stripped for the session, so statements against WordPress's zero-date schema
521+
* behave the same as they do in WordPress itself.
522+
*
516523
* ## EXAMPLES
517524
*
518525
* # Execute a query stored in a file
@@ -606,8 +613,14 @@ public function query( $args, $assoc_args ) {
606613
$assoc_args['execute'] = $args[0];
607614
}
608615

609-
// Note: `wp db query` intentionally runs under the server's own SQL modes,
610-
// like the `mysql` client itself. No SQL-mode adaptation is applied here.
616+
// Adapt the session SQL mode to be WordPress-compatible via --init-command,
617+
// so it runs on connect before the query -- exactly like `wp db import` and
618+
// WordPress Core's own wpdb -- and composes with any caller-provided
619+
// --init-command. No separate probe connection is needed. Pass
620+
// --skip-sql-mode-compat to run under the server's own SQL modes instead.
621+
$this->apply_sql_mode_compat_init_command( $assoc_args, $assoc_args );
622+
unset( $assoc_args['skip-sql-mode-compat'] );
623+
611624
$is_row_modifying_query = isset( $assoc_args['execute'] ) && preg_match( '/\b(UPDATE|DELETE|INSERT|REPLACE(?!\s*\()|LOAD DATA)\b/i', $assoc_args['execute'] );
612625

613626
if ( $is_row_modifying_query ) {
@@ -928,12 +941,11 @@ public function import( $args, $assoc_args ) {
928941
self::get_mysql_args( $assoc_args )
929942
);
930943

931-
// Adapt the session SQL mode to be WordPress-compatible. When there is an
932-
// --execute batch (importing from a file), prepend it there so it composes
933-
// with any init command the caller already set (via --init-command or an
934-
// option file). When importing from STDIN there is no --execute, so apply
935-
// it via --init-command instead. Either way, no separate probe connection.
936-
$sql_mode_compat = $this->get_sql_mode_compat_statement( $assoc_args );
944+
// Adapt the session SQL mode to be WordPress-compatible via --init-command,
945+
// so it runs on connect before any SQL is read. This covers both file and
946+
// STDIN imports, composes with any caller-provided --init-command, and needs
947+
// no separate probe connection.
948+
$this->apply_sql_mode_compat_init_command( $mysql_args, $assoc_args );
937949

938950
if ( '-' !== $result_file ) {
939951
if ( ! is_readable( $result_file ) ) {
@@ -944,17 +956,9 @@ public function import( $args, $assoc_args ) {
944956
? 'SOURCE %s;'
945957
: 'SET autocommit = 0; SET unique_checks = 0; SET foreign_key_checks = 0; SOURCE %s; COMMIT;';
946958

947-
if ( '' !== $sql_mode_compat ) {
948-
$query = $sql_mode_compat . '; ' . $query;
949-
}
950-
951959
$mysql_args['execute'] = sprintf( $query, $result_file );
952960
} else {
953961
$result_file = 'STDIN';
954-
955-
if ( '' !== $sql_mode_compat && ! isset( $mysql_args['init-command'] ) ) {
956-
$mysql_args['init-command'] = $sql_mode_compat;
957-
}
958962
}
959963

960964
$command = sprintf(
@@ -1939,21 +1943,18 @@ private static function get_create_query() {
19391943
* @param array $assoc_args Optional. Associative array of arguments.
19401944
*/
19411945
protected function run_query( $query, $assoc_args = [] ) {
1942-
// Adapt the session SQL mode to be WordPress-compatible by prepending it to
1943-
// the executed batch, so it composes with any init command the caller set
1944-
// and needs no separate probe connection.
1945-
$sql_mode_compat = $this->get_sql_mode_compat_statement( $assoc_args );
1946-
if ( '' !== $sql_mode_compat ) {
1947-
$query = $sql_mode_compat . '; ' . $query;
1948-
}
1949-
19501946
WP_CLI::debug( "Query: {$query}", 'db' );
19511947

19521948
$mysql_args = array_merge(
19531949
self::get_dbuser_dbpass_args( $assoc_args ),
19541950
self::get_mysql_args( $assoc_args )
19551951
);
19561952

1953+
// Adapt the session SQL mode to be WordPress-compatible via --init-command,
1954+
// so it runs on connect before the query, composes with any caller-provided
1955+
// --init-command, and needs no separate probe connection.
1956+
$this->apply_sql_mode_compat_init_command( $mysql_args, $assoc_args );
1957+
19571958
self::run(
19581959
sprintf(
19591960
'%s%s --no-auto-rehash',
@@ -2312,14 +2313,14 @@ protected function get_defaults_flag_string( &$assoc_args ) {
23122313
* Get the statement that strips the SQL modes incompatible with WordPress
23132314
* from the current session.
23142315
*
2315-
* The statement is run on the same connection as the import or query -- either
2316-
* prepended to the executed batch, or via `--init-command` when importing from
2317-
* STDIN (where there is no executed batch). Imports therefore behave like
2318-
* WordPress Core (which disables these modes in `wpdb`), including when the
2319-
* dump is streamed from STDIN. Unlike the previous implementation, no separate
2320-
* connection is opened to first discover the current modes, so it works
2321-
* regardless of the connection options in play (custom `--host`, `--defaults`,
2322-
* SSL/TLS, sockets, ...).
2316+
* The statement is applied on the same connection as the import or query via
2317+
* the MySQL client's `--init-command`, so it runs on connect before any SQL is
2318+
* read. Imports and queries therefore behave like WordPress Core (which
2319+
* disables these modes in `wpdb`), including when a dump is streamed from
2320+
* STDIN. Unlike the previous implementation, no separate connection is opened
2321+
* to first discover the current modes, so it works regardless of the
2322+
* connection options in play (custom `--host`, `--defaults`, SSL/TLS,
2323+
* sockets, ...).
23232324
*
23242325
* Returns an empty string when the `--skip-sql-mode-compat` flag is set.
23252326
*
@@ -2345,4 +2346,38 @@ protected function get_sql_mode_compat_statement( $assoc_args = [] ) {
23452346

23462347
return sprintf( "SET SESSION sql_mode = TRIM( BOTH ',' FROM %s )", $expression );
23472348
}
2349+
2350+
/**
2351+
* Applies the WordPress SQL-mode compatibility statement to the MySQL client
2352+
* arguments via `--init-command`, so it runs on connect before any SQL is read.
2353+
*
2354+
* If the caller already supplied their own `--init-command` (via the CLI or an
2355+
* option file), the compatibility statement is composed with it as a single
2356+
* multi-statement `--init-command` value -- the compatibility statement first,
2357+
* the caller's command second -- rather than dropping either one. A caller that
2358+
* re-sets `sql_mode` in their own command therefore still wins (last statement
2359+
* takes effect), while the compatibility statement is guaranteed to run.
2360+
*
2361+
* Running both statements from a single `--init-command` value works across
2362+
* every targeted client (mysql 5.6/5.7/8.0/8.4 and mariadb). The newer
2363+
* `--init-command-add` option is only available on mysql 8.4+, so it is not
2364+
* used here.
2365+
*
2366+
* Does nothing when compatibility is disabled via `--skip-sql-mode-compat`.
2367+
*
2368+
* @param array $mysql_args MySQL client arguments, passed by reference.
2369+
* @param array $assoc_args The associative argument array passed to the command.
2370+
*/
2371+
protected function apply_sql_mode_compat_init_command( &$mysql_args, $assoc_args ) {
2372+
$sql_mode_compat = $this->get_sql_mode_compat_statement( $assoc_args );
2373+
if ( '' === $sql_mode_compat ) {
2374+
return;
2375+
}
2376+
2377+
if ( isset( $mysql_args['init-command'] ) && '' !== trim( (string) $mysql_args['init-command'] ) ) {
2378+
$mysql_args['init-command'] = $sql_mode_compat . '; ' . $mysql_args['init-command'];
2379+
} else {
2380+
$mysql_args['init-command'] = $sql_mode_compat;
2381+
}
2382+
}
23482383
}

0 commit comments

Comments
 (0)