diff --git a/composer.json b/composer.json index 6a3fe712..7cfe8726 100644 --- a/composer.json +++ b/composer.json @@ -40,6 +40,7 @@ "commands": [ "core", "core check-update", + "core check-update-db", "core download", "core install", "core is-installed", diff --git a/features/core-check-update-db.feature b/features/core-check-update-db.feature new file mode 100644 index 00000000..b8e3bd41 --- /dev/null +++ b/features/core-check-update-db.feature @@ -0,0 +1,99 @@ +Feature: Check if WordPress database update is needed + + # This test downgrades to an older WordPress version, but the SQLite plugin requires 6.0+ + @require-mysql + Scenario: Check if database update is needed on a single site + Given a WP install + And a disable_sidebar_check.php file: + """ + $wpdb->blogs, + 'where' => [ + 'spam' => 0, + 'deleted' => 0, + 'archived' => 0, + ], + ]; + $it = new TableIterator( $iterator_args ); + $total = 0; + $needs_update = 0; + $sites_needing_update = []; + + /** + * @var object{site_id: int, domain: string, path: string} $blog + */ + foreach ( $it as $blog ) { + ++$total; + $url = $blog->domain . $blog->path; + $cmd = "--url={$url} core check-update-db"; + + /** + * @var object{stdout: string, stderr: string, return_code: int} $process + */ + $process = WP_CLI::runcommand( + $cmd, + [ + 'return' => 'all', + 'exit_error' => false, + ] + ); + // If return code is 1, it means update is needed + if ( 1 === (int) $process->return_code ) { + ++$needs_update; + $sites_needing_update[] = $url; + } + } + + if ( $needs_update > 0 ) { + WP_CLI::log( "WordPress database update needed on {$needs_update}/{$total} sites:" ); + foreach ( $sites_needing_update as $site_url ) { + WP_CLI::log( " - {$site_url}" ); + } + WP_CLI::halt( 1 ); + } else { + WP_CLI::success( "WordPress databases are up to date on {$total}/{$total} sites." ); + } + } else { + require_once ABSPATH . 'wp-admin/includes/upgrade.php'; + + /** + * @var string $wp_current_db_version + */ + // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Replacing WP Core behavior is the goal here. + $wp_current_db_version = __get_option( 'db_version' ); + // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Replacing WP Core behavior is the goal here. + $wp_current_db_version = (int) $wp_current_db_version; + + if ( $wp_db_version !== $wp_current_db_version ) { + WP_CLI::log( "WordPress database update required from db version {$wp_current_db_version} to {$wp_db_version}." ); + WP_CLI::halt( 1 ); + } else { + WP_CLI::success( 'WordPress database is up to date.' ); + } + } + } + /** * Runs the WordPress database update procedure. *