Make WordPress Core

Changeset 37938


Ignore:
Timestamp:
07/01/2016 11:41:57 AM (10 years ago)
Author:
pento
Message:

Database: dbDelta() will no longer try to downgrade the size of TEXT and BLOB columns.

When upgrading to utf8mb4, TEXT fields will be upgraded to MEDIUMTEXT (and likewise for all other *TEXT and *BLOB fields). This is to allow for the additional space requirements of utf8mb4.

On the subsequent upgrade, dbDelta() would try and downgrade the fields to their original size again. At best, this it a waste of time, at worst, this could truncate any data larger than the original size. There's no harm in leaving them at their new size, so let's do that.

The FULLTEXT indexes are removed from the tests, as dbDelta()'s FULLTEXT support was added in WordPress 4.4.

This also fixes a typo in the dbDelta() tests.

Merge of [37525] to the 4.3 branch.
Partial merge of [36552] to the 4.3 branch.

See #36748.

Location:
branches/4.3
Files:
2 edited

Legend:

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

    r33950 r37938  
    20632063        $iqueries = apply_filters( 'dbdelta_insert_queries', $iqueries );
    20642064
     2065        $text_fields = array( 'tinytext', 'text', 'mediumtext', 'longtext' );
     2066        $blob_fields = array( 'tinyblob', 'blob', 'mediumblob', 'longblob' );
     2067
    20652068        $global_tables = $wpdb->tables( 'global' );
    20662069        foreach ( $cqueries as $table => $qry ) {
     
    21322135                                // Is actual field type different from the field type in query?
    21332136                                if ($tablefield->Type != $fieldtype) {
     2137                                        $do_change = true;
     2138                                        if ( in_array( strtolower( $fieldtype ), $text_fields ) && in_array( strtolower( $tablefield->Type ), $text_fields ) ) {
     2139                                                if ( array_search( strtolower( $fieldtype ), $text_fields ) < array_search( strtolower( $tablefield->Type ), $text_fields ) ) {
     2140                                                        $do_change = false;
     2141                                                }
     2142                                        }
     2143
     2144                                        if ( in_array( strtolower( $fieldtype ), $blob_fields ) && in_array( strtolower( $tablefield->Type ), $blob_fields ) ) {
     2145                                                if ( array_search( strtolower( $fieldtype ), $blob_fields ) < array_search( strtolower( $tablefield->Type ), $blob_fields ) ) {
     2146                                                        $do_change = false;
     2147                                                }
     2148                                        }
     2149
     2150                                        if ( $do_change ) {
    21342151                                        // Add a query to change the column type
    2135                                         $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)];
    2136                                         $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
     2152                                                $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)];
     2153                                                $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
     2154                                        }
    21372155                                }
    21382156
  • branches/4.3/tests/phpunit/tests/dbdelta.php

    r33425 r37938  
    3131                                id bigint(20) NOT NULL AUTO_INCREMENT,
    3232                                column_1 varchar(255) NOT NULL,
    33                                 PRIMARY KEY  (id),
    34                                 KEY key_1 (column_1),
    35                                 KEY compoud_key (id,column_1)
     33                                column_2 text,
     34                                column_3 blob,
     35                                PRIMARY KEY  (id),
     36                                KEY key_1 (column_1),
     37                                KEY compound_key (id,column_1)
    3638                        )
    3739                        "
     
    104106                                PRIMARY KEY  (id),
    105107                                KEY key_1 (column_1),
    106                                 KEY compoud_key (id,column_1)
     108                                KEY compound_key (id,column_1)
    107109                        )
    108110                        "
     
    127129                                PRIMARY KEY  (id),
    128130                                KEY key_1 (column_1),
    129                                 KEY compoud_key (id,column_1)
     131                                KEY compound_key (id,column_1)
    130132                        )
    131133                        "
     
    156158                                PRIMARY KEY  (id),
    157159                                KEY key_1 (column_1),
    158                                 KEY compoud_key (id,column_1)
     160                                KEY compound_key (id,column_1)
    159161                        )
    160162                        "
     
    188190                                PRIMARY KEY  (id),
    189191                                KEY key_1 (column_1),
    190                                 KEY compoud_key (id,column_1)
     192                                KEY compound_key (id,column_1)
    191193                        )
    192194                        "
     
    214216                                PRIMARY KEY  (id),
    215217                                KEY key_1 (column_1),
    216                                 KEY compoud_key (id,column_1)
     218                                KEY compound_key (id,column_1)
    217219                        )
    218220                        "
     
    323325                $this->assertSame( array(), $actual );
    324326        }
     327
     328        /**
     329         * @ticket 36748
     330         */
     331        function test_dont_downsize_text_fields() {
     332                global $wpdb;
     333
     334                $result = dbDelta(
     335                        "
     336                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     337                                id bigint(20) NOT NULL AUTO_INCREMENT,
     338                                column_1 varchar(255) NOT NULL,
     339                                column_2 tinytext,
     340                                column_3 blob,
     341                                PRIMARY KEY  (id),
     342                                KEY key_1 (column_1),
     343                                KEY compound_key (id,column_1)
     344                        ) ENGINE=MyISAM
     345                        ", false );
     346
     347                $this->assertSame( array(), $result );
     348        }
     349
     350        /**
     351         * @ticket 36748
     352         */
     353        function test_dont_downsize_blob_fields() {
     354                global $wpdb;
     355
     356                $result = dbDelta(
     357                        "
     358                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     359                                id bigint(20) NOT NULL AUTO_INCREMENT,
     360                                column_1 varchar(255) NOT NULL,
     361                                column_2 text,
     362                                column_3 tinyblob,
     363                                PRIMARY KEY  (id),
     364                                KEY key_1 (column_1),
     365                                KEY compound_key (id,column_1)
     366                        ) ENGINE=MyISAM
     367                        ", false );
     368
     369                $this->assertSame( array(), $result );
     370        }
     371
     372        /**
     373         * @ticket 36748
     374         */
     375        function test_upsize_text_fields() {
     376                global $wpdb;
     377
     378                $result = dbDelta(
     379                        "
     380                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     381                                id bigint(20) NOT NULL AUTO_INCREMENT,
     382                                column_1 varchar(255) NOT NULL,
     383                                column_2 bigtext,
     384                                column_3 blob,
     385                                PRIMARY KEY  (id),
     386                                KEY key_1 (column_1),
     387                                KEY compound_key (id,column_1)
     388                        ) ENGINE=MyISAM
     389                        ", false );
     390
     391                $this->assertSame(
     392                        array(
     393                                "{$wpdb->prefix}dbdelta_test.column_2"
     394                                        => "Changed type of {$wpdb->prefix}dbdelta_test.column_2 from text to bigtext"
     395                        ), $result );
     396        }
     397
     398        /**
     399         * @ticket 36748
     400         */
     401        function test_upsize_blob_fields() {
     402                global $wpdb;
     403
     404                $result = dbDelta(
     405                        "
     406                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     407                                id bigint(20) NOT NULL AUTO_INCREMENT,
     408                                column_1 varchar(255) NOT NULL,
     409                                column_2 text,
     410                                column_3 mediumblob,
     411                                PRIMARY KEY  (id),
     412                                KEY key_1 (column_1),
     413                                KEY compound_key (id,column_1)
     414                        ) ENGINE=MyISAM
     415                        ", false );
     416
     417                $this->assertSame(
     418                        array(
     419                                "{$wpdb->prefix}dbdelta_test.column_3"
     420                                        => "Changed type of {$wpdb->prefix}dbdelta_test.column_3 from blob to mediumblob"
     421                        ), $result );
     422        }
    325423}
Note: See TracChangeset for help on using the changeset viewer.