@@ -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