@@ -247,3 +247,114 @@ Feature: Import a WordPress database
247247 """
248248 🍣
249249 """
250+
251+ # SQLite does not use the MySQL client and has no concept of SQL modes.
252+ @require-mysql-or-mariadb
253+ Scenario : `wp db import` adapts the SQL mode via --init-command by default
254+ Given a WP install
255+
256+ When I run `wp db export wp_cli_test.sql`
257+ Then the wp_cli_test.sql file should exist
258+
259+ # The WordPress-compatibility mode adaptation runs on the same import
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).
263+ When I try `wp db import wp_cli_test.sql --debug`
264+ Then the return code should be 0
265+ And STDERR should contain:
266+ """
267+ SET SESSION sql_mode
268+ """
269+ And STDERR should not contain:
270+ """
271+ Failed to get current SQL modes
272+ """
273+
274+ @require-mysql-or-mariadb
275+ Scenario : `wp db import --skip-sql-mode-compat` imports under the server's own SQL modes
276+ Given a WP install
277+
278+ When I run `wp db export wp_cli_test.sql`
279+ Then the wp_cli_test.sql file should exist
280+
281+ When I try `wp db import wp_cli_test.sql --skip-sql-mode-compat --debug`
282+ Then the return code should be 0
283+ And STDERR should not contain:
284+ """
285+ SET SESSION sql_mode
286+ """
287+
288+ # Regression test for the WordPress-compatibility behavior. WordPress schema
289+ # declares datetime columns as `DEFAULT '0000-00-00 00:00:00'`, so real dumps
290+ # carry zero-date values. On servers whose default SQL mode includes
291+ # NO_ZERO_DATE/STRICT_TRANS_TABLES (MySQL 5.7+/8.0), a raw dump without its own
292+ # SQL_MODE header would fail to import with "Invalid default value". `wp db
293+ # import` must strip those modes for the session so the import succeeds.
294+ @require-mysql-or-mariadb
295+ Scenario : `wp db import` loads a dump containing legacy zero-date values
296+ Given a WP install
297+ And a zerodate.sql file:
298+ """
299+ CREATE TABLE `wp_cli_zerodate` (
300+ `id` int NOT NULL,
301+ `d` datetime NOT NULL DEFAULT '0000-00-00 00:00:00'
302+ );
303+ INSERT INTO `wp_cli_zerodate` (`id`, `d`) VALUES (1, '0000-00-00 00:00:00');
304+ """
305+
306+ When I run `wp db import zerodate.sql`
307+ Then STDOUT should contain:
308+ """
309+ Success: Imported from 'zerodate.sql'.
310+ """
311+
312+ When I run `wp db query 'SELECT COUNT(*) FROM wp_cli_zerodate;' --skip-column-names`
313+ Then STDOUT should contain:
314+ """
315+ 1
316+ """
317+
318+ # Regression test for https://github.com/wp-cli/db-command/issues/171
319+ # A dump streamed from STDIN must get the same WordPress SQL-mode compatibility
320+ # as a file import. This is now handled via --init-command, which applies on the
321+ # STDIN connection too (the previous prepend only covered file imports).
322+ @require-mysql-or-mariadb
323+ Scenario : `wp db import -` from STDIN loads a dump containing legacy zero-date values
324+ Given a WP install
325+ And a zerodate_stdin.sql file:
326+ """
327+ CREATE TABLE wp_cli_zerodate_stdin (id int NOT NULL, d datetime NOT NULL DEFAULT '0000-00-00 00:00:00');
328+ INSERT INTO wp_cli_zerodate_stdin (id, d) VALUES (1, '0000-00-00 00:00:00');
329+ """
330+
331+ When I run `wp db import - < zerodate_stdin.sql`
332+ Then STDOUT should contain:
333+ """
334+ Success: Imported from 'STDIN'.
335+ """
336+
337+ When I run `wp db query 'SELECT COUNT(*) FROM wp_cli_zerodate_stdin;' --skip-column-names`
338+ Then STDOUT should contain:
339+ """
340+ 1
341+ """
342+
343+ # The compatibility statement must compose with a caller-supplied --init-command
344+ # rather than replace it. Both are sent as a single multi-statement
345+ # --init-command (compatibility statement first, caller's second), so the
346+ # caller's own init command still runs and the zero-date import still succeeds.
347+ @require-mysql-or-mariadb
348+ Scenario: `wp db import` keeps SQL-mode compatibility when the caller sets --init-command
349+ Given a WP install
350+ And a zerodate_compose.sql file:
351+ """
352+ CREATE TABLE wp_cli_zd_compose (id int NOT NULL, d datetime NOT NULL DEFAULT '0000-00-00 00:00:00');
353+ INSERT INTO wp_cli_zd_compose (id, d) VALUES (1, '0000-00-00 00:00:00');
354+ """
355+
356+ When I run `wp db import zerodate_compose.sql --init-command="SET @x = 1"`
357+ Then STDOUT should contain:
358+ """
359+ Success: Imported from 'zerodate_compose.sql'.
360+ """
0 commit comments