Rewrite default '0000-00-00 00:00:00' to now()

The current rewriting changes "default '0000-00-00 00:00:00'" when it
appears in CREATE TABLE statements, but does not handle the case that a
column default value is modified.  This results in errors such as the
following during schema upgrade:

Error running :
ALTER TABLE wp_users ALTER COLUMN user_registered SET DEFAULT '0000-00-00 00:00:00'
---- converted to ----
ALTER TABLE wp_users ALTER COLUMN user_registered SET DEFAULT '0000-00-00 00:00:00'
----> ERROR:  date/time field value out of range: "0000-00-00 00:00:00"

Apply the conversion to ALTER TABLE statements as well.

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
This commit is contained in:
Kevin Locke
2015-06-03 00:51:25 -06:00
parent 935a99c429
commit f97491346b

View File

@@ -20,6 +20,7 @@
'unsigned' => '', 'unsigned' => '',
'gmt datetime NOT NULL default \'0000-00-00 00:00:00\'' => 'gmt timestamp NOT NULL DEFAULT timezone(\'gmt\'::text, now())', '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()', 'default \'0000-00-00 00:00:00\'' => 'DEFAULT now()',
'\'0000-00-00 00:00:00\'' => 'now()',
'datetime' => 'timestamp', 'datetime' => 'timestamp',
'DEFAULT CHARACTER SET utf8' => '', 'DEFAULT CHARACTER SET utf8' => '',
@@ -86,6 +87,13 @@ WHERE bc.oid = i.indrelid
$newq .= ";ALTER TABLE $table RENAME COLUMN $col TO $newcol;"; $newq .= ";ALTER TABLE $table RENAME COLUMN $col TO $newcol;";
$sql = $newq; $sql = $newq;
} }
$pattern = '/ALTER TABLE\s+(\w+)\s+ALTER COLUMN\s+/';
if( 1 === preg_match( $pattern, $sql))
{
// Translate default values
$sql = str_replace(
array_keys($GLOBALS['pg4wp_ttr']), array_values($GLOBALS['pg4wp_ttr']), $sql);
}
$pattern = '/ALTER TABLE\s+(\w+)\s+ADD COLUMN\s+([^\s]+)\s+([^ ]+)( unsigned|)\s+(NOT NULL|)\s*(default (.+)|)/'; $pattern = '/ALTER TABLE\s+(\w+)\s+ADD COLUMN\s+([^\s]+)\s+([^ ]+)( unsigned|)\s+(NOT NULL|)\s*(default (.+)|)/';
if( 1 === preg_match( $pattern, $sql, $matches)) if( 1 === preg_match( $pattern, $sql, $matches))
{ {