Skip to content
Permalink
Browse files

Allow CURRENT_TIMESTAMP for datetime columns - MySQL5.6+

  • Loading branch information...
vagrant
vagrant committed Sep 18, 2015
1 parent 2611ea8 commit 90e8fcda68590e99ac3c889ea0d1ee9fd2dd5a81
Showing with 11 additions and 2 deletions.
  1. +6 −2 src/Database/Schema/MysqlSchema.php
  2. +5 −0 tests/TestCase/Database/Schema/MysqlSchemaTest.php
@@ -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']);
@@ -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 ';
}
@@ -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'
],
];
}

0 comments on commit 90e8fcd

Please sign in to comment.
You can’t perform that action at this time.