mirror of
https://github.com/PostgreSQL-For-Wordpress/postgresql-for-wordpress.git
synced 2025-07-31 10:17:13 +02:00
add tests for table creation
This commit is contained in:
@ -6,6 +6,7 @@ class CreateTableSQLRewriter extends AbstractSQLRewriter
|
|||||||
'bigint(20)' => 'bigint',
|
'bigint(20)' => 'bigint',
|
||||||
'bigint(10)' => 'int',
|
'bigint(10)' => 'int',
|
||||||
'int(11)' => 'int',
|
'int(11)' => 'int',
|
||||||
|
'int(1)' => 'smallint',
|
||||||
'tinytext' => 'text',
|
'tinytext' => 'text',
|
||||||
'mediumtext' => 'text',
|
'mediumtext' => 'text',
|
||||||
'longtext' => 'text',
|
'longtext' => 'text',
|
||||||
@ -45,22 +46,22 @@ class CreateTableSQLRewriter extends AbstractSQLRewriter
|
|||||||
$sql = trim($sql) . ';';
|
$sql = trim($sql) . ';';
|
||||||
|
|
||||||
// Translate types and some other replacements
|
// Translate types and some other replacements
|
||||||
$sql = str_replace(
|
$sql = str_ireplace(
|
||||||
array_keys($this->stringReplacements),
|
array_keys($this->stringReplacements),
|
||||||
array_values($this->stringReplacements),
|
array_values($this->stringReplacements),
|
||||||
$sql
|
$sql
|
||||||
);
|
);
|
||||||
|
|
||||||
// Fix auto_increment by adding a sequence
|
// 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);
|
preg_match($pattern, $sql, $matches);
|
||||||
if($matches) {
|
if($matches) {
|
||||||
$seq = $table . '_seq';
|
$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;";
|
$sql .= "\nCREATE SEQUENCE $seq;";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Support for INDEX creation
|
// Support for UNIQUE INDEX creation
|
||||||
$pattern = '/,\s+(UNIQUE |)KEY\s+([^\s]+)\s+\(((?:[\w]+(?:\([\d]+\))?[,]?)*)\)/';
|
$pattern = '/,\s+(UNIQUE |)KEY\s+([^\s]+)\s+\(((?:[\w]+(?:\([\d]+\))?[,]?)*)\)/';
|
||||||
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) {
|
||||||
@ -75,6 +76,22 @@ class CreateTableSQLRewriter extends AbstractSQLRewriter
|
|||||||
}
|
}
|
||||||
// Now remove handled indexes
|
// Now remove handled indexes
|
||||||
$sql = preg_replace($pattern, '', $sql);
|
$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;
|
return $sql;
|
||||||
}
|
}
|
||||||
|
@ -31,4 +31,45 @@ final class rewriteTest extends TestCase
|
|||||||
$postgresql = pg4wp_rewrite($sql);
|
$postgresql = pg4wp_rewrite($sql);
|
||||||
$this->assertSame($postgresql, $expected);
|
$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));
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user