Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions features/core-update-db.feature
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,45 @@ 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:
"""
<?php
WP_CLI::add_wp_hook( 'init', static function () {
remove_action( 'after_switch_theme', '_wp_sidebars_changed' );
} );
"""
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`

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}
"""
Comment on lines +156 to +191

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The new test scenario is titled "Update db respects current network in multinetwork setup", but the test itself only sets up a standard single-network multisite installation. While this is a good regression test to ensure the command still works for standard multisite, it doesn't verify the core of the fix, which is to correctly target a specific network in a true multinetwork environment (with multiple networks).

To make the test more comprehensive and accurately reflect its name, it should be updated to:

  • Create a multinetwork environment with at least two distinct networks.
  • Create sites within each network.
  • Use the --url parameter to target the second network.
  • Verify that wp core update-db --network only updates the sites within the specified network, leaving the other network's sites untouched.

This would provide confidence that the fix works as intended for the multinetwork scenario described in the pull request.


Scenario: Ensure update-db sets WP_INSTALLING constant
Given a WP install
And a before.php file:
Expand Down
26 changes: 19 additions & 7 deletions src/Core_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,11 @@ static function () {
* 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.
Expand All @@ -1329,9 +1334,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 ( ! $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,
Expand All @@ -1340,16 +1356,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';
}
Expand Down Expand Up @@ -1379,9 +1393,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 {
Expand Down