Add support for keys with multiple values

This commit is contained in:
mattbucci
2024-01-08 05:00:42 +00:00
parent 5f27c893b3
commit 53125aec2d
3 changed files with 78 additions and 31 deletions

View File

@@ -86,16 +86,24 @@ class CreateTableSQLRewriter extends AbstractSQLRewriter
} }
// Support for UNIQUE INDEX creation // 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)) { if(preg_match_all($pattern, $sql, $matches, PREG_SET_ORDER)) {
foreach($matches as $match) { foreach($matches as $match) {
$unique = $match[1]; $unique = $match[1];
$index = $match[2]; $index = $match[2];
$columns = $match[3]; $columns = $match[3];
$columns = preg_replace('/\(\d+\)/', '', $columns);
// Workaround for index name duplicate // Removing backticks from the index names
$index = $table . '_' . $index; $index = str_replace('`', '', $index);
$sql .= "\nCREATE {$unique}INDEX $index ON $table ($columns);";
// 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 // Now remove handled indexes

View File

@@ -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\''; $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\''; $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); $postgresql = pg4wp_rewrite($sql);
$this->assertSame($postgresql, $expected); $this->assertSame(trim($expected), trim($postgresql));
} }
@@ -31,7 +31,7 @@ final class rewriteTest extends TestCase
$sql = 'SELECT COUNT(id), username FROM users'; $sql = 'SELECT COUNT(id), username FROM users';
$expected = 'SELECT COUNT(id) AS count0, username FROM users GROUP BY username'; $expected = 'SELECT COUNT(id) AS count0, username FROM users GROUP BY username';
$postgresql = pg4wp_rewrite($sql); $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()
@@ -71,7 +71,7 @@ final class rewriteTest extends TestCase
SQL; SQL;
$postgresql = pg4wp_rewrite($sql); $postgresql = pg4wp_rewrite($sql);
$this->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()
@@ -93,7 +93,7 @@ final class rewriteTest extends TestCase
SQL; SQL;
$postgresql = pg4wp_rewrite($sql); $postgresql = pg4wp_rewrite($sql);
$this->assertSame(trim($postgresql), trim($expected)); $this->assertSame(trim($expected), trim($postgresql));
} }
@@ -116,13 +116,13 @@ final class rewriteTest extends TestCase
ip varchar(40), ip varchar(40),
time timestamp NOT NULL, time timestamp NOT NULL,
count int NOT NULL, count int NOT NULL,
PRIMARY KEY (id), PRIMARY KEY (id)
UNIQUE (ip, time)
); );
CREATE UNIQUE INDEX wp_itsec_dashboard_lockouts_ip__time ON wp_itsec_dashboard_lockouts (ip, time);
SQL; SQL;
$postgresql = pg4wp_rewrite($sql); $postgresql = pg4wp_rewrite($sql);
$this->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()
@@ -164,7 +164,7 @@ final class rewriteTest extends TestCase
SQL; SQL;
$postgresql = pg4wp_rewrite($sql); $postgresql = pg4wp_rewrite($sql);
$this->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()
@@ -186,13 +186,13 @@ final class rewriteTest extends TestCase
ip varchar(40), ip varchar(40),
time timestamp NOT NULL, time timestamp NOT NULL,
count int NOT NULL, count int NOT NULL,
PRIMARY KEY (id), PRIMARY KEY (id)
UNIQUE (ip, time)
); );
CREATE UNIQUE INDEX wp_itsec_dashboard_lockouts_ip__time ON wp_itsec_dashboard_lockouts (ip, time);
SQL; SQL;
$postgresql = pg4wp_rewrite($sql); $postgresql = pg4wp_rewrite($sql);
$this->assertSame(trim($postgresql), trim($expected)); $this->assertSame(trim($expected), trim($postgresql));
} }
@@ -237,9 +237,48 @@ final class rewriteTest extends TestCase
SQL; SQL;
$postgresql = pg4wp_rewrite($sql); $postgresql = pg4wp_rewrite($sql);
$this->assertSame(trim($postgresql), trim($expected)); $this->assertSame(trim($expected), trim($postgresql));
} }
public function test_it_handles_multiple_keys()
{
$sql = <<<SQL
CREATE TABLE wp_statistics_pages (
page_id BIGINT(20) NOT NULL AUTO_INCREMENT,
uri varchar(190) NOT NULL,
type varchar(180) NOT NULL,
date date NOT NULL,
count int(11) NOT NULL,
id int(11) NOT NULL,
UNIQUE KEY date_2 (date,uri),
KEY url (uri),
KEY date (date),
KEY id (id),
KEY `uri` (`uri`,`count`,`id`),
PRIMARY KEY (`page_id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci
SQL;
$expected = <<<SQL
CREATE TABLE wp_statistics_pages (
page_id bigserial,
uri varchar(190) NOT NULL,
type varchar(180) NOT NULL,
date date NOT NULL,
count int NOT NULL,
id int NOT NULL,
PRIMARY KEY (page_id)
);
CREATE UNIQUE INDEX wp_statistics_pages_date_2 ON wp_statistics_pages (date,uri);
CREATE INDEX wp_statistics_pages_url ON wp_statistics_pages (uri);
CREATE INDEX wp_statistics_pages_date ON wp_statistics_pages (date);
CREATE INDEX wp_statistics_pages_id ON wp_statistics_pages (id);
CREATE INDEX wp_statistics_pages_uri ON wp_statistics_pages (uri,count,id);
SQL;
$postgresql = pg4wp_rewrite($sql);
$this->assertSame(trim($expected), trim($postgresql));
}
protected function setUp(): void protected function setUp(): void
{ {

View File

@@ -22,7 +22,7 @@ final class verifyAgainstStubsTest extends TestCase
$files = array_diff(scandir(self::STUBS_DIRECTORY), array('.', '..')); $files = array_diff(scandir(self::STUBS_DIRECTORY), array('.', '..'));
foreach($files as $file) { foreach($files as $file) {
$data = json_decode(file_get_contents(self::STUBS_DIRECTORY . "/" . $file), true); $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']));
} }
} }