Make WordPress Core

Ticket #49364: 49364.2.diff

File 49364.2.diff, 16.0 KB (added by SergeyBiryukov, 2 years ago)
  • src/wp-admin/includes/upgrade.php

     
    27132713 * Useful for creating new tables and updating existing tables to a new structure.
    27142714 *
    27152715 * @since 1.5.0
     2716 * @since 6.1.0 Ignores display width for integer data types on MySQL 8.0.17 or later,
     2717 *              to match MySQL behavior. Note: This does not affect MariaDB.
    27162718 *
    27172719 * @global wpdb $wpdb WordPress database abstraction object.
    27182720 *
     
    27892791
    27902792        $text_fields = array( 'tinytext', 'text', 'mediumtext', 'longtext' );
    27912793        $blob_fields = array( 'tinyblob', 'blob', 'mediumblob', 'longblob' );
     2794        $int_fields  = array( 'tinyint', 'smallint', 'mediumint', 'int', 'integer', 'bigint' );
    27922795
    2793         $global_tables = $wpdb->tables( 'global' );
     2796        $global_tables  = $wpdb->tables( 'global' );
     2797        $db_version     = $wpdb->db_version();
     2798        $db_server_info = $wpdb->db_server_info();
     2799
    27942800        foreach ( $cqueries as $table => $qry ) {
    27952801                // Upgrade global tables only for the main site. Don't upgrade at all if conditions are not optimal.
    27962802                if ( in_array( $table, $global_tables, true ) && ! wp_should_upgrade_global_tables() ) {
     
    29462952                        $tablefield_field_lowercased = strtolower( $tablefield->Field );
    29472953                        $tablefield_type_lowercased  = strtolower( $tablefield->Type );
    29482954
     2955                        $tablefield_type_without_parentheses = preg_replace(
     2956                                '/'
     2957                                . '(.+)'       // Field type, e.g. `int`.
     2958                                . '\(\d*\)'    // Display width.
     2959                                . '(.*)'       // Optional attributes, e.g. `unsigned`.
     2960                                . '/',
     2961                                '$1$2',
     2962                                $tablefield_type_lowercased
     2963                        );
     2964
     2965                        // Get the type without attributes, e.g. `int`.
     2966                        $tablefield_type_base = strtok( $tablefield_type_without_parentheses, ' ' );
     2967
    29492968                        // If the table field exists in the field array...
    29502969                        if ( array_key_exists( $tablefield_field_lowercased, $cfields ) ) {
    29512970
     
    29542973                                $fieldtype            = $matches[1];
    29552974                                $fieldtype_lowercased = strtolower( $fieldtype );
    29562975
     2976                                $fieldtype_without_parentheses = preg_replace(
     2977                                        '/'
     2978                                        . '(.+)'       // Field type, e.g. `int`.
     2979                                        . '\(\d*\)'    // Display width.
     2980                                        . '(.*)'       // Optional attributes, e.g. `unsigned`.
     2981                                        . '/',
     2982                                        '$1$2',
     2983                                        $fieldtype_lowercased
     2984                                );
     2985
     2986                                // Get the type without attributes, e.g. `int`.
     2987                                $fieldtype_base = strtok( $fieldtype_without_parentheses, ' ' );
     2988
    29572989                                // Is actual field type different from the field type in query?
    29582990                                if ( $tablefield->Type != $fieldtype ) {
    29592991                                        $do_change = true;
     
    29693001                                                }
    29703002                                        }
    29713003
     3004                                        if ( in_array( $fieldtype_base, $int_fields, true ) && in_array( $tablefield_type_base, $int_fields, true )
     3005                                                && $fieldtype_without_parentheses === $tablefield_type_without_parentheses
     3006                                        ) {
     3007                                                /*
     3008                                                 * MySQL 8.0.17 or later does not support display width for integer data types,
     3009                                                 * so if display width is the only difference, it can be safely ignored.
     3010                                                 * Note: This is specific to MySQL and does not affect MariaDB.
     3011                                                 */
     3012                                                if ( version_compare( $db_version, '8.0.17', '>=' )
     3013                                                        && ! str_contains( $db_server_info, 'MariaDB' )
     3014                                                ) {
     3015                                                        $do_change = false;
     3016                                                }
     3017                                        }
     3018
    29723019                                        if ( $do_change ) {
    29733020                                                // Add a query to change the column type.
    29743021                                                $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN `{$tablefield->Field}` " . $cfields[ $tablefield_field_lowercased ];
  • tests/phpunit/tests/dbdelta.php

     
    2222        protected $db_engine = '';
    2323
    2424        /**
    25          * Display width for BIGINT data type.
     25         * The database server version.
    2626         *
    27          * Prior to MySQL 8.0.17, default width of 20 digits was used: BIGINT(20).
    28          * Since MySQL 8.0.17, display width for integer data types is no longer supported.
     27         * @var string
    2928         */
    30         protected $bigint_display_width = '';
     29        private static $db_version;
    3130
    3231        /**
     32         * Full database server information.
     33         *
     34         * @var string
     35         */
     36        private static $db_server_info;
     37
     38        /**
    3339         * Make sure the upgrade code is loaded before the tests are run.
    3440         */
    3541        public static function set_up_before_class() {
    3642
     43                global $wpdb;
     44
    3745                parent::set_up_before_class();
    3846
    3947                require_once ABSPATH . 'wp-admin/includes/upgrade.php';
     48
     49                self::$db_version     = $wpdb->db_version();
     50                self::$db_server_info = $wpdb->db_server_info();
    4051        }
    4152
    4253        /**
     
    4657
    4758                global $wpdb;
    4859
    49                 $db_version = $wpdb->db_version();
    50 
    51                 if ( version_compare( $db_version, '5.7', '<' ) ) {
     60                if ( version_compare( self::$db_version, '5.7', '<' ) ) {
    5261                        // Prior to MySQL 5.7, InnoDB did not support FULLTEXT indexes, so MyISAM is used instead.
    5362                        $this->db_engine = 'ENGINE=MyISAM';
    5463                }
    5564
    56                 if ( version_compare( $db_version, '8.0.17', '<' ) ) {
    57                         // Prior to MySQL 8.0.17, default width of 20 digits was used: BIGINT(20).
    58                         $this->bigint_display_width = '(20)';
    59                 }
    60 
    6165                $wpdb->query(
    6266                        $wpdb->prepare(
    6367                                "
    6468                                CREATE TABLE {$wpdb->prefix}dbdelta_test (" .
    6569                                        // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
    66                                         "id bigint{$this->bigint_display_width} NOT NULL AUTO_INCREMENT,
     70                                        "id bigint(20) NOT NULL AUTO_INCREMENT,
    6771                                        column_1 varchar(255) NOT NULL,
    6872                                        column_2 text,
    6973                                        column_3 blob,
     
    109113
    110114                $updates = dbDelta(
    111115                        "CREATE TABLE {$wpdb->prefix}dbdelta_create_test (
    112                                 id bigint{$this->bigint_display_width} NOT NULL AUTO_INCREMENT,
     116                                id bigint(20) NOT NULL AUTO_INCREMENT,
    113117                                column_1 varchar(255) NOT NULL,
    114118                                PRIMARY KEY  (id)
    115119                        );"
     
    144148                $updates = dbDelta(
    145149                        "
    146150                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
    147                                 id bigint{$this->bigint_display_width} NOT NULL AUTO_INCREMENT,
     151                                id bigint(20) NOT NULL AUTO_INCREMENT,
    148152                                column_1 varchar(255) NOT NULL,
    149153                                PRIMARY KEY  (id),
    150154                                KEY key_1 (column_1($this->max_index_length)),
     
    163167
    164168                global $wpdb;
    165169
    166                 // id: bigint => int(11)
     170                // id: bigint(20) => int(11)
    167171                $updates = dbDelta(
    168172                        "
    169173                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     
    176180                        "
    177181                );
    178182
     183                $bigint_display_width = '(20)';
     184
     185                /*
     186                 * MySQL 8.0.17 or later does not support display width for integer data types,
     187                 * so if display width is the only difference, it can be safely ignored.
     188                 * Note: This is specific to MySQL and does not affect MariaDB.
     189                 */
     190                if ( version_compare( self::$db_version, '8.0.17', '>=' )
     191                        && ! str_contains( self::$db_server_info, 'MariaDB' )
     192                ) {
     193                        $bigint_display_width = '';
     194                }
     195
    179196                $this->assertSame(
    180197                        array(
    181198                                "{$wpdb->prefix}dbdelta_test.id"
    182                                         => "Changed type of {$wpdb->prefix}dbdelta_test.id from bigint{$this->bigint_display_width} to int(11)",
     199                                        => "Changed type of {$wpdb->prefix}dbdelta_test.id from bigint{$bigint_display_width} to int(11)",
    183200                        ),
    184201                        $updates
    185202                );
     
    195212                $updates = dbDelta(
    196213                        "
    197214                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
    198                                 id bigint{$this->bigint_display_width} NOT NULL AUTO_INCREMENT,
     215                                id bigint(20) NOT NULL AUTO_INCREMENT,
    199216                                column_1 varchar(255) NOT NULL,
    200217                                extra_col longtext,
    201218                                PRIMARY KEY  (id),
     
    230247                $updates = dbDelta(
    231248                        "
    232249                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
    233                                 id bigint{$this->bigint_display_width} NOT NULL AUTO_INCREMENT,
     250                                id bigint(20) NOT NULL AUTO_INCREMENT,
    234251                                PRIMARY KEY  (id),
    235252                                KEY key_1 (column_1($this->max_index_length)),
    236253                                KEY compound_key (id,column_1($this->max_index_length))
     
    254271                $updates = dbDelta(
    255272                        "
    256273                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
    257                                 id bigint{$this->bigint_display_width} NOT NULL AUTO_INCREMENT,
     274                                id bigint(20) NOT NULL AUTO_INCREMENT,
    258275                                column_1 varchar(255) NOT NULL,
    259276                                extra_col longtext,
    260277                                PRIMARY KEY  (id),
     
    306323                $updates = dbDelta(
    307324                        "
    308325                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
    309                                 id bigint{$this->bigint_display_width} NOT NULL AUTO_INCREMENT,
     326                                id bigint(20) NOT NULL AUTO_INCREMENT,
    310327                                column_1 varchar(255) NOT NULL,
    311328                                PRIMARY KEY  (id),
    312329                                KEY key_1 (column_1($this->max_index_length)),
     
    451468                $result = dbDelta(
    452469                        "
    453470                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
    454                                 id bigint{$this->bigint_display_width} NOT NULL AUTO_INCREMENT,
     471                                id bigint(20) NOT NULL AUTO_INCREMENT,
    455472                                column_1 varchar(255) NOT NULL,
    456473                                column_2 tinytext,
    457474                                column_3 blob,
     
    476493                $result = dbDelta(
    477494                        "
    478495                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
    479                                 id bigint{$this->bigint_display_width} NOT NULL AUTO_INCREMENT,
     496                                id bigint(20) NOT NULL AUTO_INCREMENT,
    480497                                column_1 varchar(255) NOT NULL,
    481498                                column_2 text,
    482499                                column_3 tinyblob,
     
    501518                $result = dbDelta(
    502519                        "
    503520                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
    504                                 id bigint{$this->bigint_display_width} NOT NULL AUTO_INCREMENT,
     521                                id bigint(20) NOT NULL AUTO_INCREMENT,
    505522                                column_1 varchar(255) NOT NULL,
    506523                                column_2 bigtext,
    507524                                column_3 blob,
     
    532549                $result = dbDelta(
    533550                        "
    534551                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
    535                                 id bigint{$this->bigint_display_width} NOT NULL AUTO_INCREMENT,
     552                                id bigint(20) NOT NULL AUTO_INCREMENT,
    536553                                column_1 varchar(255) NOT NULL,
    537554                                column_2 text,
    538555                                column_3 mediumblob,
     
    562579
    563580                $schema = "
    564581                        CREATE TABLE {$wpdb->prefix}dbdelta_test2 (
    565                                 `id` bigint{$this->bigint_display_width} NOT NULL AUTO_INCREMENT,
     582                                `id` bigint(20) NOT NULL AUTO_INCREMENT,
    566583                                `column_1` varchar(255) NOT NULL,
    567584                                PRIMARY KEY  (id),
    568585                                KEY compound_key (id,column_1($this->max_index_length))
     
    585602        public function test_spatial_indices() {
    586603                global $wpdb;
    587604
    588                 $db_version = $wpdb->db_version();
    589 
    590                 if ( version_compare( $db_version, '5.4', '<' ) ) {
     605                if ( version_compare( self::$db_version, '5.4', '<' ) ) {
    591606                        $this->markTestSkipped( 'Spatial indices require MySQL 5.4 and above.' );
    592607                }
    593608
    594                 $geomcollection_name = 'geomcollection';
     609                $geometrycollection_name = 'geometrycollection';
    595610
    596                 if ( version_compare( $db_version, '8.0.11', '<' ) ) {
    597                         // Prior to MySQL 8.0.11, GeometryCollection data type name was used.
    598                         $geomcollection_name = 'geometrycollection';
     611                if ( version_compare( self::$db_version, '8.0.11', '>=' )
     612                        && ! str_contains( self::$db_server_info, 'MariaDB' )
     613                ) {
     614                        /*
     615                         * MySQL 8.0.11 or later uses GeomCollection data type name
     616                         * as the preferred synonym for GeometryCollection.
     617                         * Note: This is specific to MySQL and does not affect MariaDB.
     618                         */
     619                        $geometrycollection_name = 'geomcollection';
    599620                }
    600621
    601622                $schema =
    602623                        "
    603624                        CREATE TABLE {$wpdb->prefix}spatial_index_test (
    604                                 non_spatial bigint{$this->bigint_display_width} unsigned NOT NULL,
    605                                 spatial_value {$geomcollection_name} NOT NULL,
     625                                non_spatial bigint(20) unsigned NOT NULL,
     626                                spatial_value {$geometrycollection_name} NOT NULL,
    606627                                KEY non_spatial (non_spatial),
    607628                                SPATIAL KEY spatial_key (spatial_value)
    608629                        ) {$this->db_engine};
     
    618639                $schema =
    619640                        "
    620641                        CREATE TABLE {$wpdb->prefix}spatial_index_test (
    621                                 non_spatial bigint{$this->bigint_display_width} unsigned NOT NULL,
    622                                 spatial_value {$geomcollection_name} NOT NULL,
    623                                 spatial_value2 {$geomcollection_name} NOT NULL,
     642                                non_spatial bigint(20) unsigned NOT NULL,
     643                                spatial_value {$geometrycollection_name} NOT NULL,
     644                                spatial_value2 {$geometrycollection_name} NOT NULL,
    624645                                KEY non_spatial (non_spatial),
    625646                                SPATIAL KEY spatial_key (spatial_value)
    626647                                SPATIAL KEY spatial_key2 (spatial_value2)
     
    648669
    649670                $schema = "
    650671                        CREATE TABLE {$wpdb->prefix}dbdelta_test2 (
    651                                 `id` bigint{$this->bigint_display_width} NOT NULL AUTO_INCREMENT,
     672                                `id` bigint(20) NOT NULL AUTO_INCREMENT,
    652673                                `references` varchar(255) NOT NULL,
    653674                                PRIMARY KEY  (`id`),
    654675                                KEY `compound_key` (`id`,`references`($this->max_index_length))
     
    678699                $updates = dbDelta(
    679700                        "
    680701                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
    681                                 id bigint{$this->bigint_display_width} NOT NULL AUTO_INCREMENT,
     702                                id bigint(20) NOT NULL AUTO_INCREMENT,
    682703                                column_1 varchar(255) NOT NULL,
    683704                                column_2 text,
    684705                                column_3 blob,
     
    708729        /**
    709730         * @ticket 20263
    710731         */
    711         public function test_wp_get_db_schema_does_no_alter_queries_on_existing_install() {
     732        public function test_wp_get_db_schema_does_not_alter_queries_on_existing_install() {
    712733                $updates = dbDelta( wp_get_db_schema() );
    713734
    714735                $this->assertEmpty( $updates );
     
    722743
    723744                $schema = "
    724745                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
    725                                 id bigint{$this->bigint_display_width} NOT NULL AUTO_INCREMENT,
     746                                id bigint(20) NOT NULL AUTO_INCREMENT,
    726747                                column_1 varchar(255) NOT NULL,
    727748                                column_2 text,
    728749                                column_3 blob,
     
    761782                $updates = dbDelta(
    762783                        "
    763784                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
    764                                 id bigint{$this->bigint_display_width} NOT NULL AUTO_INCREMENT,
     785                                id bigint(20) NOT NULL AUTO_INCREMENT,
    765786                                column_1 varchar(255) NOT NULL,
    766787                                column_2 text,
    767788                                column_3 blob,
     
    784805
    785806                $schema = "
    786807                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
    787                                 id bigint{$this->bigint_display_width} NOT NULL AUTO_INCREMENT,
     808                                id bigint(20) NOT NULL AUTO_INCREMENT,
    788809                                column_1 varchar(255) NOT NULL,
    789810                                column_2 text,
    790811                                column_3 blob,
     
    819840                $updates = dbDelta(
    820841                        "
    821842                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
    822                                 id bigint{$this->bigint_display_width} NOT NULL AUTO_INCREMENT,
     843                                id bigint(20) NOT NULL AUTO_INCREMENT,
    823844                                column_1 varchar(255) NOT NULL,
    824845                                column_2 text,
    825846                                column_3 blob,
     
    843864                $updates = dbDelta(
    844865                        "
    845866                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
    846                                 id bigint{$this->bigint_display_width} NOT NULL AUTO_INCREMENT,
     867                                id bigint(20) NOT NULL AUTO_INCREMENT,
    847868                                column_1 varchar(255) NOT NULL,
    848869                                column_2 text,
    849870                                column_3 blob,
     
    867888                $updates = dbDelta(
    868889                        "
    869890                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
    870                                 id bigint{$this->bigint_display_width} NOT NULL AUTO_INCREMENT,
     891                                id bigint(20) NOT NULL AUTO_INCREMENT,
    871892                                column_1 varchar(255) NOT NULL,
    872893                                column_2 text,
    873894                                column_3 blob,
     
    891912                $updates = dbDelta(
    892913                        "
    893914                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
    894                                 id bigint{$this->bigint_display_width} NOT NULL AUTO_INCREMENT,
     915                                id bigint(20) NOT NULL AUTO_INCREMENT,
    895916                                column_1 varchar(255) NOT NULL,
    896917                                column_2 text,
    897918                                column_3 blob,
     
    915936                $updates = dbDelta(
    916937                        "
    917938                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
    918                                 id bigint{$this->bigint_display_width} NOT NULL AUTO_INCREMENT,
     939                                id bigint(20) NOT NULL AUTO_INCREMENT,
    919940                                column_1 varchar(255) NOT NULL,
    920941                                column_2 text,
    921942                                column_3 blob,
     
    940961                $updates = dbDelta(
    941962                        "
    942963                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
    943                                 id bigint{$this->bigint_display_width} NOT NULL AUTO_INCREMENT,
     964                                id bigint(20) NOT NULL AUTO_INCREMENT,
    944965                                column_1 varchar(255) NOT NULL,
    945966                                column_2 text,
    946967                                column_3 blob,
     
    965986                $updates = dbDelta(
    966987                        "
    967988                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
    968                                 id bigint{$this->bigint_display_width} NOT NULL AUTO_INCREMENT,
     989                                id bigint(20) NOT NULL AUTO_INCREMENT,
    969990                                column_1 varchar(255) NOT NULL,
    970991                                column_2 text,
    971992                                column_3 blob,
     
    9881009                $updates = dbDelta(
    9891010                        "
    9901011                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
    991                                 id bigint{$this->bigint_display_width} NOT NULL AUTO_INCREMENT,
     1012                                id bigint(20) NOT NULL AUTO_INCREMENT,
    9921013                                column_1 varchar(255) NOT NULL,
    9931014                                column_2 text,
    9941015                                column_3 blob,
     
    10061027                $updates = dbDelta(
    10071028                        "
    10081029                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
    1009                                 id bigint{$this->bigint_display_width} NOT NULL AUTO_INCREMENT,
     1030                                id bigint(20) NOT NULL AUTO_INCREMENT,
    10101031                                column_1 varchar(255) NOT NULL,
    10111032                                column_2 text,
    10121033                                column_3 blob,
     
    10241045                $updates = dbDelta(
    10251046                        "
    10261047                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
    1027                                 id bigint{$this->bigint_display_width} NOT NULL AUTO_INCREMENT,
     1048                                id bigint(20) NOT NULL AUTO_INCREMENT,
    10281049                                column_1 varchar(255) NOT NULL,
    10291050                                column_2 text,
    10301051                                column_3 blob,