Make WordPress Core


Ignore:
Timestamp:
01/17/2017 04:00:09 AM (10 years ago)
Author:
pento
Message:

dbDelta: Ignore index subparts when checking for duplicate indices.

If index lengths change in table definitions, we don't recreate the index - instead, we throw a database error, as dbDelta() tries to create a new index with the same name.

It's better to leave the index as is, MySQL doesn't have an efficient process for resizing indices, and dropping/creating is a slow process which we don't want to trigger automatically.

Fixes #34870.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/dbdelta.php

    r38591 r39921  
    827827
    828828        /**
     829         * @ticket 34870
     830         */
     831        function test_unchanged_key_lengths_do_not_recreate_index() {
     832                global $wpdb;
     833
     834                $updates = dbDelta(
     835                        "
     836                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     837                                id bigint(20) NOT NULL AUTO_INCREMENT,
     838                                column_1 varchar(255) NOT NULL,
     839                                column_2 text,
     840                                column_3 blob,
     841                                PRIMARY KEY  (id),
     842                                KEY key_1 (column_1(255)),
     843                                KEY compound_key (id,column_1),
     844                                FULLTEXT KEY fulltext_key (column_1)
     845                        ) ENGINE=MyISAM
     846                        ", false );
     847
     848                $this->assertEmpty( $updates );
     849        }
     850
     851        /**
     852         * @ticket 34870
     853         */
     854        function test_changed_key_lengths_do_not_recreate_index() {
     855                global $wpdb;
     856
     857                $updates = dbDelta(
     858                        "
     859                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     860                                id bigint(20) NOT NULL AUTO_INCREMENT,
     861                                column_1 varchar(255) NOT NULL,
     862                                column_2 text,
     863                                column_3 blob,
     864                                PRIMARY KEY  (id),
     865                                KEY key_1 (column_1),
     866                                KEY compound_key (id,column_1),
     867                                KEY changing_key_length (column_1(20)),
     868                                FULLTEXT KEY fulltext_key (column_1)
     869                        ) ENGINE=MyISAM
     870                        " );
     871
     872                $this->assertSame( array(
     873                        "Added index {$wpdb->prefix}dbdelta_test KEY `changing_key_length` (`column_1`(20))"
     874                ), $updates );
     875
     876                $updates = dbDelta(
     877                        "
     878                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     879                                id bigint(20) NOT NULL AUTO_INCREMENT,
     880                                column_1 varchar(255) NOT NULL,
     881                                column_2 text,
     882                                column_3 blob,
     883                                PRIMARY KEY  (id),
     884                                KEY key_1 (column_1),
     885                                KEY compound_key (id,column_1),
     886                                KEY changing_key_length (column_1(50)),
     887                                FULLTEXT KEY fulltext_key (column_1)
     888                        ) ENGINE=MyISAM
     889                        " );
     890
     891                $this->assertEmpty( $updates );
     892
     893                $updates = dbDelta(
     894                        "
     895                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     896                                id bigint(20) NOT NULL AUTO_INCREMENT,
     897                                column_1 varchar(255) NOT NULL,
     898                                column_2 text,
     899                                column_3 blob,
     900                                PRIMARY KEY  (id),
     901                                KEY key_1 (column_1),
     902                                KEY compound_key (id,column_1),
     903                                KEY changing_key_length (column_1(1)),
     904                                FULLTEXT KEY fulltext_key (column_1)
     905                        ) ENGINE=MyISAM
     906                        " );
     907
     908                $this->assertEmpty( $updates );
     909
     910                $updates = dbDelta(
     911                        "
     912                        CREATE TABLE {$wpdb->prefix}dbdelta_test (
     913                                id bigint(20) NOT NULL AUTO_INCREMENT,
     914                                column_1 varchar(255) NOT NULL,
     915                                column_2 text,
     916                                column_3 blob,
     917                                PRIMARY KEY  (id),
     918                                KEY key_1 (column_1),
     919                                KEY compound_key (id,column_1),
     920                                KEY changing_key_length (column_1),
     921                                FULLTEXT KEY fulltext_key (column_1)
     922                        ) ENGINE=MyISAM
     923                        " );
     924
     925                $this->assertEmpty( $updates );
     926        }
     927
     928        /**
    829929         * @ticket 31679
    830930         */
Note: See TracChangeset for help on using the changeset viewer.