Skip to content

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

Open
Copilot wants to merge 12 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 12 commits intomainfrom
copilot/fix-multinetwork-update-db

Conversation

Copy link
Contributor

Copilot AI commented Nov 7, 2025

  • Understand the current implementation and issue
  • Filter sites by the current network's site_id
  • Fix get_current_network_id() to be called first
  • Update documentation with multinetwork example
  • Add proper two-network test
  • Fix CI failure: correct approach for verifying multinetwork isolation

Root cause of the persistent CI failure

The SITE_ID_CURRENT_SITE constant swap in wp-config.php cannot change the active network context because wp-config.php also hardcodes BLOG_ID_CURRENT_SITE=1. During the WordPress multisite bootstrap, ms_load_current_site_and_network() detects that blog 1's site_id (1) does not match the SITE_ID_CURRENT_SITE value (2) and overrides $current_site back to network 1. get_current_network_id() therefore always returns 1, regardless of the SITE_ID_CURRENT_SITE change.

New test strategy

Rather than switching network context (which requires changing both SITE_ID_CURRENT_SITE and BLOG_ID_CURRENT_SITE to values we don't know statically), the test proves the filtering works by:

  1. Setup: 5 sites total — 3 in network 1 (active, belong to the current context) and 2 in network 2 (moved via direct DB update)
  2. Assert 3/3 not 5/5: Without the site_id filter, the old code would process all 5 active sites (5/5). Seeing 3/3 proves the filter is active.
  3. Assert net2-site1 absent from STDOUT: Explicitly proves the network-2 site was not touched.

This is a valid regression test for the fix: it would fail on the un-patched code.

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 added the command:core-update-db Related to 'core update-db' command label Nov 8, 2025
@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.

gemini-code-assist[bot]

This comment was marked as resolved.

@swissspidy

This comment was marked as resolved.

@swissspidy swissspidy changed the title Fix wp core update-db --network to respect network ID in multinetwork installations Fix wp core update-db --network to respect network ID in multi-network installations Mar 10, 2026
- Fix update_db to call get_current_network_id() first (WP 4.9+) so that
  the --url parameter actually determines the target network. Previously,
  SITE_ID_CURRENT_SITE (always 1 in wp-config.php) was checked first,
  making the fallback to get_current_network_id() unreachable.
- Replace the simplistic scenario with a proper two-network setup:
  creates sites in both networks, verifies the primary network run only
  upgrades its 3 sites, then verifies --url targets network 2 and upgrades
  its 2 sites.

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

This comment was marked as resolved.

Copilot AI changed the title Fix wp core update-db --network to respect network ID in multi-network installations Fix wp core update-db --network to respect network ID in multinetwork installations Mar 10, 2026
@swissspidy

This comment was marked as resolved.

Two bugs in the previous test:
1. Wrong domain: network 2 was created with domain=localhost but the
   Behat test environment installs WordPress at https://example.com.
2. URL routing doesn't work: DOMAIN_CURRENT_SITE is a PHP constant
   hardcoded in wp-config.php, so WordPress skips the DB lookup entirely
   and get_current_network_id() always returns SITE_ID_CURRENT_SITE (1)
   regardless of --url. The --url=localhost/net2-site1/ approach could
   never route to network 2.

Fix: create network 2 with domain=example.com, move 2 blogs to it, then
swap SITE_ID_CURRENT_SITE from 1 to 2 in wp-config.php before the second
run. This correctly puts WordPress into network 2 context so the command
processes only the 2 network-2 sites.

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

This comment was marked as resolved.

@swissspidy
Copy link
Member

@copilot still failing, the update appears to be running on 3 sites, not 2.

001 Scenario: Update db targets only the specified network in multinetwork setup # features/core-update-db.feature:156
      Then STDOUT should contain:                                                # features/core-update-db.feature:201
        $ wp core update-db --network
        WordPress database already at latest db version 47018 on example.com/
        WordPress database already at latest db version 47018 on example.com/net1-site1/
        WordPress database already at latest db version 47018 on example.com/net1-site2/
        Success: WordPress database upgraded on 3/3 sites.
        
        cwd: /tmp/wp-cli-test-run--69b038d7460435.28429357/
        run time: 0.82551288604736
        exit status: 0
        
        --- Expected
        +++ Actual
        @@ @@
        -Success: WordPress database upgraded on 2/2 sites.
        +WordPress database already at latest db version 47018 on example.com/
        +WordPress database already at latest db version 47018 on example.com/net1-site1/
        +WordPress database already at latest db version 47018 on example.com/net1-site2/
        +Success: WordPress database upgraded on 3/3 sites.

The wp-config.php SITE_ID_CURRENT_SITE swap approach cannot change the
network context because WordPress also reads BLOG_ID_CURRENT_SITE=1
(hardcoded, can't be changed without knowing the network-2 blog ID), and
since blog 1 belongs to network 1, WordPress overrides $current_site back
to network 1 regardless of SITE_ID_CURRENT_SITE.

Instead, verify the site_id filter works by:
1. Creating 5 active sites (3 in network 1, 2 in network 2)
2. Running wp core update-db --network and asserting "3/3 sites" not "5/5"
3. Asserting that net2-site1 URL is absent from output

This is a true regression test: without the site_id filter the old code
would have processed all 5 sites (5/5), and network-2 URLs would appear.

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

This comment was marked as resolved.

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

Labels

command:core-update-db Related to 'core update-db' command

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wp core update-db --network only updates the first network for "multinetwork" install

4 participants