Fix wp core update-db --network to respect network ID in multinetwork installations#302
Fix wp core update-db --network to respect network ID in multinetwork installations#302
wp core update-db --network to respect network ID in multinetwork installations#302Conversation
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>
wp core update-db --network to respect network ID in multinetwork installations
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 Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
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>
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
There was a problem hiding this comment.
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.
| 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} | ||
| """ |
There was a problem hiding this comment.
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
--urlparameter to target the second network. - Verify that
wp core update-db --networkonly 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.
Description
In multinetwork WordPress installations,
wp core update-db --networkqueries all blogs without filtering bysite_id, causing it to only update the first network regardless of the--urlparameter.Changes
Core Logic (
src/Core_Command.php)SITE_ID_CURRENT_SITEconstant (set by--url), falling back toget_current_network_id(), then network ID 1site_idfilter to TableIterator WHERE clause to scope blogs to current network$network_idinstead of collecting and deduplicating site IDsTest Coverage (
features/core-update-db.feature)Usage
Backward Compatibility
Standard multisite installations (single network) maintain existing behavior as network ID defaults to 1.
Fixes #198
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.