Skip to content

Commit 647b235

Browse files
Copilotswissspidy
andauthored
Add --delete-tables-with-prefix to wp site delete for multisite custom table cleanup (#617)
* Initial plan * Add opt-in site delete table-prefix cleanup flag Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> * Update features/site.feature * Fix multisite delete safety checks Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> * Apply suggestion from @swissspidy * Apply suggestion from @swissspidy --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascalb@google.com>
1 parent 346446f commit 647b235

2 files changed

Lines changed: 85 additions & 1 deletion

File tree

‎features/site.feature‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,42 @@ Feature: Manage sites in a multisite installation
7878
When I try the previous command again
7979
Then the return code should be 1
8080

81+
@skip-windows
82+
Scenario: Delete a site by id and remove all prefixed tables
83+
Given a WP multisite subdirectory install
84+
85+
When I run `wp site create --slug=first --porcelain`
86+
Then STDOUT should be a number
87+
And save STDOUT as {SITE_ID}
88+
And I try `wp db query "CREATE TABLE wp_{SITE_ID}_custom_data (id INTEGER PRIMARY KEY);"`
89+
And I run `wp site delete {SITE_ID} --yes --delete-tables-with-prefix`
90+
And STDOUT should contain:
91+
"""
92+
Success: The site at '
93+
"""
94+
95+
When I try `wp db query "CREATE TABLE wp_{SITE_ID}_custom_data (id INTEGER PRIMARY KEY);"`
96+
Then STDOUT should be empty
97+
And the return code should be 0
98+
And I try `wp db query "DROP TABLE wp_{SITE_ID}_custom_data;"`
99+
And the return code should be 0
100+
101+
@skip-windows
102+
Scenario: Deleting a site cannot combine keep-tables with delete-tables-with-prefix
103+
Given a WP multisite subdirectory install
104+
105+
When I run `wp site create --slug=first --porcelain`
106+
Then STDOUT should be a number
107+
And save STDOUT as {SITE_ID}
108+
109+
When I try `wp site delete {SITE_ID} --yes --keep-tables --delete-tables-with-prefix`
110+
Then STDERR should be:
111+
"""
112+
Error: The '--keep-tables' and '--delete-tables-with-prefix' flags cannot be used together.
113+
"""
114+
And STDOUT should be empty
115+
And the return code should be 1
116+
81117
@skip-windows
82118
Scenario: Filter site list
83119
Given a WP multisite install

‎src/Site_Command.php‎

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ public function empty_( $args, $assoc_args ) {
288288
* [--keep-tables]
289289
* : Delete the blog from the list, but don't drop its tables.
290290
*
291+
* [--delete-tables-with-prefix]
292+
* : Delete all tables with the site's database table prefix after deleting the site.
293+
*
291294
* ## EXAMPLES
292295
*
293296
* $ wp site delete 123
@@ -299,11 +302,18 @@ public function delete( $args, $assoc_args ) {
299302
WP_CLI::error( 'This is not a multisite installation.' );
300303
}
301304

305+
if ( Utils\get_flag_value( $assoc_args, 'keep-tables' ) && Utils\get_flag_value( $assoc_args, 'delete-tables-with-prefix' ) ) {
306+
WP_CLI::error( "The '--keep-tables' and '--delete-tables-with-prefix' flags cannot be used together." );
307+
}
308+
302309
if ( isset( $assoc_args['slug'] ) ) {
303310
$blog_id = get_id_from_blogname( $assoc_args['slug'] );
304311
if ( null === $blog_id ) {
305312
WP_CLI::error( sprintf( 'Could not find site with slug \'%s\'.', $assoc_args['slug'] ) );
306313
}
314+
if ( is_main_site( $blog_id ) ) {
315+
WP_CLI::error( 'You cannot delete the root site.' );
316+
}
307317
$blog = get_blog_details( $blog_id );
308318
} else {
309319
if ( empty( $args ) ) {
@@ -327,11 +337,49 @@ public function delete( $args, $assoc_args ) {
327337

328338
WP_CLI::confirm( "Are you sure you want to delete the '{$site_url}' site?", $assoc_args );
329339

330-
wpmu_delete_blog( (int) $blog->blog_id, ! Utils\get_flag_value( $assoc_args, 'keep-tables' ) );
340+
$did_delete = wpmu_delete_blog( (int) $blog->blog_id, ! Utils\get_flag_value( $assoc_args, 'keep-tables' ) );
341+
if ( false === $did_delete ) {
342+
WP_CLI::error( "The site at '{$site_url}' could not be deleted." );
343+
}
344+
345+
if ( Utils\get_flag_value( $assoc_args, 'delete-tables-with-prefix' ) ) {
346+
$this->drop_tables_with_prefix( (int) $blog->blog_id );
347+
}
331348

332349
WP_CLI::success( "The site at '{$site_url}' was deleted." );
333350
}
334351

352+
/**
353+
* Drops all database tables for a site prefix.
354+
*
355+
* @param int $blog_id Site ID.
356+
*/
357+
private function drop_tables_with_prefix( $blog_id ) {
358+
global $wpdb;
359+
360+
$blog_prefix = $wpdb->get_blog_prefix( $blog_id );
361+
if ( is_main_site( $blog_id ) || $blog_prefix === $wpdb->base_prefix ) {
362+
WP_CLI::error( 'You cannot drop tables for the root site.' );
363+
}
364+
365+
$prefix_like = $wpdb->esc_like( $blog_prefix ) . '%';
366+
$tables = $wpdb->get_col( $wpdb->prepare( 'SHOW TABLES LIKE %s', $prefix_like ) );
367+
368+
if ( empty( $tables ) ) {
369+
return;
370+
}
371+
372+
$tables = array_map(
373+
static function ( $table ) {
374+
return '`' . str_replace( '`', '``', $table ) . '`';
375+
},
376+
$tables
377+
);
378+
379+
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Table identifiers are escaped and cannot be passed as placeholders.
380+
$wpdb->query( 'DROP TABLE IF EXISTS ' . implode( ', ', $tables ) );
381+
}
382+
335383
/**
336384
* Gets details about a site in a multisite installation.
337385
*

0 commit comments

Comments
 (0)