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