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

Support CREATE TABLE unique keys with no key name #130

Merged
merged 2 commits into from
Jul 18, 2024
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
17 changes: 17 additions & 0 deletions tests/WP_SQLite_Query_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,23 @@ public function testOnDuplicateKey() {
ON DUPLICATE KEY UPDATE `text` = "test1"'
);
}
public function testOnDuplicateKeyWithUnnamedKeys() {
$this->assertQuery(
'CREATE TABLE `test` (
`id` INT,
`name` VARCHAR(255),
`other` VARCHAR(255),
PRIMARY KEY (id),
UNIQUE KEY (name)
);'
);
// The order is deliberate to test that the query works with the keys in any order.
$this->assertQuery(
'INSERT INTO test (`name`, other)
VALUES ("name", "test")
ON DUPLICATE KEY UPDATE `other` = values(other)'
);
}

public function testShowColumns() {

Expand Down
8 changes: 7 additions & 1 deletion wp-includes/sqlite/class-wp-sqlite-translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,9 @@ private function parse_mysql_create_table_constraint() {
$result->value = $this->normalize_mysql_index_type( $constraint->value );
if ( $result->value ) {
$this->rewriter->skip(); // Constraint type.
if ( 'PRIMARY' !== $result->value ) {

$name = $this->rewriter->peek();
if ( '(' !== $name->token && 'PRIMARY' !== $result->value ) {
$result->name = $this->rewriter->skip()->value;
}

Expand All @@ -1202,6 +1204,10 @@ private function parse_mysql_create_table_constraint() {
}
$this->rewriter->skip(); // `,` or `)`
} while ( $this->rewriter->depth > $constraint_depth );

if ( empty( $result->name ) ) {
$result->name = implode( '_', $result->columns );
}
}

do {
Expand Down
Loading