Skip to content

Commit ce8c51a

Browse files
authored
Merge pull request #93 from marksabbath/feature/wp-db-clean
Add `wp db clean` command
2 parents b29f461 + 4fc68ff commit ce8c51a

File tree

2 files changed

+92
-3
lines changed

2 files changed

+92
-3
lines changed

features/db.feature

+31
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,37 @@ Feature: Perform database operations
104104
"""
105105
And STDOUT should be empty
106106
107+
Scenario: Clean up a WordPress install without dropping its database entirely but tables with prefix.
108+
Given a WP install
109+
110+
When I run `wp db query "create table custom_table as select * from wp_users;"`
111+
Then STDOUT should be empty
112+
And the return code should be 0
113+
114+
When I run `wp db clean --yes --dbuser=wp_cli_test --dbpass=password1`
115+
Then STDOUT should be:
116+
"""
117+
Success: Tables dropped.
118+
"""
119+
120+
When I run `wp core install --title="WP-CLI Test" --url=example.com --admin_user=admin --admin_password=admin [email protected]`
121+
Then STDOUT should not be empty
122+
123+
When I try `wp db clean --yes --dbuser=no_such_user`
124+
Then the return code should not be 0
125+
And STDERR should contain:
126+
"""
127+
Access denied
128+
"""
129+
And STDOUT should be empty
130+
131+
When I run `wp db tables custom_table --all-tables`
132+
Then STDOUT should be:
133+
"""
134+
custom_table
135+
"""
136+
And the return code should be 0
137+
107138
Scenario: DB Operations
108139
Given a WP install
109140

src/DB_Command.php

+61-3
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,64 @@ public function reset( $_, $assoc_args ) {
118118
WP_CLI::success( "Database reset." );
119119
}
120120

121+
/**
122+
* Removes all tables with `$table_prefix` from the database.
123+
*
124+
* Runs `DROP_TABLE` for each table that has a `$table_prefix` as specified
125+
* in wp-config.php.
126+
*
127+
* ## OPTIONS
128+
*
129+
* [--dbuser=<value>]
130+
* : Username to pass to mysql. Defaults to DB_USER.
131+
*
132+
* [--dbpass=<value>]
133+
* : Password to pass to mysql. Defaults to DB_PASSWORD.
134+
*
135+
* [--yes]
136+
* : Answer yes to the confirmation message.
137+
*
138+
* ## EXAMPLES
139+
*
140+
* # Delete all tables that match the current site prefix.
141+
* $ wp db clean --yes
142+
* Success: Tables dropped.
143+
*
144+
* @when after_wp_load
145+
*/
146+
public function clean( $_, $assoc_args ) {
147+
global $wpdb;
148+
149+
WP_CLI::confirm(
150+
sprintf(
151+
"Are you sure you want to drop all the tables on '%s' that use the current site's database prefix ('%s')?",
152+
DB_NAME,
153+
$wpdb->get_blog_prefix()
154+
),
155+
$assoc_args
156+
);
157+
158+
$mysql_args = self::get_dbuser_dbpass_args( $assoc_args );
159+
160+
$tables = WP_CLI\Utils\wp_get_table_names(
161+
array(),
162+
array( 'all-tables-with-prefix' )
163+
);
164+
165+
foreach ( $tables as $table ) {
166+
self::run_query(
167+
sprintf(
168+
'DROP TABLE IF EXISTS `%s`.`%s`',
169+
DB_NAME,
170+
$table
171+
),
172+
$mysql_args
173+
);
174+
}
175+
176+
WP_CLI::success( 'Tables dropped.' );
177+
}
178+
121179
/**
122180
* Checks the current status of the database.
123181
*
@@ -744,11 +802,11 @@ public function size( $args, $assoc_args ) {
744802
case 'tb':
745803
$divisor = TB_IN_BYTES;
746804
break;
747-
805+
748806
case 'gb':
749807
$divisor = GB_IN_BYTES;
750808
break;
751-
809+
752810
case 'mb':
753811
$divisor = MB_IN_BYTES;
754812
break;
@@ -865,7 +923,7 @@ public function prefix() {
865923
* : 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).
866924
*
867925
* The percent color codes available are:
868-
*
926+
*
869927
* | Code | Color
870928
* | ---- | -----
871929
* | %y | Yellow (dark) (mustard)

0 commit comments

Comments
 (0)