Skip to content

Commit

Permalink
Fixing Sqlite's handling of datetime/timestamp columns with current_t…
Browse files Browse the repository at this point in the history
…imestamp default set
  • Loading branch information
MarkusBauer committed May 18, 2016
1 parent 3265831 commit 2227e01
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/Cake/Model/Datasource/Database/Sqlite.php
Expand Up @@ -185,6 +185,9 @@ public function describe($model) {
'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;
Expand Down
34 changes: 34 additions & 0 deletions lib/Cake/Test/Case/Model/Datasource/Database/SqliteTest.php
Expand Up @@ -422,6 +422,40 @@ public function testDescribeWithUuidPrimaryKey() {
$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 = <<<SQL
CREATE TABLE $name (
id INT NOT NULL,
phone VARCHAR(10),
limit_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);
SQL;
$this->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.
*
Expand Down

0 comments on commit 2227e01

Please sign in to comment.