mirror of
https://github.com/PostgreSQL-For-Wordpress/postgresql-for-wordpress.git
synced 2025-07-29 17:27:13 +02:00
switch to serial numeric types
This commit is contained in:
@ -52,13 +52,34 @@ class CreateTableSQLRewriter extends AbstractSQLRewriter
|
||||
$sql
|
||||
);
|
||||
|
||||
// Fix auto_increment by adding a sequence
|
||||
$pattern = '/int[ ]+NOT NULL auto_increment/i';
|
||||
// bigint
|
||||
$pattern = '/bigint(\(\d+\))?([ ]*NOT NULL)?[ ]*auto_increment/i';
|
||||
preg_match($pattern, $sql, $matches);
|
||||
if($matches) {
|
||||
$seq = $table . '_seq';
|
||||
$sql = str_ireplace('NOT NULL auto_increment', "NOT NULL DEFAULT nextval('$seq'::text)", $sql);
|
||||
$sql .= "\nCREATE SEQUENCE $seq;";
|
||||
$sql = preg_replace($pattern, 'bigserial', $sql);
|
||||
}
|
||||
|
||||
// int
|
||||
$pattern = '/int(\(\d+\))?([ ]*NOT NULL)?[ ]*auto_increment/i';
|
||||
preg_match($pattern, $sql, $matches);
|
||||
if($matches) {
|
||||
$sql = preg_replace($pattern, 'serial', $sql);
|
||||
}
|
||||
|
||||
// smallint
|
||||
$pattern = '/smallint(\(\d+\))?([ ]*NOT NULL)?[ ]*auto_increment/i';
|
||||
preg_match($pattern, $sql, $matches);
|
||||
if($matches) {
|
||||
$sql = preg_replace($pattern, 'smallserial', $sql);
|
||||
}
|
||||
|
||||
// Handle for numeric and decimal -- being replaced with serial
|
||||
$numeric_patterns = ['/numeric(\(\d+\))?([ ]*NOT NULL)?[ ]*auto_increment/i', '/decimal(\(\d+\))?([ ]*NOT NULL)?[ ]*auto_increment/i'];
|
||||
foreach($numeric_patterns as $pattern) {
|
||||
preg_match($pattern, $sql, $matches);
|
||||
if($matches) {
|
||||
$sql = preg_replace($pattern, 'serial', $sql);
|
||||
}
|
||||
}
|
||||
|
||||
// Support for UNIQUE INDEX creation
|
||||
|
@ -53,7 +53,7 @@ final class rewriteTest extends TestCase
|
||||
|
||||
$expected = <<<SQL
|
||||
CREATE TABLE wp_itsec_lockouts (
|
||||
lockout_id bigint NOT NULL DEFAULT nextval('wp_itsec_lockouts_seq'::text),
|
||||
lockout_id bigserial,
|
||||
lockout_type varchar(25) NOT NULL,
|
||||
lockout_start timestamp NOT NULL,
|
||||
lockout_start_gmt timestamp NOT NULL,
|
||||
@ -66,7 +66,28 @@ final class rewriteTest extends TestCase
|
||||
lockout_context TEXT,
|
||||
PRIMARY KEY (lockout_id)
|
||||
);
|
||||
CREATE SEQUENCE wp_itsec_lockouts_seq;
|
||||
SQL;
|
||||
|
||||
$postgresql = pg4wp_rewrite($sql);
|
||||
$this->assertSame(trim($postgresql), trim($expected));
|
||||
}
|
||||
|
||||
public function test_it_handles_auto_increment_without_null()
|
||||
{
|
||||
$sql = <<<SQL
|
||||
CREATE TABLE wp_e_events (
|
||||
id bigint auto_increment primary key,
|
||||
event_data text null,
|
||||
created_at timestamp not null
|
||||
)
|
||||
SQL;
|
||||
|
||||
$expected = <<<SQL
|
||||
CREATE TABLE wp_e_events (
|
||||
id bigserial primary key,
|
||||
event_data text null,
|
||||
created_at timestamp not null
|
||||
);
|
||||
SQL;
|
||||
|
||||
$postgresql = pg4wp_rewrite($sql);
|
||||
|
Reference in New Issue
Block a user