cleanup AlterTableSQLRewriter
This commit is contained in:
@@ -2,39 +2,40 @@
|
||||
|
||||
class CreateTableSQLRewriter 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 utf8mb4' => '',
|
||||
'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 utf8mb4_unicode_520_ci' => '',
|
||||
'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 utf8mb4' => '',
|
||||
'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 utf8mb4_unicode_520_ci' => '',
|
||||
'COLLATE utf8_general_ci' => '',
|
||||
|
||||
// For flash-album-gallery plugin
|
||||
'tinyint' => 'smallint',
|
||||
);
|
||||
|
||||
$sql = $this->original();
|
||||
|
||||
|
||||
$sql = str_replace('CREATE TABLE IF NOT EXISTS ', 'CREATE TABLE ', $sql);
|
||||
$pattern = '/CREATE TABLE [`]?(\w+)[`]?/';
|
||||
preg_match($pattern, $sql, $matches);
|
||||
@@ -45,8 +46,8 @@ class CreateTableSQLRewriter extends AbstractSQLRewriter
|
||||
|
||||
// Translate types and some other replacements
|
||||
$sql = str_replace(
|
||||
array_keys($typeTranslations),
|
||||
array_values($typeTranslations),
|
||||
array_keys($this->stringReplacements),
|
||||
array_values($this->stringReplacements),
|
||||
$sql
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user