Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow double underscores in index names #136

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'];
aristath marked this conversation as resolved.
Show resolved Hide resolved

$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;
}
}
Loading