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

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\'';
$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 = <<<SQL
CREATE TABLE wp_itsec_lockouts (
@ -52,7 +52,7 @@ final class rewriteTest extends TestCase
PRIMARY KEY (lockout_id)
)
SQL;
$expected = <<<SQL
CREATE TABLE wp_itsec_lockouts (
lockout_id bigserial,
@ -71,10 +71,10 @@ final class rewriteTest extends TestCase
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()
{
$sql = <<<SQL
CREATE TABLE wp_e_events (
@ -83,7 +83,7 @@ final class rewriteTest extends TestCase
created_at timestamp not null
)
SQL;
$expected = <<<SQL
CREATE TABLE wp_e_events (
id bigserial primary key,
@ -93,11 +93,11 @@ final class rewriteTest extends TestCase
SQL;
$postgresql = pg4wp_rewrite($sql);
$this->assertSame(trim($postgresql), trim($expected));
$this->assertSame(trim($expected), trim($postgresql));
}
public function test_it_handles_keys()
public function test_it_handles_keys()
{
$sql = <<<SQL
CREATE TABLE wp_itsec_dashboard_lockouts (
@ -109,23 +109,23 @@ final class rewriteTest extends TestCase
UNIQUE KEY ip__time (ip, time)
)
SQL;
$expected = <<<SQL
CREATE TABLE wp_itsec_dashboard_lockouts (
id serial,
ip varchar(40),
time timestamp NOT NULL,
count int NOT NULL,
PRIMARY KEY (id),
UNIQUE (ip, time)
PRIMARY KEY (id)
);
CREATE UNIQUE INDEX wp_itsec_dashboard_lockouts_ip__time ON wp_itsec_dashboard_lockouts (ip, time);
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()
{
$sql = <<<SQL
CREATE TABLE wp_itsec_vulnerabilities (
@ -144,7 +144,7 @@ final class rewriteTest extends TestCase
KEY last_seen (last_seen)
)
SQL;
$expected = <<<SQL
CREATE TABLE wp_itsec_vulnerabilities (
id varchar(128) NOT NULL,
@ -164,10 +164,10 @@ final class rewriteTest extends TestCase
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()
{
$sql = <<<SQL
CREATE TABLE IF NOT EXISTS wp_itsec_dashboard_lockouts (
@ -179,24 +179,24 @@ final class rewriteTest extends TestCase
UNIQUE KEY ip__time (ip, time)
)
SQL;
$expected = <<<SQL
CREATE TABLE IF NOT EXISTS wp_itsec_dashboard_lockouts (
id serial,
ip varchar(40),
time timestamp NOT NULL,
count int NOT NULL,
PRIMARY KEY (id),
UNIQUE (ip, time)
PRIMARY KEY (id)
);
CREATE UNIQUE INDEX wp_itsec_dashboard_lockouts_ip__time ON wp_itsec_dashboard_lockouts (ip, time);
SQL;
$postgresql = pg4wp_rewrite($sql);
$this->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 = <<<SQL
CREATE TABLE wp_statistics_useronline (
@ -216,7 +216,7 @@ final class rewriteTest extends TestCase
PRIMARY KEY (ID)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci
SQL;
$expected = <<<SQL
CREATE TABLE wp_statistics_useronline (
"ID" bigserial,
@ -237,9 +237,48 @@ final class rewriteTest extends TestCase
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
{

View File

@ -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']));
}
}