Skip to content

Commit 11895a7

Browse files
authored
Add --defaults flag to allow loading of MySQL configuration
Add `--defaults` flag to allow loading of MySQL configuration
2 parents 119cf31 + ed1a640 commit 11895a7

File tree

6 files changed

+303
-23
lines changed

6 files changed

+303
-23
lines changed

README.md

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ specified in wp-config.php.
160160
Checks the current status of the database.
161161

162162
~~~
163-
wp db check [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>]
163+
wp db check [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--defaults]
164164
~~~
165165

166166
Runs `mysqlcheck` utility with `--check` using `DB_HOST`,
@@ -181,6 +181,9 @@ for more details on the `CHECK TABLE` statement.
181181
[--<field>=<value>]
182182
Extra arguments to pass to mysqlcheck. [Refer to mysqlcheck docs](https://dev.mysql.com/doc/en/mysqlcheck.html).
183183

184+
[--defaults]
185+
Loads the environment's MySQL option files. Default behavior is to skip loading them to avoid failures due to misconfiguration.
186+
184187
**EXAMPLES**
185188

186189
$ wp db check
@@ -193,7 +196,7 @@ for more details on the `CHECK TABLE` statement.
193196
Optimizes the database.
194197

195198
~~~
196-
wp db optimize [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>]
199+
wp db optimize [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--defaults]
197200
~~~
198201

199202
Runs `mysqlcheck` utility with `--optimize=true` using `DB_HOST`,
@@ -214,6 +217,9 @@ for more details on the `OPTIMIZE TABLE` statement.
214217
[--<field>=<value>]
215218
Extra arguments to pass to mysqlcheck. [Refer to mysqlcheck docs](https://dev.mysql.com/doc/en/mysqlcheck.html).
216219

220+
[--defaults]
221+
Loads the environment's MySQL option files. Default behavior is to skip loading them to avoid failures due to misconfiguration.
222+
217223
**EXAMPLES**
218224

219225
$ wp db optimize
@@ -226,7 +232,7 @@ for more details on the `OPTIMIZE TABLE` statement.
226232
Displays the database table prefix.
227233

228234
~~~
229-
wp db prefix
235+
wp db prefix
230236
~~~
231237

232238
Display the database table prefix, as defined by the database handler's interpretation of the current site.
@@ -243,7 +249,7 @@ Display the database table prefix, as defined by the database handler's interpre
243249
Repairs the database.
244250

245251
~~~
246-
wp db repair [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>]
252+
wp db repair [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--defaults]
247253
~~~
248254

249255
Runs `mysqlcheck` utility with `--repair=true` using `DB_HOST`,
@@ -264,6 +270,9 @@ more details on the `REPAIR TABLE` statement.
264270
[--<field>=<value>]
265271
Extra arguments to pass to mysqlcheck. [Refer to mysqlcheck docs](https://dev.mysql.com/doc/en/mysqlcheck.html).
266272

273+
[--defaults]
274+
Loads the environment's MySQL option files. Default behavior is to skip loading them to avoid failures due to misconfiguration.
275+
267276
**EXAMPLES**
268277

269278
$ wp db repair
@@ -276,7 +285,7 @@ more details on the `REPAIR TABLE` statement.
276285
Opens a MySQL console using credentials from wp-config.php
277286

278287
~~~
279-
wp db cli [--database=<database>] [--default-character-set=<character-set>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>]
288+
wp db cli [--database=<database>] [--default-character-set=<character-set>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--defaults]
280289
~~~
281290

282291
**OPTIONS**
@@ -296,6 +305,9 @@ wp db cli [--database=<database>] [--default-character-set=<character-set>] [--d
296305
[--<field>=<value>]
297306
Extra arguments to pass to mysql. [Refer to mysql docs](https://dev.mysql.com/doc/en/mysql-command-options.html).
298307

308+
[--defaults]
309+
Loads the environment's MySQL option files. Default behavior is to skip loading them to avoid failures due to misconfiguration.
310+
299311
**EXAMPLES**
300312

301313
# Open MySQL console
@@ -309,7 +321,7 @@ wp db cli [--database=<database>] [--default-character-set=<character-set>] [--d
309321
Executes a SQL query against the database.
310322

311323
~~~
312-
wp db query [<sql>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>]
324+
wp db query [<sql>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--defaults]
313325
~~~
314326

315327
Executes an arbitrary SQL query using `DB_HOST`, `DB_NAME`, `DB_USER`
@@ -329,6 +341,9 @@ Executes an arbitrary SQL query using `DB_HOST`, `DB_NAME`, `DB_USER`
329341
[--<field>=<value>]
330342
Extra arguments to pass to mysql. [Refer to mysql docs](https://dev.mysql.com/doc/en/mysql-command-options.html).
331343

344+
[--defaults]
345+
Loads the environment's MySQL option files. Default behavior is to skip loading them to avoid failures due to misconfiguration.
346+
332347
**EXAMPLES**
333348

334349
# Execute a query stored in a file
@@ -366,7 +381,7 @@ Executes an arbitrary SQL query using `DB_HOST`, `DB_NAME`, `DB_USER`
366381
Exports the database to a file or to STDOUT.
367382

368383
~~~
369-
wp db export [<file>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--tables=<tables>] [--exclude_tables=<tables>] [--porcelain]
384+
wp db export [<file>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--tables=<tables>] [--exclude_tables=<tables>] [--porcelain] [--defaults]
370385
~~~
371386

372387
Runs `mysqldump` utility using `DB_HOST`, `DB_NAME`, `DB_USER` and
@@ -396,6 +411,9 @@ Runs `mysqldump` utility using `DB_HOST`, `DB_NAME`, `DB_USER` and
396411
[--porcelain]
397412
Output filename for the exported database.
398413

414+
[--defaults]
415+
Loads the environment's MySQL option files. Default behavior is to skip loading them to avoid failures due to misconfiguration.
416+
399417
**EXAMPLES**
400418

401419
# Export database with drop query included
@@ -450,7 +468,7 @@ Runs `mysqldump` utility using `DB_HOST`, `DB_NAME`, `DB_USER` and
450468
Imports a database from a file or from STDIN.
451469

452470
~~~
453-
wp db import [<file>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--skip-optimization]
471+
wp db import [<file>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--skip-optimization] [--defaults]
454472
~~~
455473

456474
Runs SQL queries using `DB_HOST`, `DB_NAME`, `DB_USER` and
@@ -475,6 +493,9 @@ defined in the SQL.
475493
[--skip-optimization]
476494
When using an SQL file, do not include speed optimization such as disabling auto-commit and key checks.
477495

496+
[--defaults]
497+
Loads the environment's MySQL option files. Default behavior is to skip loading them to avoid failures due to misconfiguration.
498+
478499
**EXAMPLES**
479500

480501
# Import MySQL from a file.

features/db-check.feature

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,32 @@ Feature: Check the database
1313
Success: Database checked.
1414
"""
1515

16+
Scenario: Run db check with MySQL defaults to check the database
17+
Given a WP install
18+
19+
When I run `wp db check --defaults`
20+
Then STDOUT should contain:
21+
"""
22+
wp_cli_test.wp_users
23+
"""
24+
And STDOUT should contain:
25+
"""
26+
Success: Database checked.
27+
"""
28+
29+
Scenario: Run db check with --no-defaults to check the database
30+
Given a WP install
31+
32+
When I run `wp db check --no-defaults`
33+
Then STDOUT should contain:
34+
"""
35+
wp_cli_test.wp_users
36+
"""
37+
And STDOUT should contain:
38+
"""
39+
Success: Database checked.
40+
"""
41+
1642
Scenario: Run db check with passed-in options
1743
Given a WP install
1844

@@ -97,3 +123,24 @@ Feature: Check the database
97123
Access denied
98124
"""
99125
And STDOUT should be empty
126+
127+
Scenario: MySQL defaults are available as appropriate with --defaults flag
128+
Given a WP install
129+
130+
When I try `wp db check --defaults --debug`
131+
Then STDERR should contain:
132+
"""
133+
Debug (db): Running shell command: /usr/bin/env mysqlcheck %s
134+
"""
135+
136+
When I try `wp db check --debug`
137+
Then STDERR should contain:
138+
"""
139+
Debug (db): Running shell command: /usr/bin/env mysqlcheck --no-defaults %s
140+
"""
141+
142+
When I try `wp db check --no-defaults --debug`
143+
Then STDERR should contain:
144+
"""
145+
Debug (db): Running shell command: /usr/bin/env mysqlcheck --no-defaults %s
146+
"""

features/db-export.feature

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,24 @@ Feature: Export a WordPress database
4343
-- MySQL dump
4444
"""
4545

46+
Scenario: Export database with mysql defaults to STDOUT
47+
Given a WP install
48+
49+
When I run `wp db export --defaults -`
50+
Then STDOUT should contain:
51+
"""
52+
-- MySQL dump
53+
"""
54+
55+
Scenario: Export database with mysql --no-defaults to STDOUT
56+
Given a WP install
57+
58+
When I run `wp db export --no-defaults -`
59+
Then STDOUT should contain:
60+
"""
61+
-- MySQL dump
62+
"""
63+
4664
Scenario: Export database with passed-in options
4765
Given a WP install
4866

@@ -59,3 +77,24 @@ Feature: Export a WordPress database
5977
Access denied
6078
"""
6179
And STDOUT should be empty
80+
81+
Scenario: MySQL defaults are available as appropriate with --defaults flag
82+
Given a WP install
83+
84+
When I try `wp db export --defaults --debug`
85+
Then STDERR should contain:
86+
"""
87+
Debug (db): Running initial shell command: /usr/bin/env mysqldump
88+
"""
89+
90+
When I try `wp db export --debug`
91+
Then STDERR should contain:
92+
"""
93+
Debug (db): Running initial shell command: /usr/bin/env mysqldump --no-defaults
94+
"""
95+
96+
When I try `wp db export --no-defaults --debug`
97+
Then STDERR should contain:
98+
"""
99+
Debug (db): Running initial shell command: /usr/bin/env mysqldump --no-defaults
100+
"""

features/db-import.feature

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,30 @@ Feature: Import a WordPress database
1212
Success: Imported from 'wp_cli_test.sql'.
1313
"""
1414

15+
Scenario: Import from database name path by default with mysql defaults
16+
Given a WP install
17+
18+
When I run `wp db export wp_cli_test.sql`
19+
Then the wp_cli_test.sql file should exist
20+
21+
When I run `wp db import --defaults`
22+
Then STDOUT should be:
23+
"""
24+
Success: Imported from 'wp_cli_test.sql'.
25+
"""
26+
27+
Scenario: Import from database name path by default with --no-defaults
28+
Given a WP install
29+
30+
When I run `wp db export wp_cli_test.sql`
31+
Then the wp_cli_test.sql file should exist
32+
33+
When I run `wp db import --no-defaults`
34+
Then STDOUT should be:
35+
"""
36+
Success: Imported from 'wp_cli_test.sql'.
37+
"""
38+
1539
Scenario: Import from STDIN
1640
Given a WP install
1741

@@ -104,3 +128,26 @@ Feature: Import a WordPress database
104128
"""
105129
wp db import
106130
"""
131+
Scenario: MySQL defaults are available as appropriate with --defaults flag
132+
Given a WP install
133+
134+
When I run `wp db export wp_cli_test.sql`
135+
Then the wp_cli_test.sql file should exist
136+
137+
When I try `wp db import --defaults --debug`
138+
Then STDERR should contain:
139+
"""
140+
Debug (db): Running shell command: /usr/bin/env mysql --no-auto-rehash
141+
"""
142+
143+
When I try `wp db import --debug`
144+
Then STDERR should contain:
145+
"""
146+
Debug (db): Running shell command: /usr/bin/env mysql --no-defaults --no-auto-rehash
147+
"""
148+
149+
When I try `wp db import --no-defaults --debug`
150+
Then STDERR should contain:
151+
"""
152+
Debug (db): Running shell command: /usr/bin/env mysql --no-defaults --no-auto-rehash
153+
"""

features/db-query.feature

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,58 @@ Feature: Query the database with WordPress' MySQL config
3737
Access denied
3838
"""
3939
And STDOUT should be empty
40+
41+
Scenario: Database querying with MySQL defaults and passed-in options
42+
Given a WP install
43+
44+
When I run `wp db query --defaults "SELECT COUNT(ID) FROM wp_posts;" --dbuser=wp_cli_test --html`
45+
Then STDOUT should contain:
46+
"""
47+
<TABLE
48+
"""
49+
50+
When I try `wp db query --defaults "SELECT COUNT(ID) FROM wp_posts;" --dbuser=no_such_user`
51+
Then the return code should not be 0
52+
And STDERR should contain:
53+
"""
54+
Access denied
55+
"""
56+
And STDOUT should be empty
57+
58+
Scenario: Database querying with --nodefaults and passed-in options
59+
Given a WP install
60+
61+
When I run `wp db query --no-defaults "SELECT COUNT(ID) FROM wp_posts;" --dbuser=wp_cli_test --html`
62+
Then STDOUT should contain:
63+
"""
64+
<TABLE
65+
"""
66+
67+
When I try `wp db query --no-defaults "SELECT COUNT(ID) FROM wp_posts;" --dbuser=no_such_user`
68+
Then the return code should not be 0
69+
And STDERR should contain:
70+
"""
71+
Access denied
72+
"""
73+
And STDOUT should be empty
74+
75+
Scenario: MySQL defaults are available as appropriate with --defaults flag
76+
Given a WP install
77+
78+
When I try `wp db query --defaults --debug`
79+
Then STDERR should contain:
80+
"""
81+
Debug (db): Running shell command: /usr/bin/env mysql --no-auto-rehash
82+
"""
83+
84+
When I try `wp db query --debug`
85+
Then STDERR should contain:
86+
"""
87+
Debug (db): Running shell command: /usr/bin/env mysql --no-defaults --no-auto-rehash
88+
"""
89+
90+
When I try `wp db query --no-defaults --debug`
91+
Then STDERR should contain:
92+
"""
93+
Debug (db): Running shell command: /usr/bin/env mysql --no-defaults --no-auto-rehash
94+
"""

0 commit comments

Comments
 (0)