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 wp db clean command #93

Merged
merged 14 commits into from
May 8, 2018
31 changes: 31 additions & 0 deletions features/db.feature
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,37 @@ Feature: Perform database operations
"""
And STDOUT should be empty

Scenario: Clean up a WordPress install without dropping its database entirely but tables with prefix.
Given a WP install

When I run `wp db query "create table custom_table as select * from wp_users;"`
Then STDOUT should be empty
And the return code should be 0

When I run `wp db clean --yes --dbuser=wp_cli_test --dbpass=password1`
Then STDOUT should be:
"""
Success: Tables dropped.
"""

When I run `wp core install --title="WP-CLI Test" --url=example.com --admin_user=admin --admin_password=admin --admin_email=admin@example.com`
Then STDOUT should not be empty

When I try `wp db clean --yes --dbuser=no_such_user`
Then the return code should not be 0
And STDERR should contain:
"""
Access denied
"""
And STDOUT should be empty

When I run `wp db tables custom_table --all-tables`
Then STDOUT should be:
"""
custom_table
"""
And the return code should be 0

Scenario: DB Operations
Given a WP install

Expand Down
64 changes: 61 additions & 3 deletions src/DB_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,64 @@ public function reset( $_, $assoc_args ) {
WP_CLI::success( "Database reset." );
}

/**
* Removes all tables with `$table_prefix` from the database.
*
* Runs `DROP_TABLE` for each table that has a `$table_prefix` as specified
* in wp-config.php.
*
* ## OPTIONS
*
* [--dbuser=<value>]
* : Username to pass to mysql. Defaults to DB_USER.
*
* [--dbpass=<value>]
* : Password to pass to mysql. Defaults to DB_PASSWORD.
*
* [--yes]
* : Answer yes to the confirmation message.
*
* ## EXAMPLES
*
* # Delete all tables that match the current site prefix.
* $ wp db clean --yes
* Success: Tables dropped.
*
* @when after_wp_load
*/
public function clean( $_, $assoc_args ) {
global $wpdb;

WP_CLI::confirm(
sprintf(
"Are you sure you want to drop all the tables on '%s' that use the current site's database prefix ('%s')?",
DB_NAME,
$wpdb->get_blog_prefix()
),
$assoc_args
);

$mysql_args = self::get_dbuser_dbpass_args( $assoc_args );

$tables = WP_CLI\Utils\wp_get_table_names(
array(),
array( 'all-tables-with-prefix' )
);

foreach ( $tables as $table ) {
self::run_query(
sprintf(
'DROP TABLE IF EXISTS `%s`.`%s`',
DB_NAME,
$table
),
$mysql_args
);
}

WP_CLI::success( 'Tables dropped.' );
}

/**
* Checks the current status of the database.
*
Expand Down Expand Up @@ -744,11 +802,11 @@ public function size( $args, $assoc_args ) {
case 'tb':
$divisor = TB_IN_BYTES;
break;

case 'gb':
$divisor = GB_IN_BYTES;
break;

case 'mb':
$divisor = MB_IN_BYTES;
break;
Expand Down Expand Up @@ -865,7 +923,7 @@ public function prefix() {
* : Percent color code to use for the match (unless both before and after context are 0, when no color code is used). For a list of available percent color codes, see below. Default '%3%k' (black on a mustard background).
*
* The percent color codes available are:
*
*
* | Code | Color
* | ---- | -----
* | %y | Yellow (dark) (mustard)
Expand Down