diff --git a/features/db.feature b/features/db.feature index 94fda644b..a4d84429d 100644 --- a/features/db.feature +++ b/features/db.feature @@ -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 diff --git a/src/DB_Command.php b/src/DB_Command.php index 72463bda5..622bb5d3e 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -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=] + * : Username to pass to mysql. Defaults to DB_USER. + * + * [--dbpass=] + * : 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. * @@ -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; @@ -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)