Skip to content

Commit fd4034d

Browse files
CopilotswissspidyCopilot
authored
Fix wp core update-db --network to respect network ID in multinetwork installations (#302)
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascalb@google.com>
1 parent 138641c commit fd4034d

File tree

2 files changed

+69
-7
lines changed

2 files changed

+69
-7
lines changed

features/core-update-db.feature

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,54 @@ Feature: Update core's database
151151
{UPDATE_VERSION}
152152
"""
153153

154+
# This test downgrades to an older WordPress version, but the SQLite plugin requires 6.0+
155+
@require-mysql
156+
Scenario: Update db only updates sites from the current network in multinetwork setup
157+
Given a WP multisite install
158+
And a disable_sidebar_check.php file:
159+
"""
160+
<?php
161+
WP_CLI::add_wp_hook( 'init', static function () {
162+
remove_action( 'after_switch_theme', '_wp_sidebars_changed' );
163+
} );
164+
"""
165+
And I try `wp theme install twentytwenty --activate`
166+
And I run `wp core download --version=5.4 --force`
167+
And I run `wp option update db_version 45805 --require=disable_sidebar_check.php`
168+
And I run `wp site option update wpmu_upgrade_site 45805`
169+
# Create 2 additional sites in network 1 (main site already exists; 3 total in network 1)
170+
And I run `wp site create --slug=net1-site1`
171+
And I run `wp site create --slug=net1-site2`
172+
# Create 2 sites that will be moved to a second network (5 sites total)
173+
And I run `wp site create --slug=net2-site1 --porcelain`
174+
And save STDOUT as {NET2_SITE1_ID}
175+
And I run `wp site create --slug=net2-site2 --porcelain`
176+
And save STDOUT as {NET2_SITE2_ID}
177+
# Register a second network (ID=2) and reassign those two blogs to it
178+
And I run `wp eval 'global $wpdb; $wpdb->insert( $wpdb->site, [ "domain" => "example.com", "path" => "/network2/" ] );'`
179+
And I run `wp eval 'global $wpdb; $wpdb->update( $wpdb->blogs, [ "site_id" => 2 ], [ "blog_id" => {NET2_SITE1_ID} ] ); $wpdb->update( $wpdb->blogs, [ "site_id" => 2 ], [ "blog_id" => {NET2_SITE2_ID} ] );'`
180+
181+
When I run `wp site option get wpmu_upgrade_site`
182+
Then save STDOUT as {UPDATE_VERSION}
183+
184+
# Only the 3 network-1 sites should be processed (not all 5 active sites).
185+
# If the site_id filter were absent, the count would be 5/5 and net2 URLs would appear.
186+
When I run `wp core update-db --network`
187+
Then STDOUT should contain:
188+
"""
189+
Success: WordPress database upgraded on 3/3 sites.
190+
"""
191+
And STDOUT should not contain:
192+
"""
193+
net2-site1
194+
"""
195+
196+
When I run `wp site option get wpmu_upgrade_site`
197+
Then STDOUT should not contain:
198+
"""
199+
{UPDATE_VERSION}
200+
"""
201+
154202
Scenario: Ensure update-db sets WP_INSTALLING constant
155203
Given a WP install
156204
And a before.php file:

src/Core_Command.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,6 +1522,11 @@ public function check_update_db( $args, $assoc_args ) {
15221522
* WordPress database upgraded successfully from db version 35700 to 29630 on example.com/
15231523
* Success: WordPress database upgraded on 123/123 sites.
15241524
*
1525+
* # Update databases for all sites on a specific network in a multinetwork install.
1526+
* $ wp core update-db --network --url=network2.example.com
1527+
* WordPress database upgraded successfully from db version 35700 to 29630 on network2.example.com/
1528+
* Success: WordPress database upgraded on 50/50 sites.
1529+
*
15251530
* @subcommand update-db
15261531
*
15271532
* @param string[] $args Positional arguments. Unused.
@@ -1541,9 +1546,22 @@ public function update_db( $args, $assoc_args ) {
15411546
}
15421547

15431548
if ( $network ) {
1549+
// Determine the network ID to update.
1550+
// get_current_network_id() (available since WP 4.9) reflects the network
1551+
// determined from the --url context, enabling per-network updates in
1552+
// multinetwork setups. Fall back to SITE_ID_CURRENT_SITE for older WordPress.
1553+
if ( function_exists( 'get_current_network_id' ) ) {
1554+
$network_id = get_current_network_id();
1555+
} elseif ( defined( 'SITE_ID_CURRENT_SITE' ) ) {
1556+
$network_id = (int) SITE_ID_CURRENT_SITE;
1557+
} else {
1558+
$network_id = 1;
1559+
}
1560+
15441561
$iterator_args = [
15451562
'table' => $wpdb->blogs,
15461563
'where' => [
1564+
'site_id' => $network_id,
15471565
'spam' => 0,
15481566
'deleted' => 0,
15491567
'archived' => 0,
@@ -1552,16 +1570,14 @@ public function update_db( $args, $assoc_args ) {
15521570
$it = new TableIterator( $iterator_args );
15531571
$success = 0;
15541572
$total = 0;
1555-
$site_ids = [];
15561573

15571574
/**
15581575
* @var object{site_id: int, domain: string, path: string} $blog
15591576
*/
15601577
foreach ( $it as $blog ) {
15611578
++$total;
1562-
$site_ids[] = $blog->site_id;
1563-
$url = $blog->domain . $blog->path;
1564-
$cmd = "--url={$url} core update-db";
1579+
$url = $blog->domain . $blog->path;
1580+
$cmd = "--url={$url} core update-db";
15651581
if ( $dry_run ) {
15661582
$cmd .= ' --dry-run';
15671583
}
@@ -1591,9 +1607,7 @@ public function update_db( $args, $assoc_args ) {
15911607
}
15921608
}
15931609
if ( ! $dry_run && $total && $success === $total ) {
1594-
foreach ( array_unique( $site_ids ) as $site_id ) {
1595-
update_metadata( 'site', $site_id, 'wpmu_upgrade_site', $wp_db_version );
1596-
}
1610+
update_metadata( 'site', $network_id, 'wpmu_upgrade_site', $wp_db_version );
15971611
}
15981612
WP_CLI::success( "WordPress database upgraded on {$success}/{$total} sites." );
15991613
} else {

0 commit comments

Comments
 (0)