Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 109 additions & 12 deletions src/wp-admin/includes/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -655,9 +655,14 @@ function wp_new_blog_notification(
* Upgrades the database if needed during a site update.
*
* @since 2.1.0
* @since 7.3.0 Returns a `WP_Error` object, and leaves the database version unchanged,
* if database queries failed during the upgrade.
*
* @global int $wp_current_db_version The old (current) database version.
* @global int $wp_db_version The new database version.
*
* @return true|WP_Error True if the database was upgraded or no upgrade was needed,
* WP_Error if database queries failed during the upgrade.
*/
function wp_upgrade() {
global $wp_current_db_version, $wp_db_version;
Expand All @@ -666,15 +671,21 @@ function wp_upgrade() {

// We are up to date. Nothing to do.
if ( $wp_db_version === $wp_current_db_version ) {
return;
return true;
}

if ( ! is_blog_installed() ) {
return;
return true;
}

$old_db_version = $wp_current_db_version;

wp_check_mysql_version();
wp_cache_flush();

// Start keeping track of database errors, so a failed upgrade can be detected.
_wp_upgrade_db_errors( true );

pre_schema_upgrade();
make_db_current_silent();
upgrade_all();
Expand All @@ -683,6 +694,31 @@ function wp_upgrade() {
}
wp_cache_flush();

$db_errors = _wp_upgrade_db_errors();

if ( $db_errors ) {
/*
* The network upgrade runs after upgrade_all(), which may have already
* recorded the new database version. Make sure the upgrade runs again.
*/
if ( (int) __get_option( 'db_version' ) === $wp_db_version ) {
update_option( 'db_version', $old_db_version );
update_option( 'db_upgraded', false );
wp_cache_flush();
}

$error = new WP_Error(
'db_upgrade_failed',
__( 'The database could not be updated because one or more database queries failed.' )
);

foreach ( $db_errors as $db_error ) {
$error->add( 'db_upgrade_error', $db_error['error_str'], $db_error );
}

return $error;
}

if ( is_multisite() ) {
update_site_meta( get_current_blog_id(), 'db_version', $wp_db_version );
update_site_meta( get_current_blog_id(), 'db_last_updated', microtime() );
Expand All @@ -699,9 +735,62 @@ function wp_upgrade() {
* @param int $wp_current_db_version The old (current) $wp_db_version.
*/
do_action( 'wp_upgrade', $wp_db_version, $wp_current_db_version );

return true;
}
endif;

/**
* Tracks the database errors that occur while the database is being upgraded.
*
* Errors raised while error suppression is turned on via wpdb::suppress_errors()
* are expected failures, for example dropping a column that may already be gone,
* and are not reported.
*
* @since 7.3.0
* @access private
*
* @global array $EZSQL_ERROR Stores error information of query and error string.
*
* @param bool $start Optional. Whether to start tracking errors from this point on.
* Default false.
* @return array[] {
* Database errors recorded since tracking started. An empty array when
* tracking has not been started.
*
* @type array ...$0 {
* @type string $query The query that failed.
* @type string $error_str The error message.
* @type bool $suppressed Whether errors were suppressed. Always false here.
* }
* }
*/
function _wp_upgrade_db_errors( $start = false ) {
global $EZSQL_ERROR;
static $offset = null;

$errors = is_array( $EZSQL_ERROR ) ? $EZSQL_ERROR : array();

if ( $start ) {
$offset = count( $errors );
return array();
}

if ( null === $offset ) {
return array();
}

$errors = array_slice( $errors, $offset );

foreach ( $errors as $key => $error ) {
if ( ! empty( $error['suppressed'] ) ) {
unset( $errors[ $key ] );
}
}

return array_values( $errors );
}

/**
* Functions to be called in installation and upgrade scripts.
*
Expand Down Expand Up @@ -896,6 +985,14 @@ function upgrade_all() {

maybe_disable_automattic_widgets();

/*
* If any of the database queries failed, leave the database version alone,
* so the upgrade runs again once the cause of the failure is resolved.
*/
if ( _wp_upgrade_db_errors() ) {
return;
}

update_option( 'db_version', $wp_db_version );
update_option( 'db_upgraded', true );
}
Expand Down Expand Up @@ -1212,11 +1309,11 @@ function upgrade_160() {

endforeach;
$old_user_fields = array( 'user_firstname', 'user_lastname', 'user_icq', 'user_aim', 'user_msn', 'user_yim', 'user_idmode', 'user_ip', 'user_domain', 'user_browser', 'user_description', 'user_nickname', 'user_level' );
$wpdb->hide_errors();
$suppress = $wpdb->suppress_errors();
foreach ( $old_user_fields as $old ) {
$wpdb->query( "ALTER TABLE $wpdb->users DROP $old" );
}
$wpdb->show_errors();
$wpdb->suppress_errors( $suppress );

// Populate comment_count field of posts table.
$comments = $wpdb->get_results( "SELECT comment_post_ID, COUNT(*) as c FROM $wpdb->comments WHERE comment_approved = '1' GROUP BY comment_post_ID" );
Expand Down Expand Up @@ -1541,11 +1638,11 @@ function upgrade_230() {
function upgrade_230_options_table() {
global $wpdb;
$old_options_fields = array( 'option_can_override', 'option_type', 'option_width', 'option_height', 'option_description', 'option_admin_level' );
$wpdb->hide_errors();
$suppress = $wpdb->suppress_errors();
foreach ( $old_options_fields as $old ) {
$wpdb->query( "ALTER TABLE $wpdb->options DROP $old" );
}
$wpdb->show_errors();
$wpdb->suppress_errors( $suppress );
}

/**
Expand Down Expand Up @@ -1848,15 +1945,15 @@ function upgrade_340() {
global $wp_current_db_version, $wpdb;

if ( $wp_current_db_version < 19798 ) {
$wpdb->hide_errors();
$suppress = $wpdb->suppress_errors();
$wpdb->query( "ALTER TABLE $wpdb->options DROP COLUMN blog_id" );
$wpdb->show_errors();
$wpdb->suppress_errors( $suppress );
}

if ( $wp_current_db_version < 19799 ) {
$wpdb->hide_errors();
$suppress = $wpdb->suppress_errors();
$wpdb->query( "ALTER TABLE $wpdb->comments DROP INDEX comment_approved" );
$wpdb->show_errors();
$wpdb->suppress_errors( $suppress );
}

if ( $wp_current_db_version < 20022 && wp_should_upgrade_global_tables() ) {
Expand Down Expand Up @@ -2713,7 +2810,7 @@ function maybe_create_table( $table_name, $create_ddl ) {
function drop_index( $table, $index ) {
global $wpdb;

$wpdb->hide_errors();
$suppress = $wpdb->suppress_errors();

$wpdb->query( "ALTER TABLE `$table` DROP INDEX `$index`" );

Expand All @@ -2722,7 +2819,7 @@ function drop_index( $table, $index ) {
$wpdb->query( "ALTER TABLE `$table` DROP INDEX `{$index}_$i`" );
}

$wpdb->show_errors();
$wpdb->suppress_errors( $suppress );

return true;
}
Expand Down
10 changes: 10 additions & 0 deletions src/wp-admin/network/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@
);
}

if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
wp_die(
sprintf(
/* translators: %s: Site URL. */
__( 'Warning! Problem updating %s. The database update did not complete successfully. Check the error log of your server for details.' ),
$siteurl
)
);
}

/**
* Fires after the Multisite DB upgrade for each site is complete.
*
Expand Down
32 changes: 30 additions & 2 deletions src/wp-admin/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@

// Do it. No output.
if ( 'upgrade_db' === $step ) {
wp_upgrade();
if ( is_wp_error( wp_upgrade() ) ) {
status_header( 500 );
die( '1' );
}

die( '0' );
}

Expand Down Expand Up @@ -159,11 +163,35 @@
<?php
break;
case 1:
wp_upgrade();
$upgraded = wp_upgrade();

$backto = ! empty( $_GET['backto'] ) ? wp_unslash( urldecode( $_GET['backto'] ) ) : __get_option( 'home' ) . '/';
$backto = esc_url( $backto );
$backto = wp_validate_redirect( $backto, __get_option( 'home' ) . '/' );

if ( is_wp_error( $upgraded ) ) :
$error_count = count( $upgraded->get_error_messages( 'db_upgrade_error' ) );
?>
<h1><?php _e( 'Database Update Failed' ); ?></h1>
<p><?php _e( 'Your WordPress database could not be updated.' ); ?></p>
<p>
<?php
printf(
/* translators: %s: Number of failed database queries. */
_n(
'%s database query failed. The details have been written to the error log of your server.',
'%s database queries failed. The details have been written to the error log of your server.',
$error_count
),
number_format_i18n( $error_count )
);
?>
</p>
<p><?php _e( 'Once the cause is resolved, you can run the database update again. If the problem persists, please contact your hosting provider.' ); ?></p>
<p class="step"><a class="button button-large button-primary" href="upgrade.php?step=1&amp;backto=<?php echo urlencode( $backto ); ?>"><?php _e( 'Try Again' ); ?></a></p>
<?php
break;
endif;
?>
<h1><?php _e( 'Update Complete' ); ?></h1>
<p><?php _e( 'Your WordPress database has been successfully updated!' ); ?></p>
Expand Down
6 changes: 4 additions & 2 deletions src/wp-includes/class-wpdb.php
Original file line number Diff line number Diff line change
Expand Up @@ -1818,6 +1818,7 @@ public function esc_like( $text ) {
* Prints SQL/DB error.
*
* @since 0.71
* @since 7.3.0 The `$EZSQL_ERROR` entries record whether errors were suppressed.
*
* @global array $EZSQL_ERROR Stores error information of query and error string.
*
Expand All @@ -1832,8 +1833,9 @@ public function print_error( $str = '' ) {
}

$EZSQL_ERROR[] = array(
'query' => $this->last_query,
'error_str' => $str,
'query' => $this->last_query,
'error_str' => $str,
'suppressed' => $this->suppress_errors,
);

if ( $this->suppress_errors ) {
Expand Down
Loading