Skip to content
Permalink
Browse files

Fix missing default values for datetime/timestamp

The MySQL schema adapter should allow default values other than
'current_timestamp'.

Refs #9850
  • Loading branch information...
markstory committed Dec 8, 2016
1 parent 83641b9 commit f5a4c3b84958b9057ac6d6acf5b2a0a385e0be51
Showing with 21 additions and 7 deletions.
  1. +4 −4 src/Database/Schema/MysqlSchema.php
  2. +17 −3 tests/TestCase/Database/Schema/MysqlSchemaTest.php
@@ -393,17 +393,17 @@ public function columnSql(Table $table, $name)
$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'
) {
$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']);
}
@@ -59,6 +59,10 @@ public static function convertColumnProvider()
'TIME',
['type' => 'time', 'length' => null]
],
[
'TIMESTAMP',
['type' => 'timestamp', 'length' => null]
],
[
'TINYINT(1)',
['type' => 'boolean', 'length' => null]
@@ -652,6 +656,16 @@ public static function columnSqlProvider()
['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 @@ public static function columnSqlProvider()
'`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\''
],
];
}

0 comments on commit f5a4c3b

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