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}
"""

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 @@ -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.
Expand All @@ -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 ( ! $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 @@ -1326,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';
}
Expand Down Expand Up @@ -1365,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 {
Expand Down
Loading