Make WordPress Core

Changeset 37936


Ignore:
Timestamp:
07/01/2016 06:58:40 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.

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

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

See #36748.

Location:
branches/4.4
Files:
2 edited

Legend:

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

    r35701 r37936  
    20972097        $iqueries = apply_filters( 'dbdelta_insert_queries', $iqueries );
    20982098
     2099        $text_fields = array( 'tinytext', 'text', 'mediumtext', 'longtext' );
     2100        $blob_fields = array( 'tinyblob', 'blob', 'mediumblob', 'longblob' );
     2101
    20992102        $global_tables = $wpdb->tables( 'global' );
    21002103        foreach ( $cqueries as $table => $qry ) {
     
    21662169                                // Is actual field type different from the field type in query?
    21672170                                if ($tablefield->Type != $fieldtype) {
     2171                                        $do_change = true;
     2172                                        if ( in_array( strtolower( $fieldtype ), $text_fields ) && in_array( strtolower( $tablefield->Type ), $text_fields ) ) {
     2173                                                if ( array_search( strtolower( $fieldtype ), $text_fields ) < array_search( strtolower( $tablefield->Type ), $text_fields ) ) {
     2174                                                        $do_change = false;
     2175                                                }
     2176                                        }
     2177
     2178                                        if ( in_array( strtolower( $fieldtype ), $blob_fields ) && in_array( strtolower( $tablefield->Type ), $blob_fields ) ) {
     2179                                                if ( array_search( strtolower( $fieldtype ), $blob_fields ) < array_search( strtolower( $tablefield->Type ), $blob_fields ) ) {
     2180                                                        $do_change = false;
     2181                                                }
     2182                                        }
     2183
     2184                                        if ( $do_change ) {
    21682185                                        // Add a query to change the column type
    2169                                         $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)];
    2170                                         $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
     2186                                                $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)];
     2187                                                $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
     2188                                        }
    21712189                                }
    21722190
  • branches/4.4/tests/phpunit/tests/dbdelta.php

    r35487 r37936  
    3232                                id bigint(20) NOT NULL AUTO_INCREMENT,
    3333                                column_1 varchar(255) NOT NULL,
    34                                 PRIMARY KEY  (id),
    35                                 KEY key_1 (column_1),
    36                                 KEY compoud_key (id,column_1),
     34                                column_2 text,
     35                                column_3 blob,
     36                                PRIMARY KEY  (id),
     37                                KEY key_1 (column_1),
     38                                KEY compound_key (id,column_1),
    3739                                FULLTEXT KEY fulltext_key (column_1)
    3840                        ) ENGINE=MyISAM
     
    106108                                PRIMARY KEY  (id),
    107109                                KEY key_1 (column_1),
    108                                 KEY compoud_key (id,column_1)
     110                                KEY compound_key (id,column_1)
    109111                        )
    110112                        "
     
    129131                                PRIMARY KEY  (id),
    130132                                KEY key_1 (column_1),
    131                                 KEY compoud_key (id,column_1)
     133                                KEY compound_key (id,column_1)
    132134                        )
    133135                        "
     
    158160                                PRIMARY KEY  (id),
    159161                                KEY key_1 (column_1),
    160                                 KEY compoud_key (id,column_1)
     162                                KEY compound_key (id,column_1)
    161163                        )
    162164                        "
     
    190192                                PRIMARY KEY  (id),
    191193                                KEY key_1 (column_1),
    192                                 KEY compoud_key (id,column_1)
     194                                KEY compound_key (id,column_1)
    193195                        )
    194196                        "
     
    216218                                PRIMARY KEY  (id),
    217219                                KEY key_1 (column_1),
    218                                 KEY compoud_key (id,column_1)
     220                                KEY compound_key (id,column_1)
    219221                        )
    220222                        "
     
    266268                                PRIMARY KEY  (id),
    267269                                KEY key_1 (column_1),
    268                                 KEY compoud_key (id,column_1),
     270                                KEY compound_key (id,column_1),
    269271                                FULLTEXT KEY fulltext_key (column_1)
    270272                        )
     
    348350                $this->assertSame( array(), $actual );
    349351        }
     352
     353        /**
     354         * @ticket 36748
     355         */
     356        function test_dont_downsize_text_fields() {
     357                global $wpdb;
     358
     359                $result = dbDelta(
     360                        "
     361                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     362                                id bigint(20) NOT NULL AUTO_INCREMENT,
     363                                column_1 varchar(255) NOT NULL,
     364                                column_2 tinytext,
     365                                column_3 blob,
     366                                PRIMARY KEY  (id),
     367                                KEY key_1 (column_1),
     368                                KEY compound_key (id,column_1),
     369                                FULLTEXT KEY fulltext_key (column_1)
     370                        ) ENGINE=MyISAM
     371                        ", false );
     372
     373                $this->assertSame( array(), $result );
     374        }
     375
     376        /**
     377         * @ticket 36748
     378         */
     379        function test_dont_downsize_blob_fields() {
     380                global $wpdb;
     381
     382                $result = dbDelta(
     383                        "
     384                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     385                                id bigint(20) NOT NULL AUTO_INCREMENT,
     386                                column_1 varchar(255) NOT NULL,
     387                                column_2 text,
     388                                column_3 tinyblob,
     389                                PRIMARY KEY  (id),
     390                                KEY key_1 (column_1),
     391                                KEY compound_key (id,column_1),
     392                                FULLTEXT KEY fulltext_key (column_1)
     393                        ) ENGINE=MyISAM
     394                        ", false );
     395
     396                $this->assertSame( array(), $result );
     397        }
     398
     399        /**
     400         * @ticket 36748
     401         */
     402        function test_upsize_text_fields() {
     403                global $wpdb;
     404
     405                $result = dbDelta(
     406                        "
     407                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     408                                id bigint(20) NOT NULL AUTO_INCREMENT,
     409                                column_1 varchar(255) NOT NULL,
     410                                column_2 bigtext,
     411                                column_3 blob,
     412                                PRIMARY KEY  (id),
     413                                KEY key_1 (column_1),
     414                                KEY compound_key (id,column_1),
     415                                FULLTEXT KEY fulltext_key (column_1)
     416                        ) ENGINE=MyISAM
     417                        ", false );
     418
     419                $this->assertSame(
     420                        array(
     421                                "{$wpdb->prefix}dbdelta_test.column_2"
     422                                        => "Changed type of {$wpdb->prefix}dbdelta_test.column_2 from text to bigtext"
     423                        ), $result );
     424        }
     425
     426        /**
     427         * @ticket 36748
     428         */
     429        function test_upsize_blob_fields() {
     430                global $wpdb;
     431
     432                $result = dbDelta(
     433                        "
     434                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     435                                id bigint(20) NOT NULL AUTO_INCREMENT,
     436                                column_1 varchar(255) NOT NULL,
     437                                column_2 text,
     438                                column_3 mediumblob,
     439                                PRIMARY KEY  (id),
     440                                KEY key_1 (column_1),
     441                                KEY compound_key (id,column_1),
     442                                FULLTEXT KEY fulltext_key (column_1)
     443                        ) ENGINE=MyISAM
     444                        ", false );
     445
     446                $this->assertSame(
     447                        array(
     448                                "{$wpdb->prefix}dbdelta_test.column_3"
     449                                        => "Changed type of {$wpdb->prefix}dbdelta_test.column_3 from blob to mediumblob"
     450                        ), $result );
     451        }
    350452}
Note: See TracChangeset for help on using the changeset viewer.