Skip to content

Commit 6051f7f

Browse files
committed
Merge branch 'main' into copilot/fix-wp-db-query-fallback
2 parents ec70aa9 + 4f7635d commit 6051f7f

6 files changed

Lines changed: 277 additions & 113 deletions

File tree

‎.github/workflows/copilot-setup-steps.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121

2222
steps:
2323
- name: Checkout code
24-
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
24+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
2525
with:
2626
persist-credentials: false
2727

‎README.md‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ wp db cli [--database=<database>] [--default-character-set=<character-set>] [--d
338338
Executes a SQL query against the database.
339339

340340
~~~
341-
wp db query [<sql>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--defaults]
341+
wp db query [<sql>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--defaults] [--skip-sql-mode-compat]
342342
~~~
343343

344344
Executes an arbitrary SQL query using `DB_HOST`, `DB_NAME`, `DB_USER`
@@ -365,6 +365,13 @@ entirely.
365365
[--defaults]
366366
Loads the environment's MySQL option files. Default behavior is to skip loading them to avoid failures due to misconfiguration.
367367

368+
[--skip-sql-mode-compat]
369+
Run the query under the server's own SQL modes instead of adapting them to
370+
be WordPress-compatible. By default, the incompatible modes that WordPress
371+
disables in `wpdb` (such as `NO_ZERO_DATE` and `STRICT_TRANS_TABLES`) are
372+
stripped for the session, so statements against WordPress's zero-date schema
373+
behave the same as they do in WordPress itself.
374+
368375
**EXAMPLES**
369376

370377
# Execute a query stored in a file
@@ -523,7 +530,7 @@ Runs `mysqldump` utility using `DB_HOST`, `DB_NAME`, `DB_USER` and
523530
Imports a database from a file or from STDIN.
524531

525532
~~~
526-
wp db import [<file>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--skip-optimization] [--defaults]
533+
wp db import [<file>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--skip-optimization] [--skip-sql-mode-compat] [--defaults]
527534
~~~
528535

529536
Runs SQL queries using `DB_HOST`, `DB_NAME`, `DB_USER` and
@@ -548,6 +555,9 @@ defined in the SQL.
548555
[--skip-optimization]
549556
When using an SQL file, do not include speed optimization such as disabling auto-commit and key checks.
550557

558+
[--skip-sql-mode-compat]
559+
Do not adapt the session SQL mode for WordPress compatibility. By default, `wp db import` strips the SQL modes that WordPress Core disables (such as `STRICT_TRANS_TABLES` and `NO_ZERO_DATE`) so that dumps containing legacy values like `0000-00-00` import cleanly. Pass this flag to import under the server's own SQL modes instead.
560+
551561
[--defaults]
552562
Loads the environment's MySQL option files. Default behavior is to skip loading them to avoid failures due to misconfiguration.
553563

‎features/db-import.feature‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,114 @@ Feature: Import a WordPress database
275275
"""
276276
🍣
277277
"""
278+
279+
# SQLite does not use the MySQL client and has no concept of SQL modes.
280+
@require-mysql-or-mariadb
281+
Scenario: `wp db import` adapts the SQL mode via --init-command by default
282+
Given a WP install
283+
284+
When I run `wp db export wp_cli_test.sql`
285+
Then the wp_cli_test.sql file should exist
286+
287+
# The WordPress-compatibility mode adaptation runs on the same import
288+
# connection via --init-command, so it is visible in the debug output and no
289+
# separate mode probe runs (which is what used to break with custom connection
290+
# options).
291+
When I try `wp db import wp_cli_test.sql --debug`
292+
Then the return code should be 0
293+
And STDERR should contain:
294+
"""
295+
SET SESSION sql_mode
296+
"""
297+
And STDERR should not contain:
298+
"""
299+
Failed to get current SQL modes
300+
"""
301+
302+
@require-mysql-or-mariadb
303+
Scenario: `wp db import --skip-sql-mode-compat` imports under the server's own SQL modes
304+
Given a WP install
305+
306+
When I run `wp db export wp_cli_test.sql`
307+
Then the wp_cli_test.sql file should exist
308+
309+
When I try `wp db import wp_cli_test.sql --skip-sql-mode-compat --debug`
310+
Then the return code should be 0
311+
And STDERR should not contain:
312+
"""
313+
SET SESSION sql_mode
314+
"""
315+
316+
# Regression test for the WordPress-compatibility behavior. WordPress schema
317+
# declares datetime columns as `DEFAULT '0000-00-00 00:00:00'`, so real dumps
318+
# carry zero-date values. On servers whose default SQL mode includes
319+
# NO_ZERO_DATE/STRICT_TRANS_TABLES (MySQL 5.7+/8.0), a raw dump without its own
320+
# SQL_MODE header would fail to import with "Invalid default value". `wp db
321+
# import` must strip those modes for the session so the import succeeds.
322+
@require-mysql-or-mariadb
323+
Scenario: `wp db import` loads a dump containing legacy zero-date values
324+
Given a WP install
325+
And a zerodate.sql file:
326+
"""
327+
CREATE TABLE `wp_cli_zerodate` (
328+
`id` int NOT NULL,
329+
`d` datetime NOT NULL DEFAULT '0000-00-00 00:00:00'
330+
);
331+
INSERT INTO `wp_cli_zerodate` (`id`, `d`) VALUES (1, '0000-00-00 00:00:00');
332+
"""
333+
334+
When I run `wp db import zerodate.sql`
335+
Then STDOUT should contain:
336+
"""
337+
Success: Imported from 'zerodate.sql'.
338+
"""
339+
340+
When I run `wp db query 'SELECT COUNT(*) FROM wp_cli_zerodate;' --skip-column-names`
341+
Then STDOUT should contain:
342+
"""
343+
1
344+
"""
345+
346+
# Regression test for https://github.com/wp-cli/db-command/issues/171
347+
# A dump streamed from STDIN must get the same WordPress SQL-mode compatibility
348+
# as a file import. This is now handled via --init-command, which applies on the
349+
# STDIN connection too (the previous prepend only covered file imports).
350+
@require-mysql-or-mariadb
351+
Scenario: `wp db import -` from STDIN loads a dump containing legacy zero-date values
352+
Given a WP install
353+
And a zerodate_stdin.sql file:
354+
"""
355+
CREATE TABLE wp_cli_zerodate_stdin (id int NOT NULL, d datetime NOT NULL DEFAULT '0000-00-00 00:00:00');
356+
INSERT INTO wp_cli_zerodate_stdin (id, d) VALUES (1, '0000-00-00 00:00:00');
357+
"""
358+
359+
When I run `wp db import - < zerodate_stdin.sql`
360+
Then STDOUT should contain:
361+
"""
362+
Success: Imported from 'STDIN'.
363+
"""
364+
365+
When I run `wp db query 'SELECT COUNT(*) FROM wp_cli_zerodate_stdin;' --skip-column-names`
366+
Then STDOUT should contain:
367+
"""
368+
1
369+
"""
370+
371+
# The compatibility statement must compose with a caller-supplied --init-command
372+
# rather than replace it. Both are sent as a single multi-statement
373+
# --init-command (compatibility statement first, caller's second), so the
374+
# caller's own init command still runs and the zero-date import still succeeds.
375+
@require-mysql-or-mariadb
376+
Scenario: `wp db import` keeps SQL-mode compatibility when the caller sets --init-command
377+
Given a WP install
378+
And a zerodate_compose.sql file:
379+
"""
380+
CREATE TABLE wp_cli_zd_compose (id int NOT NULL, d datetime NOT NULL DEFAULT '0000-00-00 00:00:00');
381+
INSERT INTO wp_cli_zd_compose (id, d) VALUES (1, '0000-00-00 00:00:00');
382+
"""
383+
384+
When I run `wp db import zerodate_compose.sql --init-command="SET @x = 1"`
385+
Then STDOUT should contain:
386+
"""
387+
Success: Imported from 'zerodate_compose.sql'.
388+
"""

‎features/db-query.feature‎

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,33 +91,69 @@ 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: SQL modes do not include any of the modes incompatible with WordPress
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-
When I try `wp db query 'SELECT @@SESSION.sql_mode;' --debug`
98-
Then STDOUT should not contain:
99-
"""
100-
NO_ZERO_DATE
101-
"""
102-
And STDOUT should not contain:
103+
When I try `wp db query 'SELECT 1;' --debug`
104+
Then the return code should be 0
105+
And STDERR should contain:
103106
"""
104-
ONLY_FULL_GROUP_BY
107+
SET SESSION sql_mode
105108
"""
106-
And STDOUT should not contain:
109+
And STDERR should not contain:
107110
"""
108-
STRICT_TRANS_TABLES
111+
Failed to get current SQL modes
109112
"""
110-
And STDOUT should not contain:
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
120+
And STDERR should not contain:
111121
"""
112-
STRICT_ALL_TABLES
122+
SET SESSION sql_mode
113123
"""
114-
And STDOUT should not contain:
124+
125+
# Regression test for https://github.com/wp-cli/db-command/issues/311
126+
# Passing connection options alongside an inline query used to fail, because the
127+
# old SQL-mode probe opened a *second* connection that ignored those very options
128+
# (custom --host, --defaults, SSL/TLS, sockets, ...) and then aborted the whole
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.
132+
Scenario: `wp db query` with an inline query and connection options does not trigger a failing mode probe
133+
Given a WP install
134+
135+
When I try `wp db query 'SELECT 1;' --defaults --debug`
136+
Then STDERR should not contain:
115137
"""
116-
TRADITIONAL
138+
Failed to get current SQL modes
117139
"""
118-
And STDOUT should not contain:
140+
141+
# Regression test for https://github.com/wp-cli/db-command/issues/309
142+
# MariaDB 11.4+ verifies the server certificate by default and prints a warning to
143+
# STDERR (or fails) against the auto-generated self-signed certificate, which broke
144+
# the tests. `run()` now opts out via `--skip-ssl-verify-server-cert` on MariaDB, and
145+
# both `ssl-verify-server-cert` and its `skip-` variant are allowed so the behaviour
146+
# can be overridden. Assert the flag is forwarded to the MySQL client (visible in the
147+
# debug output before the connection is attempted). SQLite does not use the MySQL
148+
# client, hence the tag.
149+
@require-mysql-or-mariadb
150+
Scenario: Query forwards the ssl-verify-server-cert flags to the MySQL client
151+
Given a WP install
152+
153+
When I try `wp db query "SELECT 1" --skip-ssl-verify-server-cert --debug`
154+
Then STDERR should contain:
119155
"""
120-
ANSI
156+
skip-ssl-verify-server-cert
121157
"""
122158

123159
@require-mysql-or-mariadb

‎features/db.feature‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,18 @@ Feature: Perform database operations
181181
"""
182182
And STDOUT should be empty
183183
184-
# Verbose option prints to STDERR.
185-
When I try `wp db repair --verbose`
184+
# `--verbose` makes the check command print progress information. MySQL's
185+
# mysqlcheck writes "# Connecting to ..." to STDERR at a single --verbose,
186+
# while MariaDB's mariadb-check writes "Processing databases" to STDOUT and
187+
# only prints connection information at a higher verbosity. Merge the streams
188+
# and accept either client's marker.
189+
When I try `wp db repair --verbose 2>&1`
186190
Then the return code should be 0
187-
And STDERR should contain:
191+
And STDOUT should match /(?:# Connecting to |Processing databases)/
192+
And STDOUT should contain:
188193
"""
189-
Connecting
194+
Success: Database repaired.
190195
"""
191-
And STDOUT should not be empty
192196

193197
@skip-sqlite
194198
Scenario: db repair with --quiet flag should only show errors

0 commit comments

Comments
 (0)