Skip to content

Commit 7f759b0

Browse files
Copilotswissspidy
andauthored
Add wpdb fallback for wp db query and wp db import when mysql/mariadb binary is unavailable (#320)
Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
1 parent 07cbfce commit 7f759b0

4 files changed

Lines changed: 807 additions & 18 deletions

File tree

‎README.md‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ wp db create [--dbuser=<value>] [--dbpass=<value>] [--defaults]
8181

8282
Runs `CREATE_DATABASE` SQL statement using `DB_HOST`, `DB_NAME`,
8383
`DB_USER` and `DB_PASSWORD` database credentials specified in
84-
wp-config.php.
84+
wp-config.php. Requires MySQL/MariaDB client binaries to be available.
8585

8686
**OPTIONS**
8787

@@ -111,7 +111,7 @@ wp db drop [--dbuser=<value>] [--dbpass=<value>] [--yes] [--defaults]
111111

112112
Runs `DROP_DATABASE` SQL statement using `DB_HOST`, `DB_NAME`,
113113
`DB_USER` and `DB_PASSWORD` database credentials specified in
114-
wp-config.php.
114+
wp-config.php. Requires MySQL/MariaDB client binaries to be available.
115115

116116
**OPTIONS**
117117

@@ -144,7 +144,7 @@ wp db reset [--dbuser=<value>] [--dbpass=<value>] [--yes] [--defaults]
144144

145145
Runs `DROP_DATABASE` and `CREATE_DATABASE` SQL statements using
146146
`DB_HOST`, `DB_NAME`, `DB_USER` and `DB_PASSWORD` database credentials
147-
specified in wp-config.php.
147+
specified in wp-config.php. Requires MySQL/MariaDB client binaries to be available.
148148

149149
**OPTIONS**
150150

@@ -177,7 +177,7 @@ wp db check [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--default
177177

178178
Runs `mysqlcheck` utility with `--check` using `DB_HOST`,
179179
`DB_NAME`, `DB_USER` and `DB_PASSWORD` database credentials
180-
specified in wp-config.php.
180+
specified in wp-config.php. Requires `mysqlcheck` or `mariadb-check` client binary.
181181

182182
[See docs](http://dev.mysql.com/doc/refman/5.7/en/check-table.html)
183183
for more details on the `CHECK TABLE` statement.
@@ -216,7 +216,7 @@ wp db optimize [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--defa
216216

217217
Runs `mysqlcheck` utility with `--optimize=true` using `DB_HOST`,
218218
`DB_NAME`, `DB_USER` and `DB_PASSWORD` database credentials
219-
specified in wp-config.php.
219+
specified in wp-config.php. Requires `mysqlcheck` or `mariadb-check` client binary.
220220

221221
[See docs](http://dev.mysql.com/doc/refman/5.7/en/optimize-table.html)
222222
for more details on the `OPTIMIZE TABLE` statement.
@@ -269,7 +269,7 @@ wp db repair [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--defaul
269269

270270
Runs `mysqlcheck` utility with `--repair=true` using `DB_HOST`,
271271
`DB_NAME`, `DB_USER` and `DB_PASSWORD` database credentials
272-
specified in wp-config.php.
272+
specified in wp-config.php. Requires `mysqlcheck` or `mariadb-check` client binary.
273273

274274
[See docs](http://dev.mysql.com/doc/refman/5.7/en/repair-table.html) for
275275
more details on the `REPAIR TABLE` statement.
@@ -297,14 +297,16 @@ more details on the `REPAIR TABLE` statement.
297297

298298
### wp db cli
299299

300-
Opens a MySQL console using credentials from wp-config.php
300+
Opens a MySQL console using credentials from wp-config.php.
301301

302302
~~~
303303
wp db cli [--database=<database>] [--default-character-set=<character-set>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--defaults]
304304
~~~
305305

306306
**Alias:** `connect`
307307

308+
Requires `mysql` or `mariadb` client binary to be available.
309+
308310
**OPTIONS**
309311

310312
[--database=<database>]
@@ -342,7 +344,9 @@ wp db query [<sql>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [-
342344
~~~
343345

344346
Executes an arbitrary SQL query using `DB_HOST`, `DB_NAME`, `DB_USER`
345-
and `DB_PASSWORD` database credentials specified in wp-config.php.
347+
and `DB_PASSWORD` database credentials specified in wp-config.php.
348+
If MySQL/MariaDB client binaries are not available, falls back to
349+
executing queries via WordPress's `wpdb`.
346350

347351
Use the `--skip-column-names` MySQL argument to exclude the headers
348352
from a SELECT query. Pipe the output to remove the ASCII table
@@ -445,6 +449,7 @@ wp db export [<file>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>]
445449

446450
Runs `mysqldump` utility using `DB_HOST`, `DB_NAME`, `DB_USER` and
447451
`DB_PASSWORD` database credentials specified in wp-config.php. Accepts any valid [`mysqldump` flags](https://dev.mysql.com/doc/en/mysqldump.html#mysqldump-option-summary).
452+
Requires `mysqldump` or `mariadb-dump` client binary to be available.
448453

449454
**OPTIONS**
450455

@@ -536,7 +541,8 @@ wp db import [<file>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>]
536541
Runs SQL queries using `DB_HOST`, `DB_NAME`, `DB_USER` and
537542
`DB_PASSWORD` database credentials specified in wp-config.php. This
538543
does not create database by itself and only performs whatever tasks are
539-
defined in the SQL.
544+
defined in the SQL. If MySQL/MariaDB client binaries are not available,
545+
falls back to importing via WordPress's `wpdb`.
540546

541547
**OPTIONS**
542548

‎features/db-import.feature‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,118 @@ Feature: Import a WordPress database
6969
Success: Imported from '{DB_NAME}.sql'.
7070
"""
7171

72+
@require-mysql-or-mariadb
73+
Scenario: Database import falls back to wpdb when mysql binary is unavailable
74+
Given a WP install
75+
And a fake-bin/mysql file:
76+
"""
77+
#!/bin/sh
78+
exit 127
79+
"""
80+
And a fake-bin/mariadb file:
81+
"""
82+
#!/bin/sh
83+
exit 127
84+
"""
85+
86+
When I try `wp db export wp_cli_test.sql`
87+
Then the wp_cli_test.sql file should exist
88+
89+
When I run `chmod +x fake-bin/mysql fake-bin/mariadb`
90+
And I try `env PATH={RUN_DIR}/fake-bin:$PATH wp db import wp_cli_test.sql --debug`
91+
Then STDOUT should be:
92+
"""
93+
Success: Imported from 'wp_cli_test.sql'.
94+
"""
95+
And STDERR should contain:
96+
"""
97+
MySQL/MariaDB binary not available, falling back to wpdb for import.
98+
"""
99+
100+
@require-mysql-or-mariadb
101+
Scenario: Database import falls back to wpdb and handles zero-date column defaults
102+
Given a WP install
103+
And a fake-bin/mysql file:
104+
"""
105+
#!/bin/sh
106+
exit 127
107+
"""
108+
And a fake-bin/mariadb file:
109+
"""
110+
#!/bin/sh
111+
exit 127
112+
"""
113+
And a zero_date.sql file:
114+
"""
115+
CREATE TABLE IF NOT EXISTS wp_zero_date_test (
116+
id INT AUTO_INCREMENT PRIMARY KEY,
117+
created_at DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00'
118+
);
119+
"""
120+
121+
When I run `chmod +x fake-bin/mysql fake-bin/mariadb`
122+
And I try `env PATH={RUN_DIR}/fake-bin:$PATH wp db import zero_date.sql --debug`
123+
Then STDOUT should be:
124+
"""
125+
Success: Imported from 'zero_date.sql'.
126+
"""
127+
128+
@require-mysql-or-mariadb
129+
Scenario: Database import falls back to wpdb and handles DELIMITER blocks with procedures
130+
Given a WP install
131+
And a fake-bin/mysql file:
132+
"""
133+
#!/bin/sh
134+
exit 127
135+
"""
136+
And a fake-bin/mariadb file:
137+
"""
138+
#!/bin/sh
139+
exit 127
140+
"""
141+
And a procedure.sql file:
142+
"""
143+
DELIMITER //
144+
CREATE PROCEDURE wp_test_procedure()
145+
BEGIN
146+
SELECT 1;
147+
END//
148+
DELIMITER ;
149+
"""
150+
151+
When I run `chmod +x fake-bin/mysql fake-bin/mariadb`
152+
And I try `env PATH={RUN_DIR}/fake-bin:$PATH wp db import procedure.sql --debug`
153+
Then STDOUT should be:
154+
"""
155+
Success: Imported from 'procedure.sql'.
156+
"""
157+
158+
@require-mysql-or-mariadb
159+
Scenario: Database import falls back to wpdb and handles malformed empty DELIMITER lines
160+
Given a WP install
161+
And a fake-bin/mysql file:
162+
"""
163+
#!/bin/sh
164+
exit 127
165+
"""
166+
And a fake-bin/mariadb file:
167+
"""
168+
#!/bin/sh
169+
exit 127
170+
"""
171+
And a malformed_delimiter.sql file:
172+
"""
173+
DELIMITER
174+
SELECT 1;
175+
"""
176+
177+
When I run `chmod +x fake-bin/mysql fake-bin/mariadb`
178+
And I try `env PATH={RUN_DIR}/fake-bin:$PATH wp db import malformed_delimiter.sql --debug`
179+
Then STDOUT should be:
180+
"""
181+
Success: Imported from 'malformed_delimiter.sql'.
182+
"""
183+
72184
# SQLite doesn't support the --dbuser flag.
73185
@require-mysql-or-mariadb
74186
Scenario: Import from database name path by default with passed-in dbuser/dbpass

‎features/db-query.feature‎

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,176 @@ Feature: Query the database with WordPress' MySQL config
155155
"""
156156
skip-ssl-verify-server-cert
157157
"""
158+
159+
@require-mysql-or-mariadb
160+
Scenario: Database querying falls back to wpdb when mysql binary is unavailable
161+
Given a WP install
162+
And a fake-bin/mysql file:
163+
"""
164+
#!/bin/sh
165+
exit 127
166+
"""
167+
And a fake-bin/mariadb file:
168+
"""
169+
#!/bin/sh
170+
exit 127
171+
"""
172+
173+
When I run `chmod +x fake-bin/mysql fake-bin/mariadb`
174+
And I try `env PATH={RUN_DIR}/fake-bin:$PATH wp db query "SELECT COUNT(ID) FROM wp_users;" --debug`
175+
Then STDOUT should be:
176+
"""
177+
COUNT(ID)
178+
1
179+
"""
180+
And STDERR should contain:
181+
"""
182+
MySQL/MariaDB binary not available, falling back to wpdb.
183+
"""
184+
185+
@require-mysql-or-mariadb
186+
Scenario: Database querying falls back to wpdb for multi-statement queries when mysql binary is unavailable
187+
Given a WP install
188+
And a fake-bin/mysql file:
189+
"""
190+
#!/bin/sh
191+
exit 127
192+
"""
193+
And a fake-bin/mariadb file:
194+
"""
195+
#!/bin/sh
196+
exit 127
197+
"""
198+
199+
When I run `chmod +x fake-bin/mysql fake-bin/mariadb`
200+
And I try `env PATH={RUN_DIR}/fake-bin:$PATH wp db query "SELECT 1 as num; SELECT 2 as num;" --debug`
201+
Then STDOUT should contain:
202+
"""
203+
num
204+
1
205+
"""
206+
And STDOUT should contain:
207+
"""
208+
num
209+
2
210+
"""
211+
212+
@require-mysql-or-mariadb
213+
Scenario: Database querying falls back to wpdb and reports results by statement type
214+
Given a WP install
215+
And a fake-bin/mysql file:
216+
"""
217+
#!/bin/sh
218+
exit 127
219+
"""
220+
And a fake-bin/mariadb file:
221+
"""
222+
#!/bin/sh
223+
exit 127
224+
"""
225+
And a queries.sql file:
226+
"""
227+
-- Statements read from STDIN.
228+
SELECT 'update; delete' AS `word;s`;
229+
UPDATE wp_options SET option_value = 'Fallback' WHERE option_name = 'blogname';
230+
SELECT option_value FROM wp_options WHERE option_name = 'blogname';
231+
"""
232+
233+
When I run `chmod +x fake-bin/mysql fake-bin/mariadb`
234+
And I try `env PATH={RUN_DIR}/fake-bin:$PATH wp db query < queries.sql`
235+
Then STDOUT should contain:
236+
"""
237+
word;s
238+
update; delete
239+
"""
240+
And STDOUT should contain:
241+
"""
242+
Success: Query succeeded. Rows affected: 1
243+
"""
244+
And STDOUT should contain:
245+
"""
246+
option_value
247+
Fallback
248+
"""
249+
And the return code should be 0
250+
251+
When I try `env PATH={RUN_DIR}/fake-bin:$PATH wp db query "SELECT * FROM wp_nonexistent_table"`
252+
Then STDERR should contain:
253+
"""
254+
Error: Query failed:
255+
"""
256+
And the return code should be 1
257+
258+
@require-mysql-or-mariadb
259+
Scenario: Database querying falls back to wpdb and reports connection errors
260+
Given a WP install
261+
And a fake-bin/mysql file:
262+
"""
263+
#!/bin/sh
264+
exit 127
265+
"""
266+
And a fake-bin/mariadb file:
267+
"""
268+
#!/bin/sh
269+
exit 127
270+
"""
271+
272+
When I run `chmod +x fake-bin/mysql fake-bin/mariadb`
273+
And I try `env PATH={RUN_DIR}/fake-bin:$PATH wp db query "SELECT 1" --dbpass=wrong-password`
274+
Then STDERR should contain:
275+
"""
276+
Error: Could not connect to the database.
277+
"""
278+
And the return code should be 1
279+
280+
@require-mysql-or-mariadb
281+
Scenario: Database querying falls back to wpdb and escapes tabs and newlines like the mysql client
282+
Given a WP install
283+
And a fake-bin/mysql file:
284+
"""
285+
#!/bin/sh
286+
exit 127
287+
"""
288+
And a fake-bin/mariadb file:
289+
"""
290+
#!/bin/sh
291+
exit 127
292+
"""
293+
294+
When I run `chmod +x fake-bin/mysql fake-bin/mariadb`
295+
And I try `env PATH={RUN_DIR}/fake-bin:$PATH wp db query "SELECT CONCAT('a', CHAR(9), 'b', CHAR(10), 'c', CHAR(92), 'd')" --skip-column-names`
296+
Then STDOUT should be:
297+
"""
298+
a\tb\nc\\d
299+
"""
300+
And the return code should be 0
301+
302+
When I try `env PATH={RUN_DIR}/fake-bin:$PATH wp db query "SELECT CONCAT('a', CHAR(10), 'b')" --skip-column-names --raw`
303+
Then STDOUT should be:
304+
"""
305+
a
306+
b
307+
"""
308+
And the return code should be 0
309+
310+
@require-mysql-or-mariadb
311+
Scenario: Database querying falls back to wpdb and honors the port argument
312+
Given a WP install
313+
And a fake-bin/mysql file:
314+
"""
315+
#!/bin/sh
316+
exit 127
317+
"""
318+
And a fake-bin/mariadb file:
319+
"""
320+
#!/bin/sh
321+
exit 127
322+
"""
323+
324+
When I run `chmod +x fake-bin/mysql fake-bin/mariadb`
325+
And I try `env PATH={RUN_DIR}/fake-bin:$PATH wp db query "SELECT 1" --host=127.0.0.1 --port=1`
326+
Then STDERR should contain:
327+
"""
328+
Error: Could not connect to the database.
329+
"""
330+
And the return code should be 1

0 commit comments

Comments
 (0)