Merge pull request #7408 from cakephp/2.7-mysql-datetime

Allow CURRENT_TIMESTAMP for datetime columns - MySQL5.6+.
This commit is contained in:
Mark Story 2015-09-17 14:56:47 -04:00
commit e176e2f4e1
2 changed files with 39 additions and 1 deletions

View file

@ -352,7 +352,7 @@ class Mysql extends DboSource {
if (in_array($fields[$column->Field]['type'], $this->fieldParameters['unsigned']['types'], true)) { if (in_array($fields[$column->Field]['type'], $this->fieldParameters['unsigned']['types'], true)) {
$fields[$column->Field]['unsigned'] = $this->_unsigned($column->Type); $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; $fields[$column->Field]['default'] = null;
} }
if (!empty($column->Key) && isset($this->index[$column->Key])) { if (!empty($column->Key) && isset($this->index[$column->Key])) {

View file

@ -926,6 +926,44 @@ SQL;
$this->assertContains('`limit_date` timestamp NOT NULL,', $result); $this->assertContains('`limit_date` timestamp NOT NULL,', $result);
} }
/**
* Test that describe() ignores `default current_timestamp` in datetime columns.
* This is for MySQL >= 5.6.
*
* @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 = <<<SQL
CREATE TABLE $name (
id INT(11) NOT NULL AUTO_INCREMENT,
phone VARCHAR(10),
limit_date DATETIME 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` datetime NOT NULL,', $result);
}
/** /**
* test that a describe() gets additional fieldParameters * test that a describe() gets additional fieldParameters
* *