From 90e8fcda68590e99ac3c889ea0d1ee9fd2dd5a81 Mon Sep 17 00:00:00 2001 From: vagrant Date: Fri, 18 Sep 2015 09:42:10 +0000 Subject: [PATCH] Allow CURRENT_TIMESTAMP for datetime columns - MySQL5.6+ --- src/Database/Schema/MysqlSchema.php | 8 ++++++-- tests/TestCase/Database/Schema/MysqlSchemaTest.php | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Database/Schema/MysqlSchema.php b/src/Database/Schema/MysqlSchema.php index 5001427e5c4..53225f246b5 100644 --- a/src/Database/Schema/MysqlSchema.php +++ b/src/Database/Schema/MysqlSchema.php @@ -347,14 +347,16 @@ public function columnSql(Table $table, $name) $out .= $data['type'] === 'timestamp' ? ' NULL' : ' DEFAULT NULL'; unset($data['default']); } - if (isset($data['default']) && $data['type'] !== 'timestamp') { + if (isset($data['default']) && !in_array($data['type'], ['timestamp', 'datetime'])) { $out .= ' DEFAULT ' . $this->_driver->schemaValue($data['default']); + unset($data['default']); } if (isset($data['default']) && - $data['type'] === 'timestamp' && + in_array($data['type'], ['timestamp', 'datetime']) && strtolower($data['default']) === 'current_timestamp' ) { $out .= ' DEFAULT CURRENT_TIMESTAMP'; + unset($data['default']); } if (isset($data['comment']) && $data['comment'] !== '') { $out .= ' COMMENT ' . $this->_driver->schemaValue($data['comment']); @@ -375,6 +377,8 @@ public function constraintSql(Table $table, $name) ); return sprintf('PRIMARY KEY (%s)', implode(', ', $columns)); } + + $out = ''; if ($data['type'] === Table::CONSTRAINT_UNIQUE) { $out = 'UNIQUE KEY '; } diff --git a/tests/TestCase/Database/Schema/MysqlSchemaTest.php b/tests/TestCase/Database/Schema/MysqlSchemaTest.php index 4bf3568593f..2248ab50c78 100644 --- a/tests/TestCase/Database/Schema/MysqlSchemaTest.php +++ b/tests/TestCase/Database/Schema/MysqlSchemaTest.php @@ -568,6 +568,11 @@ public static function columnSqlProvider() ['type' => 'timestamp', 'null' => false, 'default' => 'current_timestamp'], '`created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP' ], + [ + 'created', + ['type' => 'datetime', 'null' => false, 'default' => 'current_timestamp'], + '`created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP' + ], ]; }