Skip to content

Commit

Permalink
cleanup AlterTableSQLRewriter
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbucci committed Nov 7, 2023
1 parent 8a19f4c commit a810cfb
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 68 deletions.
136 changes: 99 additions & 37 deletions pg4wp/rewriters/AlterTableSQLRewriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,77 @@

class AlterTableSQLRewriter extends AbstractSQLRewriter
{
private $stringReplacements = [
'bigint(20)' => 'bigint',
'bigint(10)' => 'int',
'int(11)' => 'int',
'tinytext' => 'text',
'mediumtext' => 'text',
'longtext' => 'text',
'unsigned' => '',
'gmt datetime NOT NULL default \'0000-00-00 00:00:00\'' => 'gmt timestamp NOT NULL DEFAULT timezone(\'gmt\'::text, now())',
'default \'0000-00-00 00:00:00\'' => 'DEFAULT now()',
'\'0000-00-00 00:00:00\'' => 'now()',
'datetime' => 'timestamp',
'DEFAULT CHARACTER SET utf8' => '',

// WP 2.7.1 compatibility
'int(4)' => 'smallint',

// For WPMU (starting with WP 3.2)
'tinyint(2)' => 'smallint',
'tinyint(1)' => 'smallint',
"enum('0','1')" => 'smallint',
'COLLATE utf8_general_ci' => '',

// For flash-album-gallery plugin
'tinyint' => 'smallint'
];

public function rewrite(): string
{
// List of types translations (the key is the mysql one, the value is the text to use instead)
$typeTranslations = array(
'bigint(20)' => 'bigint',
'bigint(10)' => 'int',
'int(11)' => 'int',
'tinytext' => 'text',
'mediumtext' => 'text',
'longtext' => 'text',
'unsigned' => '',
'gmt datetime NOT NULL default \'0000-00-00 00:00:00\'' => 'gmt timestamp NOT NULL DEFAULT timezone(\'gmt\'::text, now())',
'default \'0000-00-00 00:00:00\'' => 'DEFAULT now()',
'\'0000-00-00 00:00:00\'' => 'now()',
'datetime' => 'timestamp',
'DEFAULT CHARACTER SET utf8' => '',

// WP 2.7.1 compatibility
'int(4)' => 'smallint',

// For WPMU (starting with WP 3.2)
'tinyint(2)' => 'smallint',
'tinyint(1)' => 'smallint',
"enum('0','1')" => 'smallint',
'COLLATE utf8_general_ci' => '',

// For flash-album-gallery plugin
'tinyint' => 'smallint',
);
$sql = $this->original();

if (str_contains($sql, 'CHANGE COLUMN')) {
$sql = $this->rewriteChangeColumn($sql);
}
if (str_contains($sql, 'ALTER COLUMN')) {
$sql = $this->rewriteAlterColumn($sql);
}
if (str_contains($sql, 'ADD COLUMN')) {
$sql = $this->rewriteAddColumn($sql);
}
if (str_contains($sql, 'ADD KEY') || str_contains($sql, 'ADD UNIQUE KEY')) {
$sql = $this->rewriteAddKey($sql);
}
if (str_contains($sql, 'DROP INDEX')) {
$sql = $this->rewriteDropIndex($sql);
}
if (str_contains($sql, 'DROP PRIMARY KEY')) {
$sql = $this->rewriteDropPrimaryKey($sql);
}

return $sql;
}

private function rewriteChangeColumn(string $sql): string
{
$pattern = '/ALTER TABLE\s+(\w+)\s+CHANGE COLUMN\s+([^\s]+)\s+([^\s]+)\s+([^ ]+)( unsigned|)\s*(NOT NULL|)\s*(default (.+)|)/';

if(1 === preg_match($pattern, $sql, $matches)) {
$table = $matches[1];
$col = $matches[2];
$newname = $matches[3];
$type = strtolower($matches[4]);
if(isset($typeTranslations[$type])) {
$type = $typeTranslations[$type];
if(isset($this->stringReplacements[$type])) {
$type = $this->stringReplacements[$type];
}
$unsigned = $matches[5];
$notnull = $matches[6];
$default = $matches[7];
$defval = $matches[8];
if(isset($typeTranslations[$defval])) {
$defval = $typeTranslations[$defval];
if(isset($this->stringReplacements[$defval])) {
$defval = $this->stringReplacements[$defval];
}
$newq = "ALTER TABLE $table ALTER COLUMN $col TYPE $type";
if(!empty($notnull)) {
Expand All @@ -59,29 +86,43 @@ public function rewrite(): string
}
$sql = $newq;
}

return $sql;
}

private function rewriteAlterColumn(string $sql): string
{
$pattern = '/ALTER TABLE\s+(\w+)\s+ALTER COLUMN\s+/';

if(1 === preg_match($pattern, $sql)) {
// Translate default values
$sql = str_replace(
array_keys($typeTranslations),
array_values($typeTranslations),
array_keys($this->stringReplacements),
array_values($this->stringReplacements),
$sql
);
}

return $sql;
}

private function rewriteAddColumn(string $sql): string
{
$pattern = '/ALTER TABLE\s+(\w+)\s+ADD COLUMN\s+([^\s]+)\s+([^ ]+)( unsigned|)\s+(NOT NULL|)\s*(default (.+)|)/';

if(1 === preg_match($pattern, $sql, $matches)) {
$table = $matches[1];
$col = $matches[2];
$type = strtolower($matches[3]);
if(isset($typeTranslations[$type])) {
$type = $typeTranslations[$type];
if(isset($this->stringReplacements[$type])) {
$type = $this->stringReplacements[$type];
}
$unsigned = $matches[4];
$notnull = $matches[5];
$default = $matches[6];
$defval = $matches[7];
if(isset($typeTranslations[$defval])) {
$defval = $typeTranslations[$defval];
if(isset($this->stringReplacements[$defval])) {
$defval = $this->stringReplacements[$defval];
}
$newq = "ALTER TABLE $table ADD COLUMN $col $type";
if(!empty($default)) {
Expand All @@ -92,7 +133,14 @@ public function rewrite(): string
}
$sql = $newq;
}

return $sql;
}

private function rewriteAddKey(string $sql): string
{
$pattern = '/ALTER TABLE\s+(\w+)\s+ADD (UNIQUE |)KEY\s+([^\s]+)\s+\(((?:[^\(\)]+|\([^\(\)]+\))+)\)/';

if(1 === preg_match($pattern, $sql, $matches)) {
$table = $matches[1];
$unique = $matches[2];
Expand All @@ -107,13 +155,27 @@ public function rewrite(): string
$index = $table . '_' . $index;
$sql = "CREATE {$unique}INDEX $index ON $table ($columns)";
}

return $sql;
}

private function rewriteDropIndex(string $sql): string
{
$pattern = '/ALTER TABLE\s+(\w+)\s+DROP INDEX\s+([^\s]+)/';

if(1 === preg_match($pattern, $sql, $matches)) {
$table = $matches[1];
$index = $matches[2];
$sql = "DROP INDEX ${table}_${index}";
}

return $sql;
}

private function rewriteDropPrimaryKey(string $sql): string
{
$pattern = '/ALTER TABLE\s+(\w+)\s+DROP PRIMARY KEY/';

if(1 === preg_match($pattern, $sql, $matches)) {
$table = $matches[1];
$sql = "ALTER TABLE ${table} DROP CONSTRAINT ${table}_pkey";
Expand Down
63 changes: 32 additions & 31 deletions pg4wp/rewriters/CreateTableSQLRewriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,40 @@

class CreateTableSQLRewriter extends AbstractSQLRewriter
{
public function rewrite(): string
{
// List of types translations (the key is the mysql one, the value is the text to use instead)
$typeTranslations = array(
'bigint(20)' => 'bigint',
'bigint(10)' => 'int',
'int(11)' => 'int',
'tinytext' => 'text',
'mediumtext' => 'text',
'longtext' => 'text',
'unsigned' => '',
'gmt datetime NOT NULL default \'0000-00-00 00:00:00\'' => 'gmt timestamp NOT NULL DEFAULT timezone(\'gmt\'::text, now())',
'default \'0000-00-00 00:00:00\'' => 'DEFAULT now()',
'\'0000-00-00 00:00:00\'' => 'now()',
'datetime' => 'timestamp',
'DEFAULT CHARACTER SET utf8mb4' => '',
'DEFAULT CHARACTER SET utf8' => '',
private $stringReplacements = [
'bigint(20)' => 'bigint',
'bigint(10)' => 'int',
'int(11)' => 'int',
'tinytext' => 'text',
'mediumtext' => 'text',
'longtext' => 'text',
'unsigned' => '',
'gmt datetime NOT NULL default \'0000-00-00 00:00:00\'' => 'gmt timestamp NOT NULL DEFAULT timezone(\'gmt\'::text, now())',
'default \'0000-00-00 00:00:00\'' => 'DEFAULT now()',
'\'0000-00-00 00:00:00\'' => 'now()',
'datetime' => 'timestamp',
'DEFAULT CHARACTER SET utf8mb4' => '',
'DEFAULT CHARACTER SET utf8' => '',

// WP 2.7.1 compatibility
'int(4)' => 'smallint',
// WP 2.7.1 compatibility
'int(4)' => 'smallint',

// For WPMU (starting with WP 3.2)
'tinyint(2)' => 'smallint',
'tinyint(1)' => 'smallint',
"enum('0','1')" => 'smallint',
'COLLATE utf8mb4_unicode_520_ci' => '',
'COLLATE utf8_general_ci' => '',

// For flash-album-gallery plugin
'tinyint' => 'smallint',
);
// For WPMU (starting with WP 3.2)
'tinyint(2)' => 'smallint',
'tinyint(1)' => 'smallint',
"enum('0','1')" => 'smallint',
'COLLATE utf8mb4_unicode_520_ci' => '',
'COLLATE utf8_general_ci' => '',

// For flash-album-gallery plugin
'tinyint' => 'smallint'
];

public function rewrite(): string
{
$sql = $this->original();


$sql = str_replace('CREATE TABLE IF NOT EXISTS ', 'CREATE TABLE ', $sql);
$pattern = '/CREATE TABLE [`]?(\w+)[`]?/';
preg_match($pattern, $sql, $matches);
Expand All @@ -45,8 +46,8 @@ public function rewrite(): string

// Translate types and some other replacements
$sql = str_replace(
array_keys($typeTranslations),
array_values($typeTranslations),
array_keys($this->stringReplacements),
array_values($this->stringReplacements),
$sql
);

Expand Down

0 comments on commit a810cfb

Please sign in to comment.