Make WordPress Core

Changeset 33057


Ignore:
Timestamp:
07/03/2015 04:33:14 AM (9 years ago)
Author:
dd32
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/upgrade.php

    r33055 r33057  
    12531253
    12541254    // 3.0 screen options key name changes.
    1255     if ( is_main_site() && !defined('DO_NOT_UPGRADE_GLOBAL_TABLES') ) {
     1255    if ( wp_should_upgrade_global_tables() ) {
    12561256        $sql = "DELETE FROM $wpdb->usermeta
    12571257            WHERE meta_key LIKE %s
     
    12931293    global $wp_current_db_version, $wpdb, $wp_registered_widgets, $sidebars_widgets;
    12941294
    1295     if ( $wp_current_db_version < 19061 && is_main_site() && ! defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) {
     1295    if ( $wp_current_db_version < 19061 && wp_should_upgrade_global_tables() ) {
    12961296        $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key IN ('show_admin_bar_admin', 'plugins_last_view')" );
    12971297    }
     
    13771377    }
    13781378
    1379     if ( $wp_current_db_version < 20022 && is_main_site() && ! defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) {
     1379    if ( $wp_current_db_version < 20022 && wp_should_upgrade_global_tables() ) {
    13801380        $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key = 'themes_last_view'" );
    13811381    }
     
    14041404        update_option( 'link_manager_enabled', 1 ); // Previously set to 0 by populate_options()
    14051405
    1406     if ( $wp_current_db_version < 21811 && is_main_site() && ! defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) {
     1406    if ( $wp_current_db_version < 21811 && wp_should_upgrade_global_tables() ) {
    14071407        $meta_keys = array();
    14081408        foreach ( array_merge( get_post_types(), get_taxonomies() ) as $name ) {
     
    16741674    // 4.2
    16751675    if ( $wp_current_db_version < 31351 && $wpdb->charset === 'utf8mb4' ) {
    1676         if ( ! ( defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) && DO_NOT_UPGRADE_GLOBAL_TABLES ) ) {
     1676        if ( wp_should_upgrade_global_tables() ) {
    16771677            $wpdb->query( "ALTER TABLE $wpdb->usermeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
    16781678            $wpdb->query( "ALTER TABLE $wpdb->site DROP INDEX domain, ADD INDEX domain(domain(140),path(51))" );
     
    16901690    // 4.3
    16911691    if ( $wp_current_db_version < 33055 && 'utf8mb4' === $wpdb->charset ) {
    1692         if ( ! ( defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) && DO_NOT_UPGRADE_GLOBAL_TABLES ) ) {
     1692        if ( wp_should_upgrade_global_tables() ) {
    16931693            $upgrade = false;
    16941694            $indexes = $wpdb->get_results( "SHOW INDEXES FROM $wpdb->signups" );
     
    20992099    $global_tables = $wpdb->tables( 'global' );
    21002100    foreach ( $cqueries as $table => $qry ) {
    2101         // Upgrade global tables only for the main site. Don't upgrade at all if DO_NOT_UPGRADE_GLOBAL_TABLES is defined.
    2102         if ( in_array( $table, $global_tables ) && ( !is_main_site() || defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) ) {
     2101        // Upgrade global tables only for the main site. Don't upgrade at all if conditions are not optimal.
     2102        if ( in_array( $table, $global_tables ) && ! wp_should_upgrade_global_tables() ) {
    21032103            unset( $cqueries[ $table ], $for_update[ $table ] );
    21042104            continue;
     
    26172617
    26182618    // Multisite schema upgrades.
    2619     if ( $wp_current_db_version < 25448 && is_multisite() && ! defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) && is_main_network() ) {
     2619    if ( $wp_current_db_version < 25448 && is_multisite() && wp_should_upgrade_global_tables() ) {
    26202620
    26212621        // Upgrade verions prior to 3.7
     
    26352635    // Upgrade versions prior to 4.2.
    26362636    if ( $wp_current_db_version < 31351 ) {
    2637         if ( ! is_multisite() ) {
     2637        if ( wp_should_upgrade_global_tables() ) {
    26382638            $wpdb->query( "ALTER TABLE $wpdb->usermeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
    26392639        }
     
    26722672}
    26732673endif;
     2674
     2675/**
     2676 * Determine if global tables should be upgraded.
     2677 *
     2678 * This function performs a series of checks to ensure the environment allows
     2679 * for the safe upgrading of global WordPress database tables. It is necessary
     2680 * because global tables will commonly grow to millions of rows on large
     2681 * installations, and the ability to control their upgrade routines can be
     2682 * critical to the operation of large networks.
     2683 *
     2684 * In a future iteration, this function may use `wp_is_large_network()` to more-
     2685 * intelligently prevent global table upgrades. Until then, we make sure
     2686 * WordPress is on the main site of the main network, to avoid running queries
     2687 * more than once in multi-site or multi-network environments.
     2688 *
     2689 * @since 4.3.0
     2690 *
     2691 * @return bool Whether to run the upgrade routines on global tables.
     2692 */
     2693function wp_should_upgrade_global_tables() {
     2694
     2695    // Return false early if explicitly not upgrading
     2696    if ( defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) {
     2697        return false;
     2698    }
     2699
     2700    // Assume global tables should be upgraded
     2701    $should_upgrade = true;
     2702
     2703    // Set to false if not on main network (does not matter if not multi-network)
     2704    if ( ! is_main_network() ) {
     2705        $should_upgrade = false;
     2706    }
     2707
     2708    // Set to false if not on main site of current network (does not matter if not multi-site)
     2709    if ( ! is_main_site() ) {
     2710        $should_upgrade = false;
     2711    }
     2712
     2713    /**
     2714     * Filter if upgrade routines should be run on global tables in multisite.
     2715     *
     2716     * @param bool $should_upgrade Whether to run the upgrade routines on global tables.
     2717     */
     2718    return apply_filters( 'wp_should_upgrade_global_tables', $should_upgrade );
     2719}
Note: See TracChangeset for help on using the changeset viewer.