Skip to content

Commit

Permalink
Add default value support for SQLServer
Browse files Browse the repository at this point in the history
Support the ANSI SQL 'CURRENT_TIMESTAMP' value, as well as custom
datetime values. We don't currently support any SQLServer specific
functions.

Refs #9850
  • Loading branch information
markstory committed Dec 8, 2016
1 parent 5cd5edc commit 54ab52c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Database/Schema/SqlserverSchema.php
Expand Up @@ -407,7 +407,14 @@ public function columnSql(Table $table, $name)
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;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/TestCase/Database/Schema/SqlserverSchemaTest.php
Expand Up @@ -628,6 +628,16 @@ public static function columnSqlProvider()
['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',
Expand Down

0 comments on commit 54ab52c

Please sign in to comment.