add tests for table creation

This commit is contained in:
Matthew Bucci
2023-11-26 01:34:10 -08:00
parent a389cc7adc
commit 91f1612d10
2 changed files with 62 additions and 4 deletions

View File

@ -6,6 +6,7 @@ class CreateTableSQLRewriter extends AbstractSQLRewriter
'bigint(20)' => 'bigint',
'bigint(10)' => 'int',
'int(11)' => 'int',
'int(1)' => 'smallint',
'tinytext' => 'text',
'mediumtext' => 'text',
'longtext' => 'text',
@ -45,22 +46,22 @@ class CreateTableSQLRewriter extends AbstractSQLRewriter
$sql = trim($sql) . ';';
// Translate types and some other replacements
$sql = str_replace(
$sql = str_ireplace(
array_keys($this->stringReplacements),
array_values($this->stringReplacements),
$sql
);
// Fix auto_increment by adding a sequence
$pattern = '/int[ ]+NOT NULL auto_increment/';
$pattern = '/int[ ]+NOT NULL auto_increment/i';
preg_match($pattern, $sql, $matches);
if($matches) {
$seq = $table . '_seq';
$sql = str_replace('NOT NULL auto_increment', "NOT NULL DEFAULT nextval('$seq'::text)", $sql);
$sql = str_ireplace('NOT NULL auto_increment', "NOT NULL DEFAULT nextval('$seq'::text)", $sql);
$sql .= "\nCREATE SEQUENCE $seq;";
}
// Support for INDEX creation
// Support for UNIQUE INDEX creation
$pattern = '/,\s+(UNIQUE |)KEY\s+([^\s]+)\s+\(((?:[\w]+(?:\([\d]+\))?[,]?)*)\)/';
if(preg_match_all($pattern, $sql, $matches, PREG_SET_ORDER)) {
foreach($matches as $match) {
@ -75,6 +76,22 @@ class CreateTableSQLRewriter extends AbstractSQLRewriter
}
// Now remove handled indexes
$sql = preg_replace($pattern, '', $sql);
// Support for PRIMARY INDEX creation
$pattern = '/,\s+(PRIMARY |)KEY\s+\(((?:[\w]+(?:\([\d]+\))?[,]?)*)\)/';
if(preg_match_all($pattern, $sql, $matches, PREG_SET_ORDER)) {
foreach($matches as $match) {
$primary = $match[1];
$columns = $match[2];
$columns = preg_replace('/\(\d+\)/', '', $columns);
$index = $columns;
// Workaround for index name duplicate
$index = $table . '_' . $index;
$sql .= "\nCREATE {$primary}INDEX $index ON $table ($columns);";
}
}
// Now remove handled indexes
$sql = preg_replace($pattern, '', $sql);
return $sql;
}

View File

@ -31,4 +31,45 @@ final class rewriteTest extends TestCase
$postgresql = pg4wp_rewrite($sql);
$this->assertSame($postgresql, $expected);
}
public function test_it_handles_auto_increment()
{
$sql = <<<SQL
CREATE TABLE wp_itsec_lockouts (
lockout_id bigint UNSIGNED NOT NULL AUTO_INCREMENT,
lockout_type varchar(25) NOT NULL,
lockout_start timestamp NOT NULL,
lockout_start_gmt timestamp NOT NULL,
lockout_expire timestamp NOT NULL,
lockout_expire_gmt timestamp NOT NULL,
lockout_host varchar(40),
lockout_user bigint UNSIGNED,
lockout_username varchar(60),
lockout_active int(1) NOT NULL DEFAULT 1,
lockout_context TEXT,
PRIMARY KEY (lockout_id)
)
SQL;
$expected = <<<SQL
CREATE TABLE wp_itsec_lockouts (
lockout_id bigint NOT NULL DEFAULT nextval('wp_itsec_lockouts_seq'::text),
lockout_type varchar(25) NOT NULL,
lockout_start timestamp NOT NULL,
lockout_start_gmt timestamp NOT NULL,
lockout_expire timestamp NOT NULL,
lockout_expire_gmt timestamp NOT NULL,
lockout_host varchar(40),
lockout_user bigint ,
lockout_username varchar(60),
lockout_active smallint NOT NULL DEFAULT 1,
lockout_context TEXT
);
CREATE SEQUENCE wp_itsec_lockouts_seq;
CREATE PRIMARY INDEX wp_itsec_lockouts_lockout_id ON wp_itsec_lockouts (lockout_id);
SQL;
$postgresql = pg4wp_rewrite($sql);
$this->assertSame(trim($postgresql), trim($expected));
}
}