Skip to content

Commit

Permalink
Allow CURRENT_TIMESTAMP for datetime columns - MySQL5.6+
Browse files Browse the repository at this point in the history
  • Loading branch information
vagrant committed Sep 18, 2015
1 parent 2611ea8 commit 90e8fcd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Database/Schema/MysqlSchema.php
Expand Up @@ -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']);
Expand All @@ -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 ';
}
Expand Down
5 changes: 5 additions & 0 deletions tests/TestCase/Database/Schema/MysqlSchemaTest.php
Expand Up @@ -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'
],
];
}

Expand Down

0 comments on commit 90e8fcd

Please sign in to comment.