From 2227e019903a8183c25f11a5cf6c2abcdab45a74 Mon Sep 17 00:00:00 2001 From: Markus Bauer Date: Thu, 19 May 2016 00:21:43 +0200 Subject: [PATCH] Fixing Sqlite's handling of datetime/timestamp columns with current_timestamp default set --- lib/Cake/Model/Datasource/Database/Sqlite.php | 3 ++ .../Model/Datasource/Database/SqliteTest.php | 34 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/lib/Cake/Model/Datasource/Database/Sqlite.php b/lib/Cake/Model/Datasource/Database/Sqlite.php index 9bdc81c4c..7dd81f8a4 100644 --- a/lib/Cake/Model/Datasource/Database/Sqlite.php +++ b/lib/Cake/Model/Datasource/Database/Sqlite.php @@ -185,6 +185,9 @@ class Sqlite extends DboSource { 'default' => $default, 'length' => $this->length($column['type']) ); + if (in_array($fields[$column['name']]['type'], array('timestamp', 'datetime')) && strtoupper($fields[$column['name']]['default']) === 'CURRENT_TIMESTAMP') { + $fields[$column['name']]['default'] = null; + } if ($column['pk'] == 1) { $fields[$column['name']]['key'] = $this->index['PRI']; $fields[$column['name']]['null'] = false; diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/SqliteTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/SqliteTest.php index 14758c7d7..c60d936ad 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/SqliteTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/SqliteTest.php @@ -422,6 +422,40 @@ class SqliteTest extends CakeTestCase { $this->Dbo->query('DROP TABLE ' . $tableName); } +/** + * 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->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" timestamp NOT NULL', $result); + } + /** * Test virtualFields with functions. *