Skip to content

Commit

Permalink
Unify datetime column default values between MySQL and Postgres.
Browse files Browse the repository at this point in the history
Datetime columns should have 'default' => null, in both Postgres and
MySQL.

Fixes #3837
  • Loading branch information
markstory committed Jul 12, 2014
1 parent a098d96 commit 03c2a8b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/Cake/Model/Datasource/Database/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,9 @@ public function describe($model) {
$this->_sequenceMap[$table][$c->name] = $sequenceName;
}
}
if ($fields[$c->name]['type'] === 'timestamp' && $fields[$c->name]['default'] === '') {
$fields[$c->name]['default'] = null;
}
if ($fields[$c->name]['type'] === 'boolean' && !empty($fields[$c->name]['default'])) {
$fields[$c->name]['default'] = constant($fields[$c->name]['default']);
}
Expand Down
50 changes: 48 additions & 2 deletions lib/Cake/Test/Case/Model/Datasource/Database/PostgresTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ public function schema($field = false) {
'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8', 'key' => 'primary'),
'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
'email' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
'created' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => ''),
'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
'created' => array('type' => 'datetime', 'null' => true, 'default' => null, 'length' => ''),
'updated' => array('type' => 'datetime', 'null' => true, 'default' => null, 'length' => null)
);
}

Expand Down Expand Up @@ -551,6 +551,7 @@ public function testCakeSchema() {
'connection' => 'test',
'models' => array('DatatypeTest')
));

$schema->tables = array(
'datatype_tests' => $result['tables']['missing']['datatype_tests']
);
Expand Down Expand Up @@ -1098,4 +1099,49 @@ public function testLimit() {
$this->assertNotContains($scientificNotation, $result);
}

/**
* Test describe() behavior for timestamp columns.
*
* @return void
*/
public function testDescribeTimestamp() {
$this->loadFixtures('User');
$model = ClassRegistry::init('User');
$result = $this->Dbo->describe($model);
$expected = array(
'id' => array(
'type' => 'integer',
'null' => false,
'default' => null,
'length' => 11,
'key' => 'primary'
),
'user' => array(
'type' => 'string',
'null' => true,
'default' => null,
'length' => 255
),
'password' => array(
'type' => 'string',
'null' => true,
'default' => null,
'length' => 255
),
'created' => array(
'type' => 'datetime',
'null' => true,
'default' => null,
'length' => null
),
'updated' => array(
'type' => 'datetime',
'null' => true,
'default' => null,
'length' => null
)
);
$this->assertEquals($expected, $result);
}

}

0 comments on commit 03c2a8b

Please sign in to comment.