From d8a55ad065009463b371517aa861e2d22401451b Mon Sep 17 00:00:00 2001 From: mark Date: Wed, 16 Sep 2015 14:16:58 +0200 Subject: [PATCH 1/3] Allow CURRENT_TIMESTAMP for datetime columns - MySQL5.6+. --- lib/Cake/Model/Datasource/Database/Mysql.php | 2 +- .../Model/Datasource/Database/MysqlTest.php | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Model/Datasource/Database/Mysql.php b/lib/Cake/Model/Datasource/Database/Mysql.php index 6a6828e2f..24358c107 100644 --- a/lib/Cake/Model/Datasource/Database/Mysql.php +++ b/lib/Cake/Model/Datasource/Database/Mysql.php @@ -352,7 +352,7 @@ 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') { + if (in_array($fields[$column->Field]['type'], array('timestamp', 'datetime')) && strtoupper($column->Default) === 'CURRENT_TIMESTAMP') { $fields[$column->Field]['default'] = null; } if (!empty($column->Key) && isset($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 6c52c06d3..7cf7d4565 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php @@ -927,6 +927,41 @@ SQL; } /** + * Test that describe() ignores `default current_timestamp` in datetime columns. + * This is for MySQL >= 5.6. + * + * @return void + */ + public function testDescribeHandleCurrentTimestampDatetime() { + $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->Dbo->execute('DROP TABLE ' . $name); + + $this->assertNull($result['limit_date']['default']); + + $schema = new CakeSchema(array( + 'connection' => 'test', + 'testdescribes' => $result + )); + $result = $this->Dbo->createSchema($schema); + $this->assertContains('`limit_date` datetime NOT NULL,', $result); + } + + /** * test that a describe() gets additional fieldParameters * * @return void From e7d6319d598b6bef305d0ed2a4a706903e95e8fc Mon Sep 17 00:00:00 2001 From: mark Date: Thu, 17 Sep 2015 10:43:43 +0200 Subject: [PATCH 2/3] Skip test for versions of < MySQL5.6. --- lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php index 7cf7d4565..39979d640 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php @@ -933,6 +933,9 @@ SQL; * @return void */ public function testDescribeHandleCurrentTimestampDatetime() { + $mysqlVersion = $this->Dbo->query('SELECT VERSION() as version', array('log' => false)); + $this->skipIf(version_compare($mysqlVersion[0][0]['version'], '5.6.0', '<')); + $name = $this->Dbo->fullTableName('timestamp_default_values'); $sql = << Date: Thu, 17 Sep 2015 11:33:59 +0200 Subject: [PATCH 3/3] Fix CS --- lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php index 39979d640..20f4d70c8 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php @@ -964,7 +964,7 @@ SQL; $this->assertContains('`limit_date` datetime NOT NULL,', $result); } - /** +/** * test that a describe() gets additional fieldParameters * * @return void