From 7936b5e7642935983833e3ea28ddafe4f3d84e08 Mon Sep 17 00:00:00 2001 From: mark_story Date: Mon, 25 Aug 2014 11:14:39 +0200 Subject: [PATCH] Fix CURRENT_TIMESTAMP being stored as a default value. When reflecting timestamp columns in MySQL current_timestamp comes back as the default value. This causes insertion errors later on as 'current_timestamp' is an invalid value for timestamp columns. Refs #4184 --- lib/Cake/Model/Datasource/Database/Mysql.php | 3 +++ .../Model/Datasource/Database/MysqlTest.php | 26 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/Cake/Model/Datasource/Database/Mysql.php b/lib/Cake/Model/Datasource/Database/Mysql.php index 3ed56016c..399b2f61b 100644 --- a/lib/Cake/Model/Datasource/Database/Mysql.php +++ b/lib/Cake/Model/Datasource/Database/Mysql.php @@ -351,6 +351,9 @@ class Mysql extends DboSource { if (in_array($fields[$column->Field]['type'], $this->fieldParameters['unsigned']['types'], true)) { $fields[$column->Field]['unsigned'] = $this->_unsigned($column->Type); } + if ($fields[$column->Field]['type'] === 'timestamp' && strtoupper($column->Default) === 'CURRENT_TIMESTAMP') { + $fields[$column->Field]['default'] = ''; + } if (!empty($column->Key) && isset($this->index[$column->Key])) { $fields[$column->Field]['key'] = $this->index[$column->Key]; } diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php index cca9d3375..673808681 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php @@ -884,6 +884,32 @@ class MysqlTest extends CakeTestCase { $this->assertTrue(isset($result['color'])); } +/** + * Test that describe() ignores `default current_timestamp` in timestamp columns. + * + * @return void + */ + public function testDescribeHandleCurrentTimestamp() { + $name = $this->Dbo->fullTableName('timestamp_default_values'); + $sql = <<Dbo->execute($sql); + $model = new Model(array( + 'table' => 'timestamp_default_values', + 'ds' => 'test', + 'alias' => 'TimestampDefaultValue' + )); + $result = $this->Dbo->describe($model); + $this->assertEquals('', $result['limit_date']['default']); + $this->Dbo->execute('DROP TABLE ' . $name); + } + /** * test that a describe() gets additional fieldParameters *