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

Fix length of index names for a create table statement #133

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
30 changes: 25 additions & 5 deletions tests/WP_SQLite_Translator_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ public function testShowCreateTable1() {
`option_name` varchar(255) DEFAULT '',
`option_value` text NOT NULL DEFAULT '',
PRIMARY KEY (`ID`),
KEY `_tmp_table__composite` (`option_name`, `option_value`),
UNIQUE KEY `_tmp_table__option_name` (`option_name`)
KEY `composite` (`option_name`, `option_value`),
UNIQUE KEY `option_name` (`option_name`)
);",
$results[0]->{'Create Table'}
);
Expand Down Expand Up @@ -312,8 +312,8 @@ public function testShowCreateTableQuoted() {
`option_name` varchar(255) DEFAULT '',
`option_value` text NOT NULL DEFAULT '',
PRIMARY KEY (`ID`),
KEY `_tmp_table__composite` (`option_name`, `option_value`),
UNIQUE KEY `_tmp_table__option_name` (`option_name`)
KEY `composite` (`option_name`, `option_value`),
UNIQUE KEY `option_name` (`option_name`)
);",
$results[0]->{'Create Table'}
);
Expand Down Expand Up @@ -365,12 +365,32 @@ public function testShowCreateTableWithAlterAndCreateIndex() {
`option_name` smallint NOT NULL DEFAULT 14,
`option_value` text NOT NULL DEFAULT \'\',
PRIMARY KEY (`ID`),
KEY `_tmp_table__option_name` (`option_name`)
KEY `option_name` (`option_name`)
);',
$results[0]->{'Create Table'}
);
}

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

$this->assertQuery(
"CREATE TABLE _tmp_table_b (
ID BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL,
option_name VARCHAR(255) default '',
option_value TEXT NOT NULL,
KEY `option_name` (`option_name`)
);"
);
}

public function testShowCreateTableWithPrimaryKeyColumnsReverseOrdered() {
$this->assertQuery(
'CREATE TABLE `_tmp_table` (
Expand Down
26 changes: 22 additions & 4 deletions wp-includes/sqlite/class-wp-sqlite-translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -898,8 +898,7 @@ private function execute_create_table() {
if ( 'UNIQUE INDEX' === $index_type ) {
$unique = 'UNIQUE ';
}
$index_name = "{$table->name}__{$constraint->name}";

$index_name = $this->generate_index_name( $table->name, $constraint->name );
$this->execute_sqlite_query(
"CREATE $unique INDEX \"$index_name\" ON \"{$table->name}\" (\"" . implode( '", "', $constraint->columns ) . '")'
);
Expand Down Expand Up @@ -3081,7 +3080,7 @@ private function execute_alter() {
} elseif ( 'ADD' === $op_type && $is_index_op ) {
$key_name = $this->rewriter->consume()->value;
$sqlite_index_type = $this->mysql_index_type_to_sqlite_type( $mysql_index_type );
$sqlite_index_name = "{$this->table_name}__$key_name";
$sqlite_index_name = $this->generate_index_name( $this->table_name, $key_name );
$this->rewriter->replace_all(
array(
new WP_SQLite_Token( 'CREATE', WP_SQLite_Token::TYPE_KEYWORD, WP_SQLite_Token::FLAG_KEYWORD_RESERVED ),
Expand Down Expand Up @@ -3565,7 +3564,12 @@ private function get_key_definitions( $table_name, $columns ) {

$key_definition[] = 'KEY';

$key_definition[] = sprintf( '`%s`', $key['index']['name'] );
// 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'];

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

$cols = array_map(
function ( $column ) {
Expand Down Expand Up @@ -4188,4 +4192,18 @@ public function rollback() {
do_action( 'sqlite_transaction_query_executed', 'ROLLBACK', (bool) $this->last_exec_returned, $this->transaction_level );
return $this->last_exec_returned;
}

/**
* Create an index name consisting of table name and original index name.
* This is to avoid duplicate index names in SQLite.
*
* @param $table
* @param $original_index_name
*
* @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.
Copy link
Member

@brandonpayton brandonpayton Jul 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeroenpf it looks like this makes it so we cannot derive the original mysql index name from sqlite index name because we cannot tell which underscores used to be double-underscores.

So import/export operations are not symmetrical/reversible. Do we care about that? Offhand, I think maybe we should.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brandonpayton for this reason i strip out double underscores from the table name alone. Later we can split on the first occurrence of double underscores. The idea was that it does not matter if the index name itself has double underscores so they would be preserved. Maybe I am overlooking something?

Example:

table__name and some_index__name becomes table_name__some_index__name and later we split on the first occurrence of __ which which leaves us with [table_name, some_index__name].

I do however see one error in the explode logic that is used to get the index name; i should have passed in a limit so that it only separates on the first occurrence of __. I will create a new PR to address this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for this reason i strip out double underscores from the table name alone.

@jeroenpf thank you for pointing that out. I had missed that this was just for the separator between table name and index. The naming scheme makes sense to me now.

return preg_replace( '/_{2,}/', '_', $table ) . '__' . $original_index_name;
}
}
Loading