Skip to content

Commit

Permalink
Allow double underscores in index names (#136)
Browse files Browse the repository at this point in the history
See:
#133 (comment)

When creating new tables in SQLite, original index names are prefixed
with the table name. This is because unlike MySQL, SQLite does not allow
identical index names across the database. Adding a table prefix ensures
the index names are unique.

When running a `show create table` query, we restore the original index
name by removing the table prefix. We split on double underscores but we
did not take into account that the original index name can also contain
`__`. This PR fixes an issue where we explode without a limit. We only
want to explode on the first occurrence of `__`. Unit tests have been
added to ensure this behavior.
  • Loading branch information
jeroenpf authored Jul 26, 2024
1 parent 00f31b4 commit 40bdc78
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
34 changes: 32 additions & 2 deletions tests/WP_SQLite_Translator_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,8 @@ public function testCreateTablseWithIdenticalIndexNames() {
ID BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL,
option_name VARCHAR(255) default '',
option_value TEXT NOT NULL,
KEY `option_name` (`option_name`)
KEY `option_name` (`option_name`),
KEY `double__underscores` (`option_name`, `ID`)
);"
);

Expand All @@ -386,11 +387,40 @@ public function testCreateTablseWithIdenticalIndexNames() {
ID BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL,
option_name VARCHAR(255) default '',
option_value TEXT NOT NULL,
KEY `option_name` (`option_name`)
KEY `option_name` (`option_name`),
KEY `double__underscores` (`option_name`, `ID`)
);"
);
}

public function testShowCreateTablePreservesDoubleUnderscoreKeyNames() {
$this->assertQuery(
"CREATE TABLE _tmp__table (
ID BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL,
option_name VARCHAR(255) default '',
option_value TEXT NOT NULL,
KEY `option_name` (`option_name`),
KEY `double__underscores` (`option_name`, `ID`)
);"
);

$this->assertQuery(
'SHOW CREATE TABLE _tmp__table;'
);
$results = $this->engine->get_query_results();
$this->assertEquals(
'CREATE TABLE `_tmp__table` (
`ID` bigint NOT NULL AUTO_INCREMENT,
`option_name` varchar(255) DEFAULT \'\',
`option_value` text NOT NULL DEFAULT \'\',
PRIMARY KEY (`ID`),
KEY `double__underscores` (`option_name`, `ID`),
KEY `option_name` (`option_name`)
);',
$results[0]->{'Create Table'}
);
}

public function testShowCreateTableWithPrimaryKeyColumnsReverseOrdered() {
$this->assertQuery(
'CREATE TABLE `_tmp_table` (
Expand Down
7 changes: 3 additions & 4 deletions wp-includes/sqlite/class-wp-sqlite-translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3570,9 +3570,7 @@ private function get_key_definitions( $table_name, $columns ) {
$key_definition[] = 'KEY';

// Remove the prefix from the index name if there is any. We use __ as a separator.
$index_name = strstr( $key['index']['name'], '__' )
? explode( '__', $key['index']['name'] )[1]
: $key['index']['name'];
$index_name = explode( '__', $key['index']['name'], 2 )[1] ?? $key['index']['name'];

$key_definition[] = sprintf( '`%s`', $index_name );

Expand Down Expand Up @@ -4214,7 +4212,8 @@ public function rollback() {
* @return string
*/
private function generate_index_name( $table, $original_index_name ) {
// Strip the occurrences of 2 or more consecutive underscores to allow easier splitting on __ later.
// Strip the occurrences of 2 or more consecutive underscores from the table name
// to allow easier splitting on __ later.
return preg_replace( '/_{2,}/', '_', $table ) . '__' . $original_index_name;
}
}

0 comments on commit 40bdc78

Please sign in to comment.