From f1c9d05d2bfabd83de6f75bfc3205477ee8a2bfc Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 7 Nov 2025 22:12:55 +0000 Subject: [PATCH 1/6] Initial plan From fe805232f9fd0022339b0ff7ff64c7a5e9280d12 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 7 Nov 2025 22:23:26 +0000 Subject: [PATCH 2/6] Fix multinetwork support in update-db --network command Add site_id filtering to ensure only sites from the current network are updated when using --network flag. This allows proper operation in multinetwork WordPress installations when combined with --url parameter. Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> --- src/Core_Command.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Core_Command.php b/src/Core_Command.php index b9bbb2b4..18e12dd4 100644 --- a/src/Core_Command.php +++ b/src/Core_Command.php @@ -1296,6 +1296,11 @@ public function update( $args, $assoc_args ) { * WordPress database upgraded successfully from db version 35700 to 29630 on example.com/ * Success: WordPress database upgraded on 123/123 sites. * + * # Update databases for all sites on a specific network in a multinetwork install. + * $ wp core update-db --network --url=network2.example.com + * WordPress database upgraded successfully from db version 35700 to 29630 on network2.example.com/ + * Success: WordPress database upgraded on 50/50 sites. + * * @subcommand update-db * * @param string[] $args Positional arguments. Unused. @@ -1315,9 +1320,20 @@ public function update_db( $args, $assoc_args ) { } if ( $network ) { + // Determine the network ID to update + // In multinetwork setups, use the current network (determined by --url parameter) + $network_id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : null; + if ( null === $network_id && function_exists( 'get_current_network_id' ) ) { + $network_id = get_current_network_id(); + } + if ( null === $network_id ) { + $network_id = 1; // Default to primary network + } + $iterator_args = [ 'table' => $wpdb->blogs, 'where' => [ + 'site_id' => $network_id, 'spam' => 0, 'deleted' => 0, 'archived' => 0, From b9f51da705c7a5d690408e58c0a9e1ccef28e2b0 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 7 Nov 2025 22:24:58 +0000 Subject: [PATCH 3/6] Add test to verify network ID detection in multisite Add a simple test to verify that SITE_ID_CURRENT_SITE is properly defined in multisite installations, which is used by the update-db command to filter sites by network. Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> --- features/core-update-db.feature | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/features/core-update-db.feature b/features/core-update-db.feature index 26c0b868..17d963d1 100644 --- a/features/core-update-db.feature +++ b/features/core-update-db.feature @@ -151,6 +151,24 @@ Feature: Update core's database {UPDATE_VERSION} """ + Scenario: Update db respects current network in multinetwork setup + Given a WP multisite install + And a disable_sidebar_check.php file: + """ + Date: Sat, 8 Nov 2025 08:48:09 +0000 Subject: [PATCH 4/6] Add complete database update steps to multinetwork test Include WordPress downgrade, database version downgrade, site creation, and verification of the actual update-db command execution and success. Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> --- features/core-update-db.feature | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/features/core-update-db.feature b/features/core-update-db.feature index 17d963d1..ab698d49 100644 --- a/features/core-update-db.feature +++ b/features/core-update-db.feature @@ -151,6 +151,8 @@ Feature: Update core's database {UPDATE_VERSION} """ + # This test downgrades to an older WordPress version, but the SQLite plugin requires 6.0+ + @require-mysql Scenario: Update db respects current network in multinetwork setup Given a WP multisite install And a disable_sidebar_check.php file: @@ -161,14 +163,33 @@ Feature: Update core's database } ); """ And I try `wp theme install twentytwenty --activate` + And I run `wp core download --version=5.4 --force` + And I run `wp option update db_version 45805 --require=disable_sidebar_check.php` + And I run `wp site option update wpmu_upgrade_site 45805` + And I run `wp site create --slug=foo` + And I run `wp site create --slug=bar` - # Verify that network ID is determined correctly When I run `wp eval "echo defined('SITE_ID_CURRENT_SITE') ? SITE_ID_CURRENT_SITE : 'not defined';"` Then STDOUT should contain: """ 1 """ + When I run `wp site option get wpmu_upgrade_site` + Then save STDOUT as {UPDATE_VERSION} + + When I run `wp core update-db --network` + Then STDOUT should contain: + """ + Success: WordPress database upgraded on 3/3 sites. + """ + + When I run `wp site option get wpmu_upgrade_site` + Then STDOUT should not contain: + """ + {UPDATE_VERSION} + """ + Scenario: Ensure update-db sets WP_INSTALLING constant Given a WP install And a before.php file: From 67cef5c89d84a234c87e5bb49e58db91b5292a4c Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Sat, 8 Nov 2025 11:13:57 +0100 Subject: [PATCH 5/6] Update src/Core_Command.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/Core_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core_Command.php b/src/Core_Command.php index 18e12dd4..4d46a3a4 100644 --- a/src/Core_Command.php +++ b/src/Core_Command.php @@ -1326,7 +1326,7 @@ public function update_db( $args, $assoc_args ) { if ( null === $network_id && function_exists( 'get_current_network_id' ) ) { $network_id = get_current_network_id(); } - if ( null === $network_id ) { + if ( ! $network_id ) { $network_id = 1; // Default to primary network } From c1c18440fc4f047ea5278c8e23441afc0eceab31 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 8 Nov 2025 10:16:17 +0000 Subject: [PATCH 6/6] Optimize network metadata update to use network_id directly Since all blogs are filtered by site_id = network_id, there's no need to collect and deduplicate site_ids. Directly use network_id for the metadata update. Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> --- src/Core_Command.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/Core_Command.php b/src/Core_Command.php index 4d46a3a4..05ee9907 100644 --- a/src/Core_Command.php +++ b/src/Core_Command.php @@ -1342,16 +1342,14 @@ public function update_db( $args, $assoc_args ) { $it = new TableIterator( $iterator_args ); $success = 0; $total = 0; - $site_ids = []; /** * @var object{site_id: int, domain: string, path: string} $blog */ foreach ( $it as $blog ) { ++$total; - $site_ids[] = $blog->site_id; - $url = $blog->domain . $blog->path; - $cmd = "--url={$url} core update-db"; + $url = $blog->domain . $blog->path; + $cmd = "--url={$url} core update-db"; if ( $dry_run ) { $cmd .= ' --dry-run'; } @@ -1381,9 +1379,7 @@ public function update_db( $args, $assoc_args ) { } } if ( ! $dry_run && $total && $success === $total ) { - foreach ( array_unique( $site_ids ) as $site_id ) { - update_metadata( 'site', $site_id, 'wpmu_upgrade_site', $wp_db_version ); - } + update_metadata( 'site', $network_id, 'wpmu_upgrade_site', $wp_db_version ); } WP_CLI::success( "WordPress database upgraded on {$success}/{$total} sites." ); } else {