Make WordPress Core


Ignore:
Timestamp:
02/06/2015 04:50:19 AM (12 years ago)
Author:
pento
Message:

WPDB: If a site is using the utf8 charset, and their version of MySQL supports utf8mb4, auto-upgrade them to utf8mb4.

This patch also resizes some indexes, to allow for the 767 byte index size limit in standard MySQL installs.

See #21212

File:
1 edited

Legend:

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

    r31246 r31349  
    519519        if ( $wp_current_db_version < 29630 )
    520520                upgrade_400();
     521
     522        if ( $wp_current_db_version < 31349 )
     523                upgrade_420();
    521524
    522525        maybe_disable_link_manager();
     
    14081411
    14091412/**
     1413 * Execute changes made in WordPress 4.2.0.
     1414 *
     1415 * @since 4.2.0
     1416 */
     1417function upgrade_420() {
     1418        global $wp_current_db_version, $wpdb;
     1419
     1420        if ( $wp_current_db_version < 31349 && $wpdb->charset === 'utf8mb4' ) {
     1421                if ( is_multisite() ) {
     1422                        $tables = $wpdb->tables( 'blog' );
     1423                } else {
     1424                        $tables = $wpdb->tables( 'all' );
     1425                }
     1426
     1427                foreach ( $tables as $table ) {
     1428                        maybe_convert_table_to_utf8mb4( $table );
     1429                }
     1430        }
     1431}
     1432
     1433/**
    14101434 * Executes network-level upgrade routines.
    14111435 *
     
    15031527                }
    15041528        }
     1529
     1530        // 4.2
     1531        if ( $wp_current_db_version < 31349 && $wpdb->charset === 'utf8mb4' ) {
     1532                if ( ! ( defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) && DO_NOT_UPGRADE_GLOBAL_TABLES ) ) {
     1533                        $wpdb->query( "ALTER TABLE $wpdb->site DROP INDEX domain, ADD INDEX domain(domain(140),path(51))" );
     1534                        $wpdb->query( "ALTER TABLE $wpdb->sitemeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
     1535                        $wpdb->query( "ALTER TABLE $wpdb->signups DROP INDEX domain, ADD INDEX domain(domain(140),path(51))" );
     1536
     1537                        $tables = $wpdb->tables( 'global' );
     1538
     1539                        foreach ( $tables as $table ) {
     1540                                maybe_convert_table_to_utf8mb4( $table );
     1541                        }
     1542                }
     1543        }
    15051544}
    15061545
     
    16061645        }
    16071646        return false;
     1647}
     1648
     1649/**
     1650 * If a table only contains utf8 or utf8mb4 columns, convert it to utf8mb4.
     1651 *
     1652 * @since 4.2.0
     1653 *
     1654 * @param string $table The table to convert.
     1655 * @return bool true if the table was converted, false if it wasn't.
     1656 */
     1657function maybe_convert_table_to_utf8mb4( $table ) {
     1658        global $wpdb;
     1659
     1660        $results = $wpdb->get_results( "SHOW FULL COLUMNS FROM `$table`" );
     1661        if ( ! $results ) {
     1662                return false;
     1663        }
     1664
     1665        $has_utf8 = false;
     1666        foreach ( $results as $column ) {
     1667                if ( $column->Collation ) {
     1668                        if ( 'utf8' === $column->Collation ) {
     1669                                $has_utf8 = true;
     1670                        } elseif ( 'utf8mb4' !== $column->Collation ) {
     1671                                // Don't upgrade tables that have non-utf8 columns.
     1672                                return false;
     1673                        }
     1674                }
     1675        }
     1676
     1677        if ( ! $has_utf8 ) {
     1678                // Don't bother upgrading tables that don't have utf8 columns.
     1679                return false;
     1680        }
     1681
     1682        return $wpdb->query( "ALTER TABLE $table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci" );
    16081683}
    16091684
     
    22852360                $wpdb->query( "ALTER TABLE $wpdb->terms DROP INDEX slug" );
    22862361        }
     2362
     2363        // Upgrade versions prior to 4.2.
     2364        if ( $wp_current_db_version < 31349 ) {
     2365                // So that we can change tables to utf8mb4, we need to shorten the index lengths to less than 767 bytes
     2366                $wpdb->query( "ALTER TABLE $wpdb->usermeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
     2367                $wpdb->query( "ALTER TABLE $wpdb->terms DROP INDEX slug, ADD INDEX slug(slug(191))" );
     2368                $wpdb->query( "ALTER TABLE $wpdb->terms DROP INDEX name, ADD INDEX name(name(191))" );
     2369                $wpdb->query( "ALTER TABLE $wpdb->commentmeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
     2370                $wpdb->query( "ALTER TABLE $wpdb->postmeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
     2371                $wpdb->query( "ALTER TABLE $wpdb->posts DROP INDEX post_name, ADD INDEX post_name(post_name(191))" );
     2372        }
    22872373}
    22882374
Note: See TracChangeset for help on using the changeset viewer.