<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[37583] trunk: Database: Normalize index definitions in `dbDelta()`.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/37583">37583</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/37583","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>ocean90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-05-27 11:56:20 +0000 (Fri, 27 May 2016)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Database: Normalize index definitions in `dbDelta()`.

`dbDelta()` compares the index definitions against the result of `SHOW INDEX FROM $table_name`. This requires a specific format so indices are not unnecessarily re-created. This format wasn't ensured, until now.

* Parse the raw index definition to extract the type, name and columns so a normalized definition can be built (<a href="https://core.trac.wordpress.org/ticket/20263">#20263</a>, <a href="https://core.trac.wordpress.org/ticket/34873">#34873</a>).
* Standardize on uppercase types (<a href="https://core.trac.wordpress.org/ticket/34871">#34871</a>) and on 'KEY'. 'INDEX' is only a synonym for 'KEY'.
* Escape index names with backticks (<a href="https://core.trac.wordpress.org/ticket/20263">#20263</a>).
* Normalize columns: Ignore ASC and DESC definitions (<a href="https://core.trac.wordpress.org/ticket/34959">#34959</a>), remove whitespaces (<a href="https://core.trac.wordpress.org/ticket/34869">#34869</a>) and escape column names with backticks (<a href="https://core.trac.wordpress.org/ticket/20263">#20263</a>).
* Add backticks to all index change queries (<a href="https://core.trac.wordpress.org/ticket/20263">#20263</a>).

Props ocean90, pento, kurtpayne.
Fixes <a href="https://core.trac.wordpress.org/ticket/20263">#20263</a>, <a href="https://core.trac.wordpress.org/ticket/34869">#34869</a>, <a href="https://core.trac.wordpress.org/ticket/34871">#34871</a>, <a href="https://core.trac.wordpress.org/ticket/34873">#34873</a>, <a href="https://core.trac.wordpress.org/ticket/34959">#34959</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesupgradephp">trunk/src/wp-admin/includes/upgrade.php</a></li>
<li><a href="#trunktestsphpunittestsdbdeltaphp">trunk/tests/phpunit/tests/dbdelta.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesupgradephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/upgrade.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/upgrade.php   2016-05-27 05:14:25 UTC (rev 37582)
+++ trunk/src/wp-admin/includes/upgrade.php     2016-05-27 11:56:20 UTC (rev 37583)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2184,10 +2184,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $flds = explode("\n", $qryline);
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // For every field line specified in the query.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                foreach ($flds as $fld) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         foreach ( $flds as $fld ) {
+                       $fld = trim( $fld, " \t\n\r\0\x0B," ); // Default trim characters, plus ','.
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        // Extract the field name.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        preg_match("|^([^ ]*)|", trim($fld), $fvals);
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 preg_match( '|^([^ ]*)|', $fld, $fvals );
</ins><span class="cx" style="display: block; padding: 0 10px">                         $fieldname = trim( $fvals[1], '`' );
</span><span class="cx" style="display: block; padding: 0 10px">                        $fieldname_lowercased = strtolower( $fieldname );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2202,14 +2203,98 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                case 'key':
</span><span class="cx" style="display: block; padding: 0 10px">                                case 'spatial':
</span><span class="cx" style="display: block; padding: 0 10px">                                        $validfield = false;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        $indices[] = trim(trim($fld), ", \n");
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                                       /*
+                                        * Normalize the index definition.
+                                        *
+                                        * This is done so the definition can be compared against the result of a
+                                        * `SHOW INDEX FROM $table_name` query which returns the current table
+                                        * index information.
+                                        */
+
+                                       // Extract type, name and columns from the definition.
+                                       preg_match(
+                                                 '/^'
+                                               .   '(?P<index_type>'             // 1) Type of the index.
+                                               .       'PRIMARY\s+KEY|(?:UNIQUE|FULLTEXT|SPATIAL)\s+(?:KEY|INDEX)|KEY|INDEX'
+                                               .   ')'
+                                               .   '\s+'                         // Followed by at least one white space character.
+                                               .   '(?:'                         // Name of the index. Optional if type is PRIMARY KEY.
+                                               .       '`?'                      // Name can be escaped with a backtick.
+                                               .           '(?P<index_name>'     // 2) Name of the index.
+                                               .               '(?:[0-9a-zA-Z$_-]|[\xC2-\xDF][\x80-\xBF])+'
+                                               .           ')'
+                                               .       '`?'                      // Name can be escaped with a backtick.
+                                               .       '\s+'                     // Followed by at least one white space character.
+                                               .   ')*'
+                                               .   '\('                          // Opening bracket for the columns.
+                                               .       '(?P<index_columns>'
+                                               .           '.+?'                 // 3) Column names, index prefixes, and orders.
+                                               .       ')'
+                                               .   '\)'                          // Closing bracket for the columns.
+                                               . '$/im',
+                                               $fld,
+                                               $index_matches
+                                       );
+
+                                       // Uppercase the index type and normalize space characters.
+                                       $index_type = strtoupper( preg_replace( '/\s+/', ' ', trim( $index_matches['index_type'] ) ) );
+
+                                       // 'INDEX' is a synonym for 'KEY', standardize on 'KEY'.
+                                       $index_type = str_replace( 'INDEX', 'KEY', $index_type );
+
+                                       // Escape the index name with backticks. An index for a primary key has no name.
+                                       $index_name = ( 'PRIMARY KEY' === $index_type ) ? '' : '`' . $index_matches['index_name'] . '`';
+
+                                       // Parse the columns. Multiple columns are separated by a comma.
+                                       $index_columns = array_map( 'trim', explode( ',', $index_matches['index_columns'] ) );
+
+                                       // Normalize columns.
+                                       foreach ( $index_columns as &$index_column ) {
+                                               // Extract column name and number of indexed characters (sub_part).
+                                               preg_match(
+                                                         '/'
+                                                       .   '`?'                      // Name can be escaped with a backtick.
+                                                       .       '(?P<column_name>'    // 1) Name of the column.
+                                                       .           '(?:[0-9a-zA-Z$_-]|[\xC2-\xDF][\x80-\xBF])+'
+                                                       .       ')'
+                                                       .   '`?'                      // Name can be escaped with a backtick.
+                                                       .   '(?:'                     // Optional sub part.
+                                                       .       '\s*'                 // Optional white space character between name and opening bracket.
+                                                       .       '\('                  // Opening bracket for the sub part.
+                                                       .           '\s*'             // Optional white space character after opening bracket.
+                                                       .           '(?P<sub_part>'
+                                                       .               '\d+'         // 2) Number of indexed characters.
+                                                       .           ')'
+                                                       .           '\s*'             // Optional white space character before closing bracket.
+                                                       .        '\)'                 // Closing bracket for the sub part.
+                                                       .   ')?'
+                                                       . '/',
+                                                       $index_column,
+                                                       $index_column_matches
+                                               );
+
+                                               // Escape the column name with backticks.
+                                               $index_column = '`' . $index_column_matches['column_name'] . '`';
+
+                                               // Append the optional sup part with the number of indexed characters.
+                                               if ( isset( $index_column_matches['sub_part'] ) ) {
+                                                       $index_column .= '(' . $index_column_matches['sub_part'] . ')';
+                                               }
+                                       }
+
+                                       // Build the normalized index definition and add it to the list of indices.
+                                       $indices[] = "{$index_type} {$index_name} (" . implode( ',', $index_columns ) . ")";
+
+                                       // Destroy no longer needed variables.
+                                       unset( $index_column, $index_column_matches, $index_matches, $index_type, $index_name, $index_columns );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                                         break;
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $fld = trim( $fld );
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        // If it's a valid field, add it to the field array.
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( $validfield ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $cfields[ $fieldname_lowercased ] = trim( $fld, ", \n" );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $cfields[ $fieldname_lowercased ] = $fld;
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2243,7 +2328,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                        if ( $do_change ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                                // Add a query to change the column type.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[ $tablefield_field_lowercased ];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                         $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN `{$tablefield->Field}` " . $cfields[ $tablefield_field_lowercased ];
</ins><span class="cx" style="display: block; padding: 0 10px">                                                 $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
</span><span class="cx" style="display: block; padding: 0 10px">                                        }
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2253,7 +2338,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        $default_value = $matches[1];
</span><span class="cx" style="display: block; padding: 0 10px">                                        if ($tablefield->Default != $default_value) {
</span><span class="cx" style="display: block; padding: 0 10px">                                                // Add a query to change the column's default value
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN {$tablefield->Field} SET DEFAULT '{$default_value}'";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                         $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN `{$tablefield->Field}` SET DEFAULT '{$default_value}'";
</ins><span class="cx" style="display: block; padding: 0 10px">                                                 $for_update[$table.'.'.$tablefield->Field] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}";
</span><span class="cx" style="display: block; padding: 0 10px">                                        }
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2306,17 +2391,19 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        $index_string .= 'SPATIAL ';
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="cx" style="display: block; padding: 0 10px">                                $index_string .= 'KEY ';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                if ($index_name != 'PRIMARY') {
-                                       $index_string .= $index_name;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         if ( 'PRIMARY' !== $index_name  ) {
+                                       $index_string .= '`' . $index_name . '`';
</ins><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><span class="cx" style="display: block; padding: 0 10px">                                $index_columns = '';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                // For each column in the index.
</span><span class="cx" style="display: block; padding: 0 10px">                                foreach ($index_data['columns'] as $column_data) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        if ($index_columns != '') $index_columns .= ',';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 if ( $index_columns != '' ) {
+                                               $index_columns .= ',';
+                                       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                        // Add the field to the column list string.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        $index_columns .= $column_data['fieldname'];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 $index_columns .= '`' . $column_data['fieldname'] . '`';
</ins><span class="cx" style="display: block; padding: 0 10px">                                         if ($column_data['subpart'] != '') {
</span><span class="cx" style="display: block; padding: 0 10px">                                                $index_columns .= '('.$column_data['subpart'].')';
</span><span class="cx" style="display: block; padding: 0 10px">                                        }
</span></span></pre></div>
<a id="trunktestsphpunittestsdbdeltaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/dbdelta.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/dbdelta.php     2016-05-27 05:14:25 UTC (rev 37582)
+++ trunk/tests/phpunit/tests/dbdelta.php       2016-05-27 11:56:20 UTC (rev 37583)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -531,9 +531,274 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( array(
</span><span class="cx" style="display: block; padding: 0 10px">                        "{$wpdb->prefix}spatial_index_test.spatial_value2" => "Added column {$wpdb->prefix}spatial_index_test.spatial_value2",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        "Added index {$wpdb->prefix}spatial_index_test SPATIAL KEY spatial_key2 (spatial_value2)"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 "Added index {$wpdb->prefix}spatial_index_test SPATIAL KEY `spatial_key2` (`spatial_value2`)"
</ins><span class="cx" style="display: block; padding: 0 10px">                         ), $updates );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}spatial_index_test" );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @ticket 20263
+        */
+       function test_query_with_backticks_does_not_cause_a_query_to_alter_all_columns_and_indices_to_run_even_if_none_have_changed() {
+               global $wpdb;
+
+               $schema = "
+                       CREATE TABLE {$wpdb->prefix}dbdelta_test2 (
+                               `id` bigint(20) NOT NULL AUTO_INCREMENT,
+                               `references` varchar(255) NOT NULL,
+                               PRIMARY KEY  (`id`),
+                               KEY `compound_key` (`id`,`references`)
+                       )
+               ";
+
+               $wpdb->query( $schema );
+
+               $updates = dbDelta( $schema );
+
+               $table_indices = $wpdb->get_results( "SHOW INDEX FROM {$wpdb->prefix}dbdelta_test2" );
+               $compound_key_index = wp_list_filter( $table_indices, array( 'Key_name' => 'compound_key' ) );
+
+               $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}dbdelta_test2" );
+
+               $this->assertCount( 2, $compound_key_index );
+               $this->assertEmpty( $updates );
+       }
+
+       /**
+        * @ticket 20263
+        */
+       function test_index_with_a_reserved_keyword_can_be_created() {
+               global $wpdb;
+
+               $updates = dbDelta(
+                       "
+                       CREATE TABLE {$wpdb->prefix}dbdelta_test (
+                               id bigint(20) NOT NULL AUTO_INCREMENT,
+                               column_1 varchar(255) NOT NULL,
+                               column_2 text,
+                               column_3 blob,
+                               `references` varchar(255) NOT NULL,
+                               PRIMARY KEY  (id),
+                               KEY key_1 (column_1),
+                               KEY compound_key (id , column_1),
+                               KEY compound_key2 (id,`references`),
+                               FULLTEXT KEY fulltext_key (column_1)
+                       ) ENGINE=MyISAM
+                       "
+               );
+
+               $table_indices = $wpdb->get_results( "SHOW INDEX FROM {$wpdb->prefix}dbdelta_test" );
+
+               $this->assertCount( 2, wp_list_filter( $table_indices, array( 'Key_name' => 'compound_key2' ) , 'AND' ) );
+
+               $this->assertSame(
+                       array(
+                               "{$wpdb->prefix}dbdelta_test.references" => "Added column {$wpdb->prefix}dbdelta_test.references",
+                               0 => "Added index {$wpdb->prefix}dbdelta_test KEY `compound_key2` (`id`,`references`)",
+                       ),
+                       $updates
+               );
+       }
+
+       /**
+        * @ticket 20263
+        */
+       function test_wp_get_db_schema_does_no_alter_queries_on_existing_install() {
+               $updates = dbDelta( wp_get_db_schema() );
+
+               $this->assertEmpty( $updates );
+       }
+
+       /**
+        * @ticket 20263
+        */
+       function test_key_and_index_and_fulltext_key_and_fulltext_index_and_unique_key_and_unique_index_indicies() {
+               global $wpdb;
+
+               $schema = "
+                       CREATE TABLE {$wpdb->prefix}dbdelta_test (
+                               id bigint(20) NOT NULL AUTO_INCREMENT,
+                               column_1 varchar(255) NOT NULL,
+                               column_2 text,
+                               column_3 blob,
+                               PRIMARY KEY  (id),
+                               KEY key_1 (column_1),
+                               KEY compound_key (id,column_1),
+                               FULLTEXT KEY fulltext_key (column_1),
+                               INDEX key_2 (column_1),
+                               UNIQUE KEY key_3 (column_1),
+                               UNIQUE INDEX key_4 (column_1),
+                               FULLTEXT INDEX key_5 (column_1),
+                       ) ENGINE=MyISAM
+               ";
+
+               $creates = dbDelta( $schema );
+               $this->assertSame(
+                       array(
+                               0 => "Added index {$wpdb->prefix}dbdelta_test KEY `key_2` (`column_1`)",
+                               1 => "Added index {$wpdb->prefix}dbdelta_test UNIQUE KEY `key_3` (`column_1`)",
+                               2 => "Added index {$wpdb->prefix}dbdelta_test UNIQUE KEY `key_4` (`column_1`)",
+                               3 => "Added index {$wpdb->prefix}dbdelta_test FULLTEXT KEY `key_5` (`column_1`)",
+                       ),
+                       $creates
+               );
+
+               $updates = dbDelta( $schema );
+               $this->assertEmpty( $updates );
+       }
+
+       /**
+        * @ticket 20263
+        */
+       function test_index_and_key_are_synonyms_and_do_not_recreate_indices() {
+               global $wpdb;
+
+               $updates = dbDelta(
+                       "
+                       CREATE TABLE {$wpdb->prefix}dbdelta_test (
+                               id bigint(20) NOT NULL AUTO_INCREMENT,
+                               column_1 varchar(255) NOT NULL,
+                               column_2 text,
+                               column_3 blob,
+                               PRIMARY KEY  (id),
+                               INDEX key_1 (column_1),
+                               INDEX compound_key (id,column_1),
+                               FULLTEXT INDEX fulltext_key (column_1)
+                       ) ENGINE=MyISAM
+                       "
+               );
+
+               $this->assertEmpty( $updates );
+       }
+
+       /**
+        * @ticket 20263
+        */
+       function test_indices_with_prefix_limits_are_created_and_do_not_recreate_indices() {
+               global $wpdb;
+
+               $schema = "
+                       CREATE TABLE {$wpdb->prefix}dbdelta_test (
+                               id bigint(20) NOT NULL AUTO_INCREMENT,
+                               column_1 varchar(255) NOT NULL,
+                               column_2 text,
+                               column_3 blob,
+                               PRIMARY KEY  (id),
+                               KEY key_1 (column_1),
+                               KEY compound_key (id,column_1),
+                               FULLTEXT KEY fulltext_key (column_1),
+                               KEY key_2 (column_1(10)),
+                               KEY key_3 (column_2(100),column_1(10)),
+                       ) ENGINE=MyISAM
+               ";
+
+               $creates = dbDelta( $schema );
+               $this->assertSame(
+                       array(
+                               0 => "Added index {$wpdb->prefix}dbdelta_test KEY `key_2` (`column_1`(10))",
+                               1 => "Added index {$wpdb->prefix}dbdelta_test KEY `key_3` (`column_2`(100),`column_1`(10))",
+                       ),
+                       $creates
+               );
+
+               $updates = dbDelta( $schema );
+               $this->assertEmpty( $updates );
+       }
+
+       /**
+        * @ticket 34959
+        */
+       function test_index_col_names_with_order_do_not_recreate_indices() {
+               global $wpdb;
+
+               $updates = dbDelta(
+                       "
+                       CREATE TABLE {$wpdb->prefix}dbdelta_test (
+                               id bigint(20) NOT NULL AUTO_INCREMENT,
+                               column_1 varchar(255) NOT NULL,
+                               column_2 text,
+                               column_3 blob,
+                               PRIMARY KEY  (id),
+                               KEY key_1 (column_1 DESC),
+                               KEY compound_key (id,column_1 ASC),
+                               FULLTEXT KEY fulltext_key (column_1)
+                       ) ENGINE=MyISAM
+                       "
+               );
+
+               $this->assertEmpty( $updates );
+       }
+
+       /**
+        * @ticket 34873
+        */
+       function test_primary_key_with_single_space_does_not_recreate_index() {
+               global $wpdb;
+
+               $updates = dbDelta(
+                       "
+                       CREATE TABLE {$wpdb->prefix}dbdelta_test (
+                               id bigint(20) NOT NULL AUTO_INCREMENT,
+                               column_1 varchar(255) NOT NULL,
+                               column_2 text,
+                               column_3 blob,
+                               PRIMARY KEY (id),
+                               KEY key_1 (column_1),
+                               KEY compound_key (id,column_1),
+                               FULLTEXT KEY fulltext_key (column_1)
+                       ) ENGINE=MyISAM
+                       "
+               );
+
+               $this->assertEmpty( $updates );
+       }
+
+       /**
+        * @ticket 34869
+        */
+       function test_index_definitions_with_spaces_do_not_recreate_indices() {
+               global $wpdb;
+
+               $updates = dbDelta(
+                       "
+                       CREATE TABLE {$wpdb->prefix}dbdelta_test (
+                               id bigint(20) NOT NULL AUTO_INCREMENT,
+                               column_1 varchar(255) NOT NULL,
+                               column_2 text,
+                               column_3 blob,
+                               PRIMARY KEY  (id),
+                               KEY key_1        (         column_1),
+                               KEY compound_key (id,      column_1),
+                               FULLTEXT KEY fulltext_key (column_1)
+                       ) ENGINE=MyISAM
+                       "
+               );
+
+               $this->assertEmpty( $updates );
+       }
+
+       /**
+        * @ticket 34871
+        */
+       function test_index_types_are_not_case_sensitive_and_do_not_recreate_indices() {
+               global $wpdb;
+
+               $updates = dbDelta(
+                       "
+                       CREATE TABLE {$wpdb->prefix}dbdelta_test (
+                               id bigint(20) NOT NULL AUTO_INCREMENT,
+                               column_1 varchar(255) NOT NULL,
+                               column_2 text,
+                               column_3 blob,
+                               PRIMARY KEY  (id),
+                               key key_1 (column_1),
+                               key compound_key (id,column_1),
+                               FULLTEXT KEY fulltext_key (column_1)
+                       ) ENGINE=MyISAM
+                       "
+               );
+
+               $this->assertEmpty( $updates );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>