Make WordPress Core

Changeset 37525


Ignore:
Timestamp:
05/23/2016 08:35:59 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, after the utf8mb4 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 original size, so let's do that.

Fixes #36748.

Location:
trunk
Files:
2 edited

Legend:

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

    r37488 r37525  
    21552155        $iqueries = apply_filters( 'dbdelta_insert_queries', $iqueries );
    21562156
     2157        $text_fields = array( 'tinytext', 'text', 'mediumtext', 'longtext' );
     2158        $blob_fields = array( 'tinyblob', 'blob', 'mediumblob', 'longblob' );
     2159
    21572160        $global_tables = $wpdb->tables( 'global' );
    21582161        foreach ( $cqueries as $table => $qry ) {
    … …  
    22242227                                // Is actual field type different from the field type in query?
    22252228                                if ($tablefield->Type != $fieldtype) {
     2229                                        $do_change = true;
     2230                                        if ( in_array( strtolower( $fieldtype ), $text_fields ) && in_array( strtolower( $tablefield->Type ), $text_fields ) ) {
     2231                                                if ( array_search( strtolower( $fieldtype ), $text_fields ) < array_search( strtolower( $tablefield->Type ), $text_fields ) ) {
     2232                                                        $do_change = false;
     2233                                                }
     2234                                        }
     2235
     2236                                        if ( in_array( strtolower( $fieldtype ), $blob_fields ) && in_array( strtolower( $tablefield->Type ), $blob_fields ) ) {
     2237                                                if ( array_search( strtolower( $fieldtype ), $blob_fields ) < array_search( strtolower( $tablefield->Type ), $blob_fields ) ) {
     2238                                                        $do_change = false;
     2239                                                }
     2240                                        }
     2241
     2242                                        if ( $do_change ) {
    22262243                                        // Add a query to change the column type
    2227                                         $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)];
    2228                                         $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
     2244                                                $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)];
     2245                                                $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
     2246                                        }
    22292247                                }
    22302248
  • trunk/tests/phpunit/tests/dbdelta.php

    r36552 r37525  
    3232                                id bigint(20) NOT NULL AUTO_INCREMENT,
    3333                                column_1 varchar(255) NOT NULL,
     34                                column_2 text,
     35                                column_3 blob,
    3436                                PRIMARY KEY  (id),
    3537                                KEY key_1 (column_1),
    … …  
    363365                $this->assertSame( array(), $actual );
    364366        }
     367
     368        /**
     369         * @ticket 36748
     370         */
     371        function test_dont_downsize_text_fields() {
     372                global $wpdb;
     373
     374                $result = dbDelta(
     375                        "
     376                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     377                                id bigint(20) NOT NULL AUTO_INCREMENT,
     378                                column_1 varchar(255) NOT NULL,
     379                                column_2 tinytext,
     380                                column_3 blob,
     381                                PRIMARY KEY  (id),
     382                                KEY key_1 (column_1),
     383                                KEY compound_key (id,column_1),
     384                                FULLTEXT KEY fulltext_key (column_1)
     385                        ) ENGINE=MyISAM
     386                        ", false );
     387
     388                $this->assertSame( array(), $result );
     389        }
     390
     391        /**
     392         * @ticket 36748
     393         */
     394        function test_dont_downsize_blob_fields() {
     395                global $wpdb;
     396
     397                $result = dbDelta(
     398                        "
     399                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     400                                id bigint(20) NOT NULL AUTO_INCREMENT,
     401                                column_1 varchar(255) NOT NULL,
     402                                column_2 text,
     403                                column_3 tinyblob,
     404                                PRIMARY KEY  (id),
     405                                KEY key_1 (column_1),
     406                                KEY compound_key (id,column_1),
     407                                FULLTEXT KEY fulltext_key (column_1)
     408                        ) ENGINE=MyISAM
     409                        ", false );
     410
     411                $this->assertSame( array(), $result );
     412        }
     413
     414        /**
     415         * @ticket 36748
     416         */
     417        function test_upsize_text_fields() {
     418                global $wpdb;
     419
     420                $result = dbDelta(
     421                        "
     422                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     423                                id bigint(20) NOT NULL AUTO_INCREMENT,
     424                                column_1 varchar(255) NOT NULL,
     425                                column_2 bigtext,
     426                                column_3 blob,
     427                                PRIMARY KEY  (id),
     428                                KEY key_1 (column_1),
     429                                KEY compound_key (id,column_1),
     430                                FULLTEXT KEY fulltext_key (column_1)
     431                        ) ENGINE=MyISAM
     432                        ", false );
     433
     434                $this->assertSame(
     435                        array(
     436                                "{$wpdb->prefix}dbdelta_test.column_2"
     437                                        => "Changed type of {$wpdb->prefix}dbdelta_test.column_2 from text to bigtext"
     438                        ), $result );
     439        }
     440
     441        /**
     442         * @ticket 36748
     443         */
     444        function test_upsize_blob_fields() {
     445                global $wpdb;
     446
     447                $result = dbDelta(
     448                        "
     449                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     450                                id bigint(20) NOT NULL AUTO_INCREMENT,
     451                                column_1 varchar(255) NOT NULL,
     452                                column_2 text,
     453                                column_3 mediumblob,
     454                                PRIMARY KEY  (id),
     455                                KEY key_1 (column_1),
     456                                KEY compound_key (id,column_1),
     457                                FULLTEXT KEY fulltext_key (column_1)
     458                        ) ENGINE=MyISAM
     459                        ", false );
     460
     461                $this->assertSame(
     462                        array(
     463                                "{$wpdb->prefix}dbdelta_test.column_3"
     464                                        => "Changed type of {$wpdb->prefix}dbdelta_test.column_3 from blob to mediumblob"
     465                        ), $result );
     466        }
    365467}
Note: See TracChangeset for help on using the changeset viewer.