From f97491346b550f16977921eb2b259fa96712db77 Mon Sep 17 00:00:00 2001 From: Kevin Locke Date: Wed, 3 Jun 2015 00:51:25 -0600 Subject: [PATCH] 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 --- pg4wp/driver_pgsql_install.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pg4wp/driver_pgsql_install.php b/pg4wp/driver_pgsql_install.php index ceb3e18..7b5f1d9 100644 --- a/pg4wp/driver_pgsql_install.php +++ b/pg4wp/driver_pgsql_install.php @@ -20,6 +20,7 @@ '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' => '', @@ -86,6 +87,13 @@ WHERE bc.oid = i.indrelid $newq .= ";ALTER TABLE $table RENAME COLUMN $col TO $newcol;"; $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 (.+)|)/'; if( 1 === preg_match( $pattern, $sql, $matches)) {