Skip to content

Fix wp core update-db --network to respect network ID in multinetwork installations#302

Open
Copilot wants to merge 8 commits intomainfrom
copilot/fix-multinetwork-update-db
Open

Fix wp core update-db --network to respect network ID in multinetwork installations#302
Copilot wants to merge 8 commits intomainfrom
copilot/fix-multinetwork-update-db

Conversation

Copy link
Contributor

Copilot AI commented Nov 7, 2025

Description

In multinetwork WordPress installations, wp core update-db --network queries all blogs without filtering by site_id, causing it to only update the first network regardless of the --url parameter.

Changes

Core Logic (src/Core_Command.php)

  • Detect current network ID from SITE_ID_CURRENT_SITE constant (set by --url), falling back to get_current_network_id(), then network ID 1
  • Add site_id filter to TableIterator WHERE clause to scope blogs to current network
  • Optimize metadata update to directly use $network_id instead of collecting and deduplicating site IDs
  • Add documentation example for multinetwork usage

Test Coverage (features/core-update-db.feature)

  • Add comprehensive test that downgrades WordPress and database versions, creates multiple sites, verifies network ID detection, executes the update-db command, and validates successful upgrade of all sites in the network

Usage

# Update sites in specific network
wp core update-db --network --url=network2.example.com

# Update primary network (backward compatible)
wp core update-db --network

Backward Compatibility

Standard multisite installations (single network) maintain existing behavior as network ID defaults to 1.


Fixes #198

Original prompt

This section details on the original issue you should resolve

<issue_title>wp core update-db --network only updates the first network for "multinetwork" install</issue_title>
<issue_description>Hello! I've been trying something but doesn't work fine.

I have a WordPress MultiNetwork installed (for a client) and I've been trying to do this command wp core update-db like a network, but doesn't update everything.

wp core update-db --network --path=/web/example.com/

This updates the "first" multisite, as expected.

But, there are 2 networks, and inside each one, there are some subsites. If I execute

wp core update-db --network --url="network1.com" --path=/web/example.com/

Or any other "url" it always executes the first multisite in the network.

I think there are 2 possibles solutions there. The first one (I think that will the best one) is to run

wp core update-db --network --path=/web/example.com/

and check all the subsites (whatever network they are). I think maybe check all the sites in the database active or not (because archived sites should also update the database tables).

The other case may be to use the --url parameter with the main hostname (in a multinetwork you have hostnames and then subfolders), so if you use the --url parameter, check al the sites for this network.</issue_description>

Comments on the Issue (you are @copilot in this section)

@danielbachhuber Thanks for the report, @javiercasares !

The other case may be to use the --url parameter with the main hostname (in a multinetwork you have hostnames and then subfolders), so if you use the --url parameter, check al the sites for this network.

I think this approach probably makes more sense.

Out of curiosity, how does WordPress core handle this for the web?</comment_new>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI self-assigned this Nov 7, 2025
Copilot AI and others added 2 commits November 7, 2025 22:23
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>
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>
Copilot AI changed the title [WIP] Fix wp core update-db for multiple networks Fix wp core update-db --network to respect network ID in multinetwork installations Nov 7, 2025
Copilot AI requested a review from swissspidy November 7, 2025 22:28
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>
@codecov
Copy link

codecov bot commented Nov 8, 2025

Codecov Report

❌ Patch coverage is 77.77778% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Core_Command.php 77.77% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@swissspidy swissspidy requested a review from Copilot November 8, 2025 09:41

This comment was marked as resolved.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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>
@swissspidy swissspidy marked this pull request as ready for review November 10, 2025 14:55
@swissspidy swissspidy requested a review from a team as a code owner November 10, 2025 14:55
@wojsmol

This comment was marked as resolved.

@swissspidy

This comment was marked as resolved.

@swissspidy

This comment was marked as resolved.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request fixes an issue with wp core update-db --network in multinetwork installations by correctly identifying and filtering by the target network ID. The logic changes in Core_Command.php are solid and correctly address the problem, including determining the network ID and optimizing the metadata update. The documentation has also been updated with a relevant example.

My main feedback is regarding the test coverage. While a new test scenario has been added, it only covers a standard single-network multisite setup. To ensure the fix is robust, the test should be expanded to cover a true multinetwork environment as detailed in my specific comment. Overall, this is a great fix that just needs more comprehensive testing to be complete.

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

3 participants