From d651de0dacc716631c3efe1c717e7a552fe64fc9 Mon Sep 17 00:00:00 2001 From: Dion Hulse Date: Fri, 3 Jul 2015 04:34:24 +0000 Subject: [PATCH] Introduce `wp_should_upgrade_global_tables()` to determine if global tables should be upgraded automatically. Use it throughout the existing upgrade routines. Props johnjamesjacoby for initial patch Fixes #32154 for trunk Built from https://develop.svn.wordpress.org/trunk@33057 git-svn-id: http://core.svn.wordpress.org/trunk@33028 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/upgrade.php | 66 +++++++++++++++++++++++++++++------ wp-includes/version.php | 2 +- 2 files changed, 57 insertions(+), 11 deletions(-) diff --git a/wp-admin/includes/upgrade.php b/wp-admin/includes/upgrade.php index fa1cf4984a..e428e31c3c 100644 --- a/wp-admin/includes/upgrade.php +++ b/wp-admin/includes/upgrade.php @@ -1252,7 +1252,7 @@ function upgrade_300() { add_site_option( 'siteurl', '' ); // 3.0 screen options key name changes. - if ( is_main_site() && !defined('DO_NOT_UPGRADE_GLOBAL_TABLES') ) { + if ( wp_should_upgrade_global_tables() ) { $sql = "DELETE FROM $wpdb->usermeta WHERE meta_key LIKE %s OR meta_key LIKE %s @@ -1292,7 +1292,7 @@ function upgrade_300() { function upgrade_330() { global $wp_current_db_version, $wpdb, $wp_registered_widgets, $sidebars_widgets; - if ( $wp_current_db_version < 19061 && is_main_site() && ! defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) { + if ( $wp_current_db_version < 19061 && wp_should_upgrade_global_tables() ) { $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key IN ('show_admin_bar_admin', 'plugins_last_view')" ); } @@ -1376,7 +1376,7 @@ function upgrade_340() { $wpdb->show_errors(); } - if ( $wp_current_db_version < 20022 && is_main_site() && ! defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) { + if ( $wp_current_db_version < 20022 && wp_should_upgrade_global_tables() ) { $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key = 'themes_last_view'" ); } @@ -1403,7 +1403,7 @@ function upgrade_350() { if ( $wp_current_db_version < 22006 && $wpdb->get_var( "SELECT link_id FROM $wpdb->links LIMIT 1" ) ) update_option( 'link_manager_enabled', 1 ); // Previously set to 0 by populate_options() - if ( $wp_current_db_version < 21811 && is_main_site() && ! defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) { + if ( $wp_current_db_version < 21811 && wp_should_upgrade_global_tables() ) { $meta_keys = array(); foreach ( array_merge( get_post_types(), get_taxonomies() ) as $name ) { if ( false !== strpos( $name, '-' ) ) @@ -1673,7 +1673,7 @@ function upgrade_network() { // 4.2 if ( $wp_current_db_version < 31351 && $wpdb->charset === 'utf8mb4' ) { - if ( ! ( defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) && DO_NOT_UPGRADE_GLOBAL_TABLES ) ) { + if ( wp_should_upgrade_global_tables() ) { $wpdb->query( "ALTER TABLE $wpdb->usermeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" ); $wpdb->query( "ALTER TABLE $wpdb->site DROP INDEX domain, ADD INDEX domain(domain(140),path(51))" ); $wpdb->query( "ALTER TABLE $wpdb->sitemeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" ); @@ -1689,7 +1689,7 @@ function upgrade_network() { // 4.3 if ( $wp_current_db_version < 33055 && 'utf8mb4' === $wpdb->charset ) { - if ( ! ( defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) && DO_NOT_UPGRADE_GLOBAL_TABLES ) ) { + if ( wp_should_upgrade_global_tables() ) { $upgrade = false; $indexes = $wpdb->get_results( "SHOW INDEXES FROM $wpdb->signups" ); foreach( $indexes as $index ) { @@ -2098,8 +2098,8 @@ function dbDelta( $queries = '', $execute = true ) { $global_tables = $wpdb->tables( 'global' ); foreach ( $cqueries as $table => $qry ) { - // Upgrade global tables only for the main site. Don't upgrade at all if DO_NOT_UPGRADE_GLOBAL_TABLES is defined. - if ( in_array( $table, $global_tables ) && ( !is_main_site() || defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) ) { + // Upgrade global tables only for the main site. Don't upgrade at all if conditions are not optimal. + if ( in_array( $table, $global_tables ) && ! wp_should_upgrade_global_tables() ) { unset( $cqueries[ $table ], $for_update[ $table ] ); continue; } @@ -2616,7 +2616,7 @@ function pre_schema_upgrade() { } // Multisite schema upgrades. - if ( $wp_current_db_version < 25448 && is_multisite() && ! defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) && is_main_network() ) { + if ( $wp_current_db_version < 25448 && is_multisite() && wp_should_upgrade_global_tables() ) { // Upgrade verions prior to 3.7 if ( $wp_current_db_version < 25179 ) { @@ -2634,7 +2634,7 @@ function pre_schema_upgrade() { // Upgrade versions prior to 4.2. if ( $wp_current_db_version < 31351 ) { - if ( ! is_multisite() ) { + if ( wp_should_upgrade_global_tables() ) { $wpdb->query( "ALTER TABLE $wpdb->usermeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" ); } $wpdb->query( "ALTER TABLE $wpdb->terms DROP INDEX slug, ADD INDEX slug(slug(191))" ); @@ -2671,3 +2671,49 @@ CREATE TABLE $wpdb->sitecategories ( dbDelta( $ms_queries ); } endif; + +/** + * Determine if global tables should be upgraded. + * + * This function performs a series of checks to ensure the environment allows + * for the safe upgrading of global WordPress database tables. It is necessary + * because global tables will commonly grow to millions of rows on large + * installations, and the ability to control their upgrade routines can be + * critical to the operation of large networks. + * + * In a future iteration, this function may use `wp_is_large_network()` to more- + * intelligently prevent global table upgrades. Until then, we make sure + * WordPress is on the main site of the main network, to avoid running queries + * more than once in multi-site or multi-network environments. + * + * @since 4.3.0 + * + * @return bool Whether to run the upgrade routines on global tables. + */ +function wp_should_upgrade_global_tables() { + + // Return false early if explicitly not upgrading + if ( defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) { + return false; + } + + // Assume global tables should be upgraded + $should_upgrade = true; + + // Set to false if not on main network (does not matter if not multi-network) + if ( ! is_main_network() ) { + $should_upgrade = false; + } + + // Set to false if not on main site of current network (does not matter if not multi-site) + if ( ! is_main_site() ) { + $should_upgrade = false; + } + + /** + * Filter if upgrade routines should be run on global tables in multisite. + * + * @param bool $should_upgrade Whether to run the upgrade routines on global tables. + */ + return apply_filters( 'wp_should_upgrade_global_tables', $should_upgrade ); +} diff --git a/wp-includes/version.php b/wp-includes/version.php index 689a754b09..96a52cbd39 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.3-beta1-33056'; +$wp_version = '4.3-beta1-33057'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.