WordPress.org

Make WordPress Core

Ticket #32154: 32154.02.patch

File 32154.02.patch, 5.5 KB (added by johnjamesjacoby, 7 years ago)

Introduce wp_can_upgrade_global_tables()

  • wp-admin/includes/upgrade.php

     
    12081208                add_site_option( 'siteurl', '' );
    12091209
    12101210        // 3.0 screen options key name changes.
    1211         if ( is_main_site() && !defined('DO_NOT_UPGRADE_GLOBAL_TABLES') ) {
     1211        if ( wp_can_upgrade_global_tables() ) {
    12121212                $sql = "DELETE FROM $wpdb->usermeta
    12131213                        WHERE meta_key LIKE %s
    12141214                        OR meta_key LIKE %s
     
    12431243function upgrade_330() {
    12441244        global $wp_current_db_version, $wpdb, $wp_registered_widgets, $sidebars_widgets;
    12451245
    1246         if ( $wp_current_db_version < 19061 && is_main_site() && ! defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) {
     1246        if ( $wp_current_db_version < 19061 && wp_can_upgrade_global_tables() ) {
    12471247                $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key IN ('show_admin_bar_admin', 'plugins_last_view')" );
    12481248        }
    12491249
     
    13241324                $wpdb->show_errors();
    13251325        }
    13261326
    1327         if ( $wp_current_db_version < 20022 && is_main_site() && ! defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) {
     1327        if ( $wp_current_db_version < 20022 && wp_can_upgrade_global_tables() ) {
    13281328                $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key = 'themes_last_view'" );
    13291329        }
    13301330
     
    13481348        if ( $wp_current_db_version < 22006 && $wpdb->get_var( "SELECT link_id FROM $wpdb->links LIMIT 1" ) )
    13491349                update_option( 'link_manager_enabled', 1 ); // Previously set to 0 by populate_options()
    13501350
    1351         if ( $wp_current_db_version < 21811 && is_main_site() && ! defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) {
     1351        if ( $wp_current_db_version < 21811 && wp_can_upgrade_global_tables() ) {
    13521352                $meta_keys = array();
    13531353                foreach ( array_merge( get_post_types(), get_taxonomies() ) as $name ) {
    13541354                        if ( false !== strpos( $name, '-' ) )
     
    15641564
    15651565        // 4.2
    15661566        if ( $wp_current_db_version < 31351 && $wpdb->charset === 'utf8mb4' ) {
    1567                 if ( ! ( defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) && DO_NOT_UPGRADE_GLOBAL_TABLES ) ) {
    1568                         $wpdb->query( "ALTER TABLE $wpdb->usermeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
    1569                         $wpdb->query( "ALTER TABLE $wpdb->site DROP INDEX domain, ADD INDEX domain(domain(140),path(51))" );
    1570                         $wpdb->query( "ALTER TABLE $wpdb->sitemeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
    1571                         $wpdb->query( "ALTER TABLE $wpdb->signups DROP INDEX domain, ADD INDEX domain(domain(140),path(51))" );
     1567                if ( wp_can_upgrade_global_tables() ) {
     1568                        $wpdb->query( "ALTER TABLE `{$wpdb->usermeta}` DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
     1569                        $wpdb->query( "ALTER TABLE `{$wpdb->site}` DROP INDEX domain, ADD INDEX domain(domain(140),path(51))" );
     1570                        $wpdb->query( "ALTER TABLE `{$wpdb->sitemeta}` DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
     1571                        $wpdb->query( "ALTER TABLE `{$wpdb->signups}` DROP INDEX domain, ADD INDEX domain(domain(140),path(51))" );
    15721572
    15731573                        $tables = $wpdb->tables( 'global' );
    15741574
     
    18701870
    18711871        $global_tables = $wpdb->tables( 'global' );
    18721872        foreach ( $cqueries as $table => $qry ) {
    1873                 // Upgrade global tables only for the main site. Don't upgrade at all if DO_NOT_UPGRADE_GLOBAL_TABLES is defined.
    1874                 if ( in_array( $table, $global_tables ) && ( !is_main_site() || defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) ) {
     1873                // Upgrade global tables only for the main site. Don't upgrade at all if conditions are not optimal.
     1874                if ( in_array( $table, $global_tables ) && ! wp_can_upgrade_global_tables() ) {
    18751875                        unset( $cqueries[ $table ], $for_update[ $table ] );
    18761876                        continue;
    18771877                }
     
    23802380        }
    23812381
    23822382        // Multisite schema upgrades.
    2383         if ( $wp_current_db_version < 25448 && is_multisite() && ! defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) && is_main_network() ) {
     2383        if ( $wp_current_db_version < 25448 && wp_can_upgrade_global_tables() ) {
    23842384
    23852385                // Upgrade verions prior to 3.7
    23862386                if ( $wp_current_db_version < 25179 ) {
     
    24032403
    24042404        // Upgrade versions prior to 4.2.
    24052405        if ( $wp_current_db_version < 31351 ) {
    2406                 if ( ! is_multisite() ) {
     2406                if ( upgrade_global_tables() ) {
    24072407                        $wpdb->query( "ALTER TABLE $wpdb->usermeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
    24082408                }
    24092409                $wpdb->query( "ALTER TABLE $wpdb->terms DROP INDEX slug, ADD INDEX slug(slug(191))" );
     
    24382438        dbDelta( $ms_queries );
    24392439}
    24402440endif;
     2441
     2442/**
     2443 * Can global tables be upgraded?
     2444 *
     2445 * This function performs a series of checks to ensure the environment allows
     2446 * for the safe upgrading of global WordPress database tables. It is necessary
     2447 * because global tables will commonly grow to millions of rows on large
     2448 * installations, and the ability to control their upgrade routines can be
     2449 * critical to the operation of large networks.
     2450 *
     2451 * In a future iteration, this function may use `wp_is_large_network()` to more-
     2452 * intelligently prevent global table upgrades. Until then, we make sure
     2453 * WordPress is on the main site of the main network, to avoid running queries
     2454 * more than once in multi-site or multi-network environments.
     2455 *
     2456 * @since WordPress 4.3.0
     2457 *
     2458 * @return boolean
     2459 */
     2460function wp_can_upgrade_global_tables() {
     2461
     2462        // Assume global tables should be updated
     2463        $retval = true;
     2464
     2465        // Return false early if explicitly not upgrading
     2466        if ( defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) {
     2467                return false;
     2468        }
     2469
     2470        // Set to false if not on main network (does not matter if not multi-network)
     2471        if ( ! is_main_network() ) {
     2472                $retval = false;
     2473        }
     2474
     2475        // Set to false if not on main site of current network (does not matter if not multi-site)
     2476        if ( ! is_main_site() ) {
     2477                $retval = false;
     2478        }
     2479
     2480        return (bool) apply_filters( 'wp_can_upgrade_global_tables', $retval );
     2481}