pgrewrite should not remove IF NOT EXISTS

This commit is contained in:
mattbucci
2024-01-08 04:13:06 +00:00
parent 200dfaa0b2
commit aca566b8bf
2 changed files with 30 additions and 2 deletions

View File

@ -39,9 +39,9 @@ class CreateTableSQLRewriter extends AbstractSQLRewriter
$sql = $this->original();
$sql = str_replace('CREATE TABLE IF NOT EXISTS ', 'CREATE TABLE ', $sql);
$tableSQL = str_replace('CREATE TABLE IF NOT EXISTS ', 'CREATE TABLE ', $sql);
$pattern = '/CREATE TABLE [`]?(\w+)[`]?/';
preg_match($pattern, $sql, $matches);
preg_match($pattern, $tableSQL, $matches);
$table = $matches[1];
// Remove trailing spaces

View File

@ -167,6 +167,34 @@ final class rewriteTest extends TestCase
$this->assertSame(trim($postgresql), trim($expected));
}
public function test_it_does_not_remove_if_not_exists()
{
$sql = <<<SQL
CREATE TABLE IF NOT EXISTS 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 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)
);
SQL;
$postgresql = pg4wp_rewrite($sql);
$this->assertSame(trim($postgresql), trim($expected));
}
protected function setUp(): void
{
global $wpdb;