handle unique keys

This commit is contained in:
Matthew Bucci
2023-11-26 15:17:26 -08:00
parent fd2467c00b
commit f3a71be3f0
2 changed files with 36 additions and 0 deletions

View File

@ -98,6 +98,11 @@ class CreateTableSQLRewriter extends AbstractSQLRewriter
// Now remove handled indexes
$sql = preg_replace($pattern, '', $sql);
$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

@ -93,4 +93,35 @@ final class rewriteTest extends TestCase
$postgresql = pg4wp_rewrite($sql);
$this->assertSame(trim($postgresql), trim($expected));
}
public function test_it_handles_keys()
{
$sql = <<<SQL
CREATE TABLE wp_itsec_dashboard_lockouts (
id int NOT NULL AUTO_INCREMENT,
ip varchar(40),
time timestamp NOT NULL,
count int NOT NULL,
PRIMARY KEY (id),
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)
);
SQL;
$postgresql = pg4wp_rewrite($sql);
$this->assertSame(trim($postgresql), trim($expected));
}
}