diff --git a/pg4wp/rewriters/CreateTableSQLRewriter.php b/pg4wp/rewriters/CreateTableSQLRewriter.php index 25512cf..3f23cc1 100644 --- a/pg4wp/rewriters/CreateTableSQLRewriter.php +++ b/pg4wp/rewriters/CreateTableSQLRewriter.php @@ -86,16 +86,24 @@ class CreateTableSQLRewriter extends AbstractSQLRewriter } // Support for UNIQUE INDEX creation - $pattern = '/,\s+(UNIQUE |)KEY\s+([^\s]+)\s+\(((?:[\w]+(?:\([\d]+\))?[,]?)*)\)/'; + $pattern = '/,\s*(UNIQUE |)KEY\s+(`[^`]+`|\w+)\s+\(([^)]+)\)/'; if(preg_match_all($pattern, $sql, $matches, PREG_SET_ORDER)) { foreach($matches as $match) { $unique = $match[1]; $index = $match[2]; $columns = $match[3]; - $columns = preg_replace('/\(\d+\)/', '', $columns); - // Workaround for index name duplicate - $index = $table . '_' . $index; - $sql .= "\nCREATE {$unique}INDEX $index ON $table ($columns);"; + + // Removing backticks from the index names + $index = str_replace('`', '', $index); + + // Removing backticks from the columns + $columns = str_replace('`', '', $columns); + + // Creating a unique index name + $indexName = $table . '_' . $index; + + // Appending the CREATE INDEX statement to SQL + $sql .= "\nCREATE {$unique}INDEX $indexName ON $table ($columns);"; } } // Now remove handled indexes @@ -105,7 +113,7 @@ class CreateTableSQLRewriter extends AbstractSQLRewriter $pattern = "/(,\s*)?UNIQUE KEY\s+[a-zA-Z0-9_]+\s+(\([a-zA-Z0-9_,\s]+\))/"; $replacement = "$1UNIQUE $2"; $sql = preg_replace($pattern, $replacement, $sql); - + return $sql; } } diff --git a/tests/rewriteTest.php b/tests/rewriteTest.php index b588e51..d71f75a 100644 --- a/tests/rewriteTest.php +++ b/tests/rewriteTest.php @@ -21,7 +21,7 @@ final class rewriteTest extends TestCase $sql = 'SELECT COUNT(NULLIF(`meta_value` LIKE \'%"administrator"%\', false)), COUNT(NULLIF(`meta_value` = \'a:0:{}\', false)), COUNT(*) FROM wp_usermeta INNER JOIN wp_users ON user_id = ID WHERE meta_key = \'wp_capabilities\''; $expected = 'SELECT COUNT(NULLIF(meta_value ILIKE \'%"administrator"%\', false)) AS count0, COUNT(NULLIF(meta_value = \'a:0:{}\', false)) AS count1, COUNT(*) FROM wp_usermeta INNER JOIN wp_users ON user_id = "ID" WHERE meta_key = \'wp_capabilities\''; $postgresql = pg4wp_rewrite($sql); - $this->assertSame($postgresql, $expected); + $this->assertSame(trim($expected), trim($postgresql)); } @@ -31,10 +31,10 @@ final class rewriteTest extends TestCase $sql = 'SELECT COUNT(id), username FROM users'; $expected = 'SELECT COUNT(id) AS count0, username FROM users GROUP BY username'; $postgresql = pg4wp_rewrite($sql); - $this->assertSame($postgresql, $expected); + $this->assertSame(trim($expected), trim($postgresql)); } - public function test_it_handles_auto_increment() + public function test_it_handles_auto_increment() { $sql = <<assertSame(trim($postgresql), trim($expected)); + $this->assertSame(trim($expected), trim($postgresql)); } - public function test_it_handles_auto_increment_without_null() + public function test_it_handles_auto_increment_without_null() { $sql = <<assertSame(trim($postgresql), trim($expected)); + $this->assertSame(trim($expected), trim($postgresql)); } - public function test_it_handles_keys() + public function test_it_handles_keys() { $sql = <<assertSame(trim($postgresql), trim($expected)); + $this->assertSame(trim($expected), trim($postgresql)); } - public function test_it_handles_keys_without_unique() + public function test_it_handles_keys_without_unique() { $sql = <<assertSame(trim($postgresql), trim($expected)); + $this->assertSame(trim($expected), trim($postgresql)); } - public function test_it_does_not_remove_if_not_exists() + public function test_it_does_not_remove_if_not_exists() { $sql = <<assertSame(trim($postgresql), trim($expected)); + $this->assertSame(trim($expected), trim($postgresql)); } - public function test_it_removes_character_sets() + public function test_it_removes_character_sets() { $sql = <<assertSame(trim($postgresql), trim($expected)); + $this->assertSame(trim($expected), trim($postgresql)); } + public function test_it_handles_multiple_keys() + { + $sql = <<assertSame(trim($expected), trim($postgresql)); + } protected function setUp(): void { diff --git a/tests/verifyAgainstStubsTest.php b/tests/verifyAgainstStubsTest.php index 35c56e4..04dd652 100644 --- a/tests/verifyAgainstStubsTest.php +++ b/tests/verifyAgainstStubsTest.php @@ -22,7 +22,7 @@ final class verifyAgainstStubsTest extends TestCase $files = array_diff(scandir(self::STUBS_DIRECTORY), array('.', '..')); foreach($files as $file) { $data = json_decode(file_get_contents(self::STUBS_DIRECTORY . "/" . $file), true); - $this->assertSame(pg4wp_rewrite($data['mysql']), $data['postgresql']); + $this->assertSame($data['postgresql'], pg4wp_rewrite($data['mysql'])); } }