Skip to content

Commit c7b4fd8

Browse files
committed
Compose SQL-mode compat with caller init commands
Applying the compatibility statement via --init-command collided with init commands the caller already had: an explicit --init-command made the compat step drop out, and an init command from an option file (--defaults) was silently overridden by the CLI value. Prepend the compatibility statement to the executed batch instead, for both file imports and run_query(), so it composes with any caller init command. STDIN imports have no executed batch, so they keep using --init-command, which is what lets them get compatibility at all.
1 parent c94eb3c commit c7b4fd8

2 files changed

Lines changed: 53 additions & 25 deletions

File tree

‎features/db-import.feature‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,10 @@ Feature: Import a WordPress database
256256
When I run `wp db export wp_cli_test.sql`
257257
Then the wp_cli_test.sql file should exist
258258

259-
# The WordPress-compatibility mode adaptation is applied via the MySQL client's
260-
# --init-command on the same connection, so it is visible in the debug output
261-
# and no separate mode probe runs (which is what used to break with custom
262-
# connection options).
259+
# 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).
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:
@@ -339,3 +339,21 @@ Feature: Import a WordPress database
339339
"""
340340
1
341341
"""
342+
343+
# 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
345+
# caller's own init command still runs and the zero-date import still succeeds.
346+
@require-mysql-or-mariadb
347+
Scenario: `wp db import` keeps SQL-mode compatibility when the caller sets --init-command
348+
Given a WP install
349+
And a zerodate_compose.sql file:
350+
"""
351+
CREATE TABLE wp_cli_zd_compose (id int NOT NULL, d datetime NOT NULL DEFAULT '0000-00-00 00:00:00');
352+
INSERT INTO wp_cli_zd_compose (id, d) VALUES (1, '0000-00-00 00:00:00');
353+
"""
354+
355+
When I run `wp db import zerodate_compose.sql --init-command="SET @x = 1"`
356+
Then STDOUT should contain:
357+
"""
358+
Success: Imported from 'zerodate_compose.sql'.
359+
"""

‎src/DB_Command.php‎

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -928,13 +928,12 @@ public function import( $args, $assoc_args ) {
928928
self::get_mysql_args( $assoc_args )
929929
);
930930

931-
// Adapt the session SQL mode to be WordPress-compatible via --init-command,
932-
// so it runs on connect before any SQL is read. This covers both file and
933-
// STDIN imports, and needs no separate probe connection.
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.
934936
$sql_mode_compat = $this->get_sql_mode_compat_statement( $assoc_args );
935-
if ( '' !== $sql_mode_compat && ! isset( $mysql_args['init-command'] ) ) {
936-
$mysql_args['init-command'] = $sql_mode_compat;
937-
}
938937

939938
if ( '-' !== $result_file ) {
940939
if ( ! is_readable( $result_file ) ) {
@@ -945,9 +944,17 @@ public function import( $args, $assoc_args ) {
945944
? 'SOURCE %s;'
946945
: 'SET autocommit = 0; SET unique_checks = 0; SET foreign_key_checks = 0; SOURCE %s; COMMIT;';
947946

947+
if ( '' !== $sql_mode_compat ) {
948+
$query = $sql_mode_compat . '; ' . $query;
949+
}
950+
948951
$mysql_args['execute'] = sprintf( $query, $result_file );
949952
} else {
950953
$result_file = 'STDIN';
954+
955+
if ( '' !== $sql_mode_compat && ! isset( $mysql_args['init-command'] ) ) {
956+
$mysql_args['init-command'] = $sql_mode_compat;
957+
}
951958
}
952959

953960
$command = sprintf(
@@ -1932,20 +1939,21 @@ private static function get_create_query() {
19321939
* @param array $assoc_args Optional. Associative array of arguments.
19331940
*/
19341941
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+
19351950
WP_CLI::debug( "Query: {$query}", 'db' );
19361951

19371952
$mysql_args = array_merge(
19381953
self::get_dbuser_dbpass_args( $assoc_args ),
19391954
self::get_mysql_args( $assoc_args )
19401955
);
19411956

1942-
// Adapt the session SQL mode to be WordPress-compatible via --init-command
1943-
// (runs on connect, needs no separate probe connection).
1944-
$sql_mode_compat = $this->get_sql_mode_compat_statement( $assoc_args );
1945-
if ( '' !== $sql_mode_compat && ! isset( $mysql_args['init-command'] ) ) {
1946-
$mysql_args['init-command'] = $sql_mode_compat;
1947-
}
1948-
19491957
self::run(
19501958
sprintf(
19511959
'%s%s --no-auto-rehash',
@@ -2317,20 +2325,22 @@ protected function get_defaults_flag_string( &$assoc_args ) {
23172325
* Get the statement that strips the SQL modes incompatible with WordPress
23182326
* from the current session.
23192327
*
2320-
* This is handed to the MySQL client as `--init-command`, so it runs on the
2321-
* very same connection right after connecting and before any SQL is read.
2322-
* Imports therefore behave like WordPress Core (which disables these modes in
2323-
* `wpdb`), including when the dump is streamed from STDIN. Unlike the previous
2324-
* implementation, no separate connection is opened to first discover the
2325-
* current modes, so it works regardless of the connection options in play
2326-
* (custom `--host`, `--defaults`, SSL/TLS, sockets, ...).
2328+
* The statement is run on the same connection as the import or query -- either
2329+
* prepended to the executed batch, or via `--init-command` when importing from
2330+
* STDIN (where there is no executed batch). Imports therefore behave like
2331+
* WordPress Core (which disables these modes in `wpdb`), including when the
2332+
* dump is streamed from STDIN. Unlike the previous implementation, no separate
2333+
* connection is opened to first discover the current modes, so it works
2334+
* regardless of the connection options in play (custom `--host`, `--defaults`,
2335+
* SSL/TLS, sockets, ...).
23272336
*
23282337
* Returns an empty string when the `--skip-sql-mode-compat` flag is set.
23292338
*
23302339
* @see https://github.com/WordPress/wordpress-develop/blob/5.4.0/src/wp-includes/wp-db.php#L559-L572
23312340
*
23322341
* @param array $assoc_args The associative argument array passed to the command.
2333-
* @return string SQL statement to run via `--init-command`, or an empty string.
2342+
* @return string SQL statement that sets a WordPress-compatible SQL mode, or an
2343+
* empty string.
23342344
*/
23352345
protected function get_sql_mode_compat_statement( $assoc_args = [] ) {
23362346
if ( Utils\get_flag_value( $assoc_args, 'skip-sql-mode-compat', false ) ) {

0 commit comments

Comments
 (0)