Make WordPress Core

Changeset 37939


Ignore:
Timestamp:
07/01/2016 11:50:08 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 includes the setUp() and tearDown() parts of [32270], to allow the tests to run, and fixes a typo them.

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

See #36748.

Location:
branches/4.2
Files:
2 edited

Legend:

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

    r33063 r37939  
    19501950        $iqueries = apply_filters( 'dbdelta_insert_queries', $iqueries );
    19511951
     1952        $text_fields = array( 'tinytext', 'text', 'mediumtext', 'longtext' );
     1953        $blob_fields = array( 'tinyblob', 'blob', 'mediumblob', 'longblob' );
     1954
    19521955        $global_tables = $wpdb->tables( 'global' );
    19531956        foreach ( $cqueries as $table => $qry ) {
     
    20192022                                // Is actual field type different from the field type in query?
    20202023                                if ($tablefield->Type != $fieldtype) {
     2024                                        $do_change = true;
     2025                                        if ( in_array( strtolower( $fieldtype ), $text_fields ) && in_array( strtolower( $tablefield->Type ), $text_fields ) ) {
     2026                                                if ( array_search( strtolower( $fieldtype ), $text_fields ) < array_search( strtolower( $tablefield->Type ), $text_fields ) ) {
     2027                                                        $do_change = false;
     2028                                                }
     2029                                        }
     2030
     2031                                        if ( in_array( strtolower( $fieldtype ), $blob_fields ) && in_array( strtolower( $tablefield->Type ), $blob_fields ) ) {
     2032                                                if ( array_search( strtolower( $fieldtype ), $blob_fields ) < array_search( strtolower( $tablefield->Type ), $blob_fields ) ) {
     2033                                                        $do_change = false;
     2034                                                }
     2035                                        }
     2036
     2037                                        if ( $do_change ) {
    20212038                                        // Add a query to change the column type
    2022                                         $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)];
    2023                                         $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
     2039                                                $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)];
     2040                                                $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
     2041                                        }
    20242042                                }
    20252043
  • branches/4.2/tests/phpunit/tests/dbdelta.php

    r32108 r37939  
    99class Tests_dbDelta extends WP_UnitTestCase {
    1010
     11        /**
     12         * Make sure the upgrade code is loaded before the tests are run.
     13         */
     14        public static function setUpBeforeClass() {
     15
     16                parent::setUpBeforeClass();
     17
     18                require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
     19        }
     20
     21        /**
     22         * Create a custom table to be used in each test.
     23         */
     24        public function setUp() {
     25
     26                global $wpdb;
     27
     28                $wpdb->query(
     29                        "
     30                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     31                                id bigint(20) NOT NULL AUTO_INCREMENT,
     32                                column_1 varchar(255) NOT NULL,
     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)
     38                        )
     39                        "
     40                );
     41
     42                parent::setUp();
     43        }
     44
     45        /**
     46         * Delete the custom table on teardown.
     47         */
     48        public function tearDown() {
     49
     50                global $wpdb;
     51
     52                parent::tearDown();
     53
     54                $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}dbdelta_test" );
     55        }
     56
    1157        function test_create_new_table() {
    12                 include_once( ABSPATH . 'wp-admin/includes/upgrade.php');
    1358                $table_name = 'test_new_table';
    1459
     
    4186                $this->assertSame( array(), $actual );
    4287        }
     88
     89        /**
     90         * @ticket 36748
     91         */
     92        function test_dont_downsize_text_fields() {
     93                global $wpdb;
     94
     95                $result = dbDelta(
     96                        "
     97                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     98                                id bigint(20) NOT NULL AUTO_INCREMENT,
     99                                column_1 varchar(255) NOT NULL,
     100                                column_2 tinytext,
     101                                column_3 blob,
     102                                PRIMARY KEY  (id),
     103                                KEY key_1 (column_1),
     104                                KEY compound_key (id,column_1)
     105                        ) ENGINE=MyISAM
     106                        ", false );
     107
     108                $this->assertSame( array(), $result );
     109        }
     110
     111        /**
     112         * @ticket 36748
     113         */
     114        function test_dont_downsize_blob_fields() {
     115                global $wpdb;
     116
     117                $result = dbDelta(
     118                        "
     119                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     120                                id bigint(20) NOT NULL AUTO_INCREMENT,
     121                                column_1 varchar(255) NOT NULL,
     122                                column_2 text,
     123                                column_3 tinyblob,
     124                                PRIMARY KEY  (id),
     125                                KEY key_1 (column_1),
     126                                KEY compound_key (id,column_1)
     127                        ) ENGINE=MyISAM
     128                        ", false );
     129
     130                $this->assertSame( array(), $result );
     131        }
     132
     133        /**
     134         * @ticket 36748
     135         */
     136        function test_upsize_text_fields() {
     137                global $wpdb;
     138
     139                $result = dbDelta(
     140                        "
     141                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     142                                id bigint(20) NOT NULL AUTO_INCREMENT,
     143                                column_1 varchar(255) NOT NULL,
     144                                column_2 bigtext,
     145                                column_3 blob,
     146                                PRIMARY KEY  (id),
     147                                KEY key_1 (column_1),
     148                                KEY compound_key (id,column_1)
     149                        ) ENGINE=MyISAM
     150                        ", false );
     151
     152                $this->assertSame(
     153                        array(
     154                                "{$wpdb->prefix}dbdelta_test.column_2"
     155                                        => "Changed type of {$wpdb->prefix}dbdelta_test.column_2 from text to bigtext"
     156                        ), $result );
     157        }
     158
     159        /**
     160         * @ticket 36748
     161         */
     162        function test_upsize_blob_fields() {
     163                global $wpdb;
     164
     165                $result = dbDelta(
     166                        "
     167                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     168                                id bigint(20) NOT NULL AUTO_INCREMENT,
     169                                column_1 varchar(255) NOT NULL,
     170                                column_2 text,
     171                                column_3 mediumblob,
     172                                PRIMARY KEY  (id),
     173                                KEY key_1 (column_1),
     174                                KEY compound_key (id,column_1)
     175                        ) ENGINE=MyISAM
     176                        ", false );
     177
     178                $this->assertSame(
     179                        array(
     180                                "{$wpdb->prefix}dbdelta_test.column_3"
     181                                        => "Changed type of {$wpdb->prefix}dbdelta_test.column_3 from blob to mediumblob"
     182                        ), $result );
     183        }
    43184}
Note: See TracChangeset for help on using the changeset viewer.