Browse Source

Merge pull request #10042 from multidimension-al/master

Change Default Behavior in Postgres and SQLServer for DATETIME
Mark Story 9 years ago
parent
commit
2b943c3954

+ 8 - 5
src/Database/Schema/PostgresSchema.php

@@ -410,16 +410,19 @@ class PostgresSchema extends BaseSchema
         if (isset($data['null']) && $data['null'] === false) {
             $out .= ' NOT NULL';
         }
-        if (isset($data['null']) && $data['null'] === true && $data['type'] === 'timestamp') {
-            $out .= ' DEFAULT NULL';
-            unset($data['default']);
-        }
-        if (isset($data['default']) && $data['type'] !== 'timestamp') {
+
+        if (isset($data['default']) &&
+            in_array($data['type'], ['timestamp', 'datetime']) &&
+            strtolower($data['default']) === 'current_timestamp') {
+            $out .= ' DEFAULT CURRENT_TIMESTAMP';
+        } elseif (isset($data['default'])) {
             $defaultValue = $data['default'];
             if ($data['type'] === 'boolean') {
                 $defaultValue = (bool)$defaultValue;
             }
             $out .= ' DEFAULT ' . $this->_driver->schemaValue($defaultValue);
+        } elseif (isset($data['null']) && $data['null'] !== false) {
+            $out .= ' DEFAULT NULL';
         }
 
         return $out;

+ 4 - 10
src/Database/Schema/SqlserverSchema.php

@@ -402,21 +402,15 @@ class SqlserverSchema extends BaseSchema
             $out .= ' NOT NULL';
         }
 
-        if (isset($data['null']) && $data['null'] === true) {
-            $out .= ' DEFAULT NULL';
-            unset($data['default']);
-        }
-
         if (isset($data['default']) &&
             in_array($data['type'], ['timestamp', 'datetime']) &&
-            strtolower($data['default']) === 'current_timestamp'
-        ) {
+            strtolower($data['default']) === 'current_timestamp') {
             $out .= ' DEFAULT CURRENT_TIMESTAMP';
-            unset($data['default']);
-        }
-        if (isset($data['default'])) {
+        } elseif (isset($data['default'])) {
             $default = is_bool($data['default']) ? (int)$data['default'] : $this->_driver->schemaValue($data['default']);
             $out .= ' DEFAULT ' . $default;
+        } elseif (isset($data['null']) && $data['null'] !== false) {
+            $out .= ' DEFAULT NULL';
         }
 
         return $out;

+ 12 - 2
tests/TestCase/Database/Schema/PostgresSchemaTest.php

@@ -782,7 +782,7 @@ SQL;
                 ['type' => 'boolean', 'default' => 1, 'null' => false],
                 '"checked" BOOLEAN NOT NULL DEFAULT TRUE'
             ],
-            // datetimes
+            // Datetime
             [
                 'created',
                 ['type' => 'datetime'],
@@ -793,6 +793,16 @@ SQL;
                 ['type' => 'datetime', 'null' => false, 'default' => '2016-12-07 23:04:00'],
                 '"open_date" TIMESTAMP NOT NULL DEFAULT \'2016-12-07 23:04:00\''
             ],
+            [
+                'null_date',
+                ['type' => 'datetime', 'null' => true],
+                '"null_date" TIMESTAMP DEFAULT NULL'
+            ],
+            [
+                'current_timestamp',
+                ['type' => 'datetime', 'null' => false, 'default' => 'CURRENT_TIMESTAMP'],
+                '"current_timestamp" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP'
+            ],
             // Date & Time
             [
                 'start_date',
@@ -804,7 +814,7 @@ SQL;
                 ['type' => 'time'],
                 '"start_time" TIME'
             ],
-            // timestamps
+            // Timestamp
             [
                 'created',
                 ['type' => 'timestamp', 'null' => true],

+ 13 - 3
tests/TestCase/Database/Schema/SqlserverSchemaTest.php

@@ -499,7 +499,7 @@ SQL;
             [
                 'title',
                 ['type' => 'string', 'length' => 25, 'null' => true, 'default' => 'ignored'],
-                '[title] NVARCHAR(25) DEFAULT NULL'
+                "[title] NVARCHAR(25) DEFAULT 'ignored'"
             ],
             [
                 'id',
@@ -622,7 +622,7 @@ SQL;
                 ['type' => 'boolean', 'default' => true, 'null' => false],
                 '[checked] BIT NOT NULL DEFAULT 1'
             ],
-            // datetimes
+            // Datetime
             [
                 'created',
                 ['type' => 'datetime'],
@@ -638,6 +638,16 @@ SQL;
                 ['type' => 'datetime', 'null' => false, 'default' => 'current_timestamp'],
                 '[open_date] DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP'
             ],
+            [
+                'null_date',
+                ['type' => 'datetime', 'null' => true, 'default' => 'current_timestamp'],
+                '[null_date] DATETIME DEFAULT CURRENT_TIMESTAMP'
+            ],
+            [
+                'null_date',
+                ['type' => 'datetime', 'null' => true],
+                '[null_date] DATETIME DEFAULT NULL'
+            ],
             // Date & Time
             [
                 'start_date',
@@ -649,7 +659,7 @@ SQL;
                 ['type' => 'time'],
                 '[start_time] TIME'
             ],
-            // timestamps
+            // Timestamp
             [
                 'created',
                 ['type' => 'timestamp', 'null' => true],