Browse Source

Allow CURRENT_TIMESTAMP for datetime columns - MySQL5.6+

vagrant 10 years ago
parent
commit
90e8fcda68

+ 6 - 2
src/Database/Schema/MysqlSchema.php

@@ -347,14 +347,16 @@ class MysqlSchema extends BaseSchema
             $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 @@ class MysqlSchema extends BaseSchema
             );
             return sprintf('PRIMARY KEY (%s)', implode(', ', $columns));
         }
+
+        $out = '';
         if ($data['type'] === Table::CONSTRAINT_UNIQUE) {
             $out = 'UNIQUE KEY ';
         }

+ 5 - 0
tests/TestCase/Database/Schema/MysqlSchemaTest.php

@@ -568,6 +568,11 @@ SQL;
                 ['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'
+            ],
         ];
     }