Skip to content

Commit

Permalink
Fix nullable field with default value in various db platforms.
Browse files Browse the repository at this point in the history
null => true and a default value should create a nullable column with
a default value not `DEFAULT NULL`.

Refs #9686
  • Loading branch information
markstory committed Nov 3, 2016
1 parent 1a1263e commit c721dde
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/Database/Schema/MysqlSchema.php
Expand Up @@ -389,8 +389,8 @@ public function columnSql(Table $table, $name)
) {
$out .= ' AUTO_INCREMENT';
}
if (isset($data['null']) && $data['null'] === true) {
$out .= $data['type'] === 'timestamp' ? ' NULL' : ' DEFAULT NULL';
if (isset($data['null']) && $data['null'] === true && $data['type'] === 'timestamp') {
$out .= ' NULL';
unset($data['default']);
}
if (isset($data['default']) && !in_array($data['type'], ['timestamp', 'datetime'])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Schema/PostgresSchema.php
Expand Up @@ -402,7 +402,7 @@ public function columnSql(Table $table, $name)
if (isset($data['null']) && $data['null'] === false) {
$out .= ' NOT NULL';
}
if (isset($data['null']) && $data['null'] === true) {
if (isset($data['null']) && $data['null'] === true && $data['type'] === 'timestamp') {
$out .= ' DEFAULT NULL';
unset($data['default']);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Database/Schema/SqliteSchema.php
Expand Up @@ -332,9 +332,8 @@ public function columnSql(Table $table, $name)
$out .= ' PRIMARY KEY AUTOINCREMENT';
}

if (isset($data['null']) && $data['null'] === true) {
if (isset($data['null']) && $data['null'] === true && $data['type'] === 'timestamp') {
$out .= ' DEFAULT NULL';
unset($data['default']);
}
if (isset($data['default'])) {
$out .= ' DEFAULT ' . $this->_driver->schemaValue($data['default']);
Expand Down
18 changes: 14 additions & 4 deletions tests/TestCase/Database/Schema/MysqlSchemaTest.php
Expand Up @@ -470,6 +470,11 @@ public static function columnSqlProvider()
{
return [
// strings
[
'title',
['type' => 'string', 'length' => 25, 'null' => true, 'default' => null],
'`title` VARCHAR(25)',
],
[
'title',
['type' => 'string', 'length' => 25, 'null' => false],
Expand All @@ -478,18 +483,23 @@ public static function columnSqlProvider()
[
'title',
['type' => 'string', 'length' => 25, 'null' => true, 'default' => 'ignored'],
'`title` VARCHAR(25) DEFAULT NULL'
'`title` VARCHAR(25) DEFAULT \'ignored\'',
],
[
'id',
['type' => 'string', 'length' => 32, 'fixed' => true, 'null' => false],
'`id` CHAR(32) NOT NULL'
'title',
['type' => 'string', 'length' => 25, 'null' => true, 'default' => ''],
'`title` VARCHAR(25) DEFAULT \'\'',
],
[
'role',
['type' => 'string', 'length' => 10, 'null' => false, 'default' => 'admin'],
'`role` VARCHAR(10) NOT NULL DEFAULT \'admin\''
],
[
'id',
['type' => 'string', 'length' => 32, 'fixed' => true, 'null' => false],
'`id` CHAR(32) NOT NULL'
],
[
'title',
['type' => 'string'],
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Database/Schema/PostgresSchemaTest.php
Expand Up @@ -625,7 +625,7 @@ public static function columnSqlProvider()
[
'title',
['type' => 'string', 'length' => 25, 'null' => true, 'default' => 'ignored'],
'"title" VARCHAR(25) DEFAULT NULL'
'"title" VARCHAR(25) DEFAULT \'ignored\'',
],
[
'id',
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Database/Schema/SqliteSchemaTest.php
Expand Up @@ -453,7 +453,7 @@ public static function columnSqlProvider()
[
'title',
['type' => 'string', 'length' => 25, 'null' => true, 'default' => 'ignored'],
'"title" VARCHAR(25) DEFAULT NULL'
'"title" VARCHAR(25) DEFAULT "ignored"',
],
[
'id',
Expand Down

0 comments on commit c721dde

Please sign in to comment.