Skip to content

Commit 246f8a0

Browse files
Copilotswissspidy
andcommitted
Add wp core check-update-db command
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 2ec53de commit 246f8a0

3 files changed

Lines changed: 203 additions & 0 deletions

File tree

‎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: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
# This test downgrades to an older WordPress version, but the SQLite plugin requires 6.0+
47+
@require-mysql
48+
Scenario: Check if database update is needed across network
49+
Given a WP multisite install
50+
And a disable_sidebar_check.php file:
51+
"""
52+
<?php
53+
WP_CLI::add_wp_hook( 'init', static function () {
54+
remove_action( 'after_switch_theme', '_wp_sidebars_changed' );
55+
} );
56+
"""
57+
And I try `wp theme install twentytwenty --activate`
58+
And I run `wp core download --version=5.4 --force`
59+
And I run `wp option update db_version 45805 --require=disable_sidebar_check.php`
60+
And I run `wp site option update wpmu_upgrade_site 45805`
61+
And I run `wp site create --slug=foo`
62+
And I run `wp site create --slug=bar`
63+
And I run `wp site create --slug=burrito --porcelain`
64+
And save STDOUT as {BURRITO_ID}
65+
And I run `wp site create --slug=taco --porcelain`
66+
And save STDOUT as {TACO_ID}
67+
And I run `wp site create --slug=pizza --porcelain`
68+
And save STDOUT as {PIZZA_ID}
69+
And I run `wp site archive {BURRITO_ID}`
70+
And I run `wp site spam {TACO_ID}`
71+
And I run `wp site delete {PIZZA_ID} --yes`
72+
73+
When I try `wp core check-update-db --network`
74+
Then the return code should be 1
75+
And STDOUT should contain:
76+
"""
77+
WordPress database update needed on 3/3 sites:
78+
"""
79+
80+
When I run `wp core update-db --network`
81+
Then STDOUT should contain:
82+
"""
83+
Success: WordPress database upgraded on 3/3 sites.
84+
"""
85+
86+
When I run `wp core check-update-db --network`
87+
Then STDOUT should contain:
88+
"""
89+
Success: WordPress databases are up to date on 3/3 sites.
90+
"""
91+
92+
Scenario: Check database update on network installation errors on single site
93+
Given a WP install
94+
95+
When I try `wp core check-update-db --network`
96+
Then STDERR should contain:
97+
"""
98+
Error: This is not a multisite installation.
99+
"""
100+
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
@@ -1244,6 +1244,108 @@ public function update( $args, $assoc_args ) {
12441244
}
12451245
}
12461246

1247+
/**
1248+
* Checks for the need for WordPress database updates.
1249+
*
1250+
* Compares the current database version with the version required by WordPress core
1251+
* to determine if database updates are needed.
1252+
*
1253+
* ## OPTIONS
1254+
*
1255+
* [--network]
1256+
* : Check databases for all sites on a network.
1257+
*
1258+
* ## EXAMPLES
1259+
*
1260+
* # Check if database update is needed
1261+
* $ wp core check-update-db
1262+
* Success: WordPress database is up to date.
1263+
*
1264+
* # Check database update status for all sites on a network
1265+
* $ wp core check-update-db --network
1266+
* Success: WordPress databases are up to date on 5/5 sites.
1267+
*
1268+
* @subcommand check-update-db
1269+
*
1270+
* @param string[] $args Positional arguments. Unused.
1271+
* @param array{network?: bool} $assoc_args Associative arguments.
1272+
*/
1273+
public function check_update_db( $args, $assoc_args ) {
1274+
global $wpdb, $wp_db_version, $wp_current_db_version;
1275+
1276+
$network = Utils\get_flag_value( $assoc_args, 'network' );
1277+
if ( $network && ! is_multisite() ) {
1278+
WP_CLI::error( 'This is not a multisite installation.' );
1279+
}
1280+
1281+
if ( $network ) {
1282+
$iterator_args = [
1283+
'table' => $wpdb->blogs,
1284+
'where' => [
1285+
'spam' => 0,
1286+
'deleted' => 0,
1287+
'archived' => 0,
1288+
],
1289+
];
1290+
$it = new TableIterator( $iterator_args );
1291+
$total = 0;
1292+
$needs_update = 0;
1293+
$sites_needing_update = [];
1294+
1295+
/**
1296+
* @var object{site_id: int, domain: string, path: string} $blog
1297+
*/
1298+
foreach ( $it as $blog ) {
1299+
++$total;
1300+
$url = $blog->domain . $blog->path;
1301+
$cmd = "--url={$url} core check-update-db";
1302+
1303+
/**
1304+
* @var object{stdout: string, stderr: string, return_code: int} $process
1305+
*/
1306+
$process = WP_CLI::runcommand(
1307+
$cmd,
1308+
[
1309+
'return' => 'all',
1310+
'exit_error' => false,
1311+
]
1312+
);
1313+
// If return code is 1, it means update is needed
1314+
if ( 1 === (int) $process->return_code ) {
1315+
++$needs_update;
1316+
$sites_needing_update[] = $url;
1317+
}
1318+
}
1319+
1320+
if ( $needs_update > 0 ) {
1321+
WP_CLI::log( "WordPress database update needed on {$needs_update}/{$total} sites:" );
1322+
foreach ( $sites_needing_update as $site_url ) {
1323+
WP_CLI::log( " - {$site_url}" );
1324+
}
1325+
WP_CLI::halt( 1 );
1326+
} else {
1327+
WP_CLI::success( "WordPress databases are up to date on {$total}/{$total} sites." );
1328+
}
1329+
} else {
1330+
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
1331+
1332+
/**
1333+
* @var string $wp_current_db_version
1334+
*/
1335+
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Replacing WP Core behavior is the goal here.
1336+
$wp_current_db_version = __get_option( 'db_version' );
1337+
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Replacing WP Core behavior is the goal here.
1338+
$wp_current_db_version = (int) $wp_current_db_version;
1339+
1340+
if ( $wp_db_version !== $wp_current_db_version ) {
1341+
WP_CLI::log( "WordPress database update required from db version {$wp_current_db_version} to {$wp_db_version}." );
1342+
WP_CLI::halt( 1 );
1343+
} else {
1344+
WP_CLI::success( 'WordPress database is up to date.' );
1345+
}
1346+
}
1347+
}
1348+
12471349
/**
12481350
* Runs the WordPress database update procedure.
12491351
*

0 commit comments

Comments
 (0)