Browse Source

Merge pull request #9853 from cakephp/issue-9850

Fix default values for datetime columns
José Lorenzo Rodríguez 9 years ago
parent
commit
77d0465dd7

+ 4 - 4
src/Database/Schema/MysqlSchema.php

@@ -393,10 +393,6 @@ class MysqlSchema extends BaseSchema
             $out .= ' NULL';
             unset($data['default']);
         }
-        if (isset($data['default']) && !in_array($data['type'], ['timestamp', 'datetime'])) {
-            $out .= ' DEFAULT ' . $this->_driver->schemaValue($data['default']);
-            unset($data['default']);
-        }
         if (isset($data['default']) &&
             in_array($data['type'], ['timestamp', 'datetime']) &&
             strtolower($data['default']) === 'current_timestamp'
@@ -404,6 +400,10 @@ class MysqlSchema extends BaseSchema
             $out .= ' DEFAULT CURRENT_TIMESTAMP';
             unset($data['default']);
         }
+        if (isset($data['default'])) {
+            $out .= ' DEFAULT ' . $this->_driver->schemaValue($data['default']);
+            unset($data['default']);
+        }
         if (isset($data['comment']) && $data['comment'] !== '') {
             $out .= ' COMMENT ' . $this->_driver->schemaValue($data['comment']);
         }

+ 8 - 1
src/Database/Schema/SqlserverSchema.php

@@ -407,7 +407,14 @@ class SqlserverSchema extends BaseSchema
             unset($data['default']);
         }
 
-        if (isset($data['default']) && $data['type'] !== 'datetime') {
+        if (isset($data['default']) &&
+            in_array($data['type'], ['timestamp', 'datetime']) &&
+            strtolower($data['default']) === 'current_timestamp'
+        ) {
+            $out .= ' DEFAULT CURRENT_TIMESTAMP';
+            unset($data['default']);
+        }
+        if (isset($data['default'])) {
             $default = is_bool($data['default']) ? (int)$data['default'] : $this->_driver->schemaValue($data['default']);
             $out .= ' DEFAULT ' . $default;
         }

+ 17 - 3
tests/TestCase/Database/Schema/MysqlSchemaTest.php

@@ -60,6 +60,10 @@ class MysqlSchemaTest extends TestCase
                 ['type' => 'time', 'length' => null]
             ],
             [
+                'TIMESTAMP',
+                ['type' => 'timestamp', 'length' => null]
+            ],
+            [
                 'TINYINT(1)',
                 ['type' => 'boolean', 'length' => null]
             ],
@@ -652,6 +656,16 @@ SQL;
                 ['type' => 'datetime', 'comment' => 'Created timestamp'],
                 '`created` DATETIME COMMENT \'Created timestamp\''
             ],
+            [
+                'created',
+                ['type' => 'datetime', 'null' => false, 'default' => 'current_timestamp'],
+                '`created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP'
+            ],
+            [
+                'open_date',
+                ['type' => 'datetime', 'null' => false, 'default' => '2016-12-07 23:04:00'],
+                '`open_date` DATETIME NOT NULL DEFAULT \'2016-12-07 23:04:00\''
+            ],
             // Date & Time
             [
                 'start_date',
@@ -675,9 +689,9 @@ SQL;
                 '`created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP'
             ],
             [
-                'created',
-                ['type' => 'datetime', 'null' => false, 'default' => 'current_timestamp'],
-                '`created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP'
+                'open_date',
+                ['type' => 'timestamp', 'null' => false, 'default' => '2016-12-07 23:04:00'],
+                '`open_date` TIMESTAMP NOT NULL DEFAULT \'2016-12-07 23:04:00\''
             ],
         ];
     }

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

@@ -760,6 +760,11 @@ SQL;
                 ['type' => 'datetime'],
                 '"created" TIMESTAMP'
             ],
+            [
+                'open_date',
+                ['type' => 'datetime', 'null' => false, 'default' => '2016-12-07 23:04:00'],
+                '"open_date" TIMESTAMP NOT NULL DEFAULT \'2016-12-07 23:04:00\''
+            ],
             // Date & Time
             [
                 'start_date',

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

@@ -566,6 +566,11 @@ SQL;
                 ['type' => 'datetime'],
                 '"created" DATETIME'
             ],
+            [
+                'open_date',
+                ['type' => 'datetime', 'null' => false, 'default' => '2016-12-07 23:04:00'],
+                '"open_date" DATETIME NOT NULL DEFAULT "2016-12-07 23:04:00"'
+            ],
             // Date & Time
             [
                 'start_date',

+ 10 - 0
tests/TestCase/Database/Schema/SqlserverSchemaTest.php

@@ -628,6 +628,16 @@ SQL;
                 ['type' => 'datetime'],
                 '[created] DATETIME'
             ],
+            [
+                'open_date',
+                ['type' => 'datetime', 'null' => false, 'default' => '2016-12-07 23:04:00'],
+                '[open_date] DATETIME NOT NULL DEFAULT \'2016-12-07 23:04:00\''
+            ],
+            [
+                'open_date',
+                ['type' => 'datetime', 'null' => false, 'default' => 'current_timestamp'],
+                '[open_date] DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP'
+            ],
             // Date & Time
             [
                 'start_date',