Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --defaults flag to allow loading of MySQL configuration #157

Merged
merged 9 commits into from
Jan 9, 2020
37 changes: 29 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ specified in wp-config.php.
Checks the current status of the database.

~~~
wp db check [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>]
wp db check [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--defaults]
~~~

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

[--defaults]
Loads the environment's MySQL option files. Default behavior is to skip loading them to avoid failures due to misconfiguration.

**EXAMPLES**

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

~~~
wp db optimize [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>]
wp db optimize [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--defaults]
~~~

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

[--defaults]
Loads the environment's MySQL option files. Default behavior is to skip loading them to avoid failures due to misconfiguration.

**EXAMPLES**

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

~~~
wp db prefix
wp db prefix
~~~

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

~~~
wp db repair [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>]
wp db repair [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--defaults]
~~~

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

[--defaults]
Loads the environment's MySQL option files. Default behavior is to skip loading them to avoid failures due to misconfiguration.

**EXAMPLES**

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

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

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

[--defaults]
Loads the environment's MySQL option files. Default behavior is to skip loading them to avoid failures due to misconfiguration.

**EXAMPLES**

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

~~~
wp db query [<sql>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>]
wp db query [<sql>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--defaults]
~~~

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

[--defaults]
Loads the environment's MySQL option files. Default behavior is to skip loading them to avoid failures due to misconfiguration.

**EXAMPLES**

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

~~~
wp db export [<file>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--tables=<tables>] [--exclude_tables=<tables>] [--porcelain]
wp db export [<file>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--tables=<tables>] [--exclude_tables=<tables>] [--porcelain] [--defaults]
~~~

Runs `mysqldump` utility using `DB_HOST`, `DB_NAME`, `DB_USER` and
Expand Down Expand Up @@ -396,6 +411,9 @@ Runs `mysqldump` utility using `DB_HOST`, `DB_NAME`, `DB_USER` and
[--porcelain]
Output filename for the exported database.

[--defaults]
Loads the environment's MySQL option files. Default behavior is to skip loading them to avoid failures due to misconfiguration.

**EXAMPLES**

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

~~~
wp db import [<file>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--skip-optimization]
wp db import [<file>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--skip-optimization] [--defaults]
~~~

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

[--defaults]
Loads the environment's MySQL option files. Default behavior is to skip loading them to avoid failures due to misconfiguration.

**EXAMPLES**

# Import MySQL from a file.
Expand Down
47 changes: 47 additions & 0 deletions features/db-check.feature
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,32 @@ Feature: Check the database
Success: Database checked.
"""

Scenario: Run db check with MySQL defaults to check the database
Given a WP install

When I run `wp db check --defaults`
Then STDOUT should contain:
"""
wp_cli_test.wp_users
"""
And STDOUT should contain:
"""
Success: Database checked.
"""

Scenario: Run db check with --no-defaults to check the database
Given a WP install

When I run `wp db check --no-defaults`
Then STDOUT should contain:
"""
wp_cli_test.wp_users
"""
And STDOUT should contain:
"""
Success: Database checked.
"""

Scenario: Run db check with passed-in options
Given a WP install

Expand Down Expand Up @@ -97,3 +123,24 @@ Feature: Check the database
Access denied
"""
And STDOUT should be empty

Scenario: MySQL defaults are available as appropriate with --defaults flag
Given a WP install

When I try `wp db check --defaults --debug`
Then STDERR should contain:
"""
Debug (db): Running shell command: /usr/bin/env mysqlcheck %s
"""

When I try `wp db check --debug`
Then STDERR should contain:
"""
Debug (db): Running shell command: /usr/bin/env mysqlcheck --no-defaults %s
"""

When I try `wp db check --no-defaults --debug`
Then STDERR should contain:
"""
Debug (db): Running shell command: /usr/bin/env mysqlcheck --no-defaults %s
"""
schlessera marked this conversation as resolved.
Show resolved Hide resolved
39 changes: 39 additions & 0 deletions features/db-export.feature
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ Feature: Export a WordPress database
-- MySQL dump
"""

Scenario: Export database with mysql defaults to STDOUT
Given a WP install

When I run `wp db export --defaults -`
Then STDOUT should contain:
"""
-- MySQL dump
"""

Scenario: Export database with mysql --no-defaults to STDOUT
Given a WP install

When I run `wp db export --no-defaults -`
Then STDOUT should contain:
"""
-- MySQL dump
"""

Scenario: Export database with passed-in options
Given a WP install

Expand All @@ -59,3 +77,24 @@ Feature: Export a WordPress database
Access denied
"""
And STDOUT should be empty

Scenario: MySQL defaults are available as appropriate with --defaults flag
Given a WP install

When I try `wp db export --defaults --debug`
Then STDERR should contain:
"""
Debug (db): Running initial shell command: /usr/bin/env mysqldump
"""

When I try `wp db export --debug`
Then STDERR should contain:
"""
Debug (db): Running initial shell command: /usr/bin/env mysqldump --no-defaults
"""

When I try `wp db export --no-defaults --debug`
Then STDERR should contain:
"""
Debug (db): Running initial shell command: /usr/bin/env mysqldump --no-defaults
"""
47 changes: 47 additions & 0 deletions features/db-import.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,30 @@ Feature: Import a WordPress database
Success: Imported from 'wp_cli_test.sql'.
"""

Scenario: Import from database name path by default with mysql defaults
Given a WP install

When I run `wp db export wp_cli_test.sql`
Then the wp_cli_test.sql file should exist

When I run `wp db import --defaults`
Then STDOUT should be:
"""
Success: Imported from 'wp_cli_test.sql'.
"""

Scenario: Import from database name path by default with --no-defaults
Given a WP install

When I run `wp db export wp_cli_test.sql`
Then the wp_cli_test.sql file should exist

When I run `wp db import --no-defaults`
Then STDOUT should be:
"""
Success: Imported from 'wp_cli_test.sql'.
"""

Scenario: Import from STDIN
Given a WP install

Expand Down Expand Up @@ -104,3 +128,26 @@ Feature: Import a WordPress database
"""
wp db import
"""
Scenario: MySQL defaults are available as appropriate with --defaults flag
Given a WP install

When I run `wp db export wp_cli_test.sql`
Then the wp_cli_test.sql file should exist

When I try `wp db import --defaults --debug`
Then STDERR should contain:
"""
Debug (db): Running shell command: /usr/bin/env mysql --no-auto-rehash
"""

When I try `wp db import --debug`
Then STDERR should contain:
"""
Debug (db): Running shell command: /usr/bin/env mysql --no-defaults --no-auto-rehash
"""

When I try `wp db import --no-defaults --debug`
Then STDERR should contain:
"""
Debug (db): Running shell command: /usr/bin/env mysql --no-defaults --no-auto-rehash
"""
55 changes: 55 additions & 0 deletions features/db-query.feature
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,58 @@ Feature: Query the database with WordPress' MySQL config
Access denied
"""
And STDOUT should be empty

Scenario: Database querying with MySQL defaults and passed-in options
Given a WP install

When I run `wp db query --defaults "SELECT COUNT(ID) FROM wp_posts;" --dbuser=wp_cli_test --html`
Then STDOUT should contain:
"""
<TABLE
"""

When I try `wp db query --defaults "SELECT COUNT(ID) FROM wp_posts;" --dbuser=no_such_user`
Then the return code should not be 0
And STDERR should contain:
"""
Access denied
"""
And STDOUT should be empty

Scenario: Database querying with --nodefaults and passed-in options
Given a WP install

When I run `wp db query --no-defaults "SELECT COUNT(ID) FROM wp_posts;" --dbuser=wp_cli_test --html`
Then STDOUT should contain:
"""
<TABLE
"""

When I try `wp db query --no-defaults "SELECT COUNT(ID) FROM wp_posts;" --dbuser=no_such_user`
Then the return code should not be 0
And STDERR should contain:
"""
Access denied
"""
And STDOUT should be empty

Scenario: MySQL defaults are available as appropriate with --defaults flag
Given a WP install

When I try `wp db query --defaults --debug`
Then STDERR should contain:
"""
Debug (db): Running shell command: /usr/bin/env mysql --no-auto-rehash
"""

When I try `wp db query --debug`
Then STDERR should contain:
"""
Debug (db): Running shell command: /usr/bin/env mysql --no-defaults --no-auto-rehash
"""

When I try `wp db query --no-defaults --debug`
Then STDERR should contain:
"""
Debug (db): Running shell command: /usr/bin/env mysql --no-defaults --no-auto-rehash
"""
Loading