Skip to content

Commit d447bc4

Browse files
Copilotswissspidy
andauthored
Add wp core check-update-db command (#294)
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascalb@google.com>
1 parent c8fd1ed commit d447bc4

File tree

3 files changed

+202
-0
lines changed

3 files changed

+202
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"commands": [
4141
"core",
4242
"core check-update",
43+
"core check-update-db",
4344
"core download",
4445
"core install",
4546
"core is-installed",
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
Feature: Check if WordPress database update is needed
2+
3+
# This test downgrades to an older WordPress version, but the SQLite plugin requires 6.0+
4+
@require-mysql
5+
Scenario: Check if database update is needed on a single site
6+
Given a WP install
7+
And a disable_sidebar_check.php file:
8+
"""
9+
<?php
10+
WP_CLI::add_wp_hook( 'init', static function () {
11+
remove_action( 'after_switch_theme', '_wp_sidebars_changed' );
12+
} );
13+
"""
14+
And I try `wp theme install twentytwenty --activate`
15+
And I run `wp core download --version=5.4 --force`
16+
And I run `wp option update db_version 45805 --require=disable_sidebar_check.php`
17+
18+
When I try `wp core check-update-db`
19+
Then the return code should be 1
20+
And STDOUT should contain:
21+
"""
22+
WordPress database update required from db version 45805 to 47018.
23+
"""
24+
25+
When I run `wp core update-db`
26+
Then STDOUT should contain:
27+
"""
28+
Success: WordPress database upgraded successfully from db version 45805 to 47018.
29+
"""
30+
31+
When I run `wp core check-update-db`
32+
Then STDOUT should contain:
33+
"""
34+
Success: WordPress database is up to date.
35+
"""
36+
37+
Scenario: Check if database update is needed when database is already up to date
38+
Given a WP install
39+
40+
When I run `wp core check-update-db`
41+
Then STDOUT should contain:
42+
"""
43+
Success: WordPress database is up to date.
44+
"""
45+
46+
Scenario: Check if database update is needed across network
47+
Given a WP multisite install
48+
And a disable_sidebar_check.php file:
49+
"""
50+
<?php
51+
WP_CLI::add_wp_hook( 'init', static function () {
52+
remove_action( 'after_switch_theme', '_wp_sidebars_changed' );
53+
} );
54+
"""
55+
And I try `wp theme install twentytwenty --activate`
56+
And I run `wp core download --version=6.6 --force`
57+
And I run `wp option update db_version 57155 --require=disable_sidebar_check.php`
58+
And I run `wp site option update wpmu_upgrade_site 57155`
59+
And I run `wp site create --slug=foo`
60+
And I run `wp site create --slug=bar`
61+
And I run `wp site create --slug=burrito --porcelain`
62+
And save STDOUT as {BURRITO_ID}
63+
And I run `wp site create --slug=taco --porcelain`
64+
And save STDOUT as {TACO_ID}
65+
And I run `wp site create --slug=pizza --porcelain`
66+
And save STDOUT as {PIZZA_ID}
67+
And I run `wp site archive {BURRITO_ID}`
68+
And I run `wp site spam {TACO_ID}`
69+
And I run `wp site delete {PIZZA_ID} --yes`
70+
And I run `wp core update`
71+
72+
When I try `wp core check-update-db --network`
73+
Then the return code should be 1
74+
And STDOUT should contain:
75+
"""
76+
WordPress database update needed on 3/3 sites:
77+
"""
78+
79+
When I run `wp core update-db --network`
80+
Then STDOUT should contain:
81+
"""
82+
Success: WordPress database upgraded on 3/3 sites.
83+
"""
84+
85+
When I run `wp core check-update-db --network`
86+
Then STDOUT should contain:
87+
"""
88+
Success: WordPress databases are up to date on 3/3 sites.
89+
"""
90+
91+
Scenario: Check database update on network installation errors on single site
92+
Given a WP install
93+
94+
When I try `wp core check-update-db --network`
95+
Then STDERR should contain:
96+
"""
97+
Error: This is not a multisite installation.
98+
"""
99+
And the return code should be 1

src/Core_Command.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,108 @@ static function () {
13151315
}
13161316
}
13171317

1318+
/**
1319+
* Checks for the need for WordPress database updates.
1320+
*
1321+
* Compares the current database version with the version required by WordPress core
1322+
* to determine if database updates are needed.
1323+
*
1324+
* ## OPTIONS
1325+
*
1326+
* [--network]
1327+
* : Check databases for all sites on a network.
1328+
*
1329+
* ## EXAMPLES
1330+
*
1331+
* # Check if database update is needed
1332+
* $ wp core check-update-db
1333+
* Success: WordPress database is up to date.
1334+
*
1335+
* # Check database update status for all sites on a network
1336+
* $ wp core check-update-db --network
1337+
* Success: WordPress databases are up to date on 5/5 sites.
1338+
*
1339+
* @subcommand check-update-db
1340+
*
1341+
* @param string[] $args Positional arguments. Unused.
1342+
* @param array{network?: bool} $assoc_args Associative arguments.
1343+
*/
1344+
public function check_update_db( $args, $assoc_args ) {
1345+
global $wpdb, $wp_db_version, $wp_current_db_version;
1346+
1347+
$network = Utils\get_flag_value( $assoc_args, 'network' );
1348+
if ( $network && ! is_multisite() ) {
1349+
WP_CLI::error( 'This is not a multisite installation.' );
1350+
}
1351+
1352+
if ( $network ) {
1353+
$iterator_args = [
1354+
'table' => $wpdb->blogs,
1355+
'where' => [
1356+
'spam' => 0,
1357+
'deleted' => 0,
1358+
'archived' => 0,
1359+
],
1360+
];
1361+
$it = new TableIterator( $iterator_args );
1362+
$total = 0;
1363+
$needs_update = 0;
1364+
$sites_needing_update = [];
1365+
1366+
/**
1367+
* @var object{site_id: int, domain: string, path: string} $blog
1368+
*/
1369+
foreach ( $it as $blog ) {
1370+
++$total;
1371+
$url = $blog->domain . $blog->path;
1372+
$cmd = "--url={$url} core check-update-db";
1373+
1374+
/**
1375+
* @var object{stdout: string, stderr: string, return_code: int} $process
1376+
*/
1377+
$process = WP_CLI::runcommand(
1378+
$cmd,
1379+
[
1380+
'return' => 'all',
1381+
'exit_error' => false,
1382+
]
1383+
);
1384+
// If return code is 1, it means update is needed
1385+
if ( 1 === (int) $process->return_code ) {
1386+
++$needs_update;
1387+
$sites_needing_update[] = $url;
1388+
}
1389+
}
1390+
1391+
if ( $needs_update > 0 ) {
1392+
WP_CLI::log( "WordPress database update needed on {$needs_update}/{$total} sites:" );
1393+
foreach ( $sites_needing_update as $site_url ) {
1394+
WP_CLI::log( " - {$site_url}" );
1395+
}
1396+
WP_CLI::halt( 1 );
1397+
} else {
1398+
WP_CLI::success( "WordPress databases are up to date on {$total}/{$total} sites." );
1399+
}
1400+
} else {
1401+
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
1402+
1403+
/**
1404+
* @var string $wp_current_db_version
1405+
*/
1406+
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Replacing WP Core behavior is the goal here.
1407+
$wp_current_db_version = __get_option( 'db_version' );
1408+
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Replacing WP Core behavior is the goal here.
1409+
$wp_current_db_version = (int) $wp_current_db_version;
1410+
1411+
if ( $wp_db_version !== $wp_current_db_version ) {
1412+
WP_CLI::log( "WordPress database update required from db version {$wp_current_db_version} to {$wp_db_version}." );
1413+
WP_CLI::halt( 1 );
1414+
} else {
1415+
WP_CLI::success( 'WordPress database is up to date.' );
1416+
}
1417+
}
1418+
}
1419+
13181420
/**
13191421
* Runs the WordPress database update procedure.
13201422
*

0 commit comments

Comments
 (0)