Skip to content

Commit

Permalink
fix(MySQLGrammar): Allow nullable timestamps in MySQL
Browse files Browse the repository at this point in the history
MySQL needs some extra attributes to allow NULL in TIMESTAMP fields.
  • Loading branch information
elpete committed Jul 17, 2020
1 parent 54b80ed commit ceb96a1
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 9 deletions.
9 changes: 6 additions & 3 deletions models/Grammars/MySQLGrammar.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,13 @@ component extends="qb.models.Grammars.BaseGrammar" singleton {
function generateDefault( column ) {
if (
column.getDefault() == "" &&
column.getType().findNoCase( "TIMESTAMP" ) > 0 &&
!column.getNullable()
column.getType().findNoCase( "TIMESTAMP" ) > 0
) {
column.withCurrent();
if ( column.getNullable() ) {
return "NULL DEFAULT NULL";
} else {
column.withCurrent();
}
}
return super.generateDefault( column );
}
Expand Down
13 changes: 13 additions & 0 deletions tests/resources/AbstractSchemaBuilderSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,19 @@ component extends="testbox.system.BaseSpec" {
}, timestamp() );
} );

it( "timestampWithNullable", function() {
testCase( function( schema ) {
return schema.create(
"posts",
function( table ) {
table.timestamp( "posted_date" ).nullable();
},
{},
false
);
}, timestampWithNullable() );
} );

it( "timestampTz", function() {
testCase( function( schema ) {
return schema.create(
Expand Down
18 changes: 12 additions & 6 deletions tests/specs/Schema/MySQLSchemaBuilderSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ component extends="tests.resources.AbstractSchemaBuilderSpec" {
}

function nullableTimestamps() {
return [ "CREATE TABLE `posts` (`createdDate` TIMESTAMP, `modifiedDate` TIMESTAMP)" ];
return [
"CREATE TABLE `posts` (`createdDate` TIMESTAMP NULL DEFAULT NULL, `modifiedDate` TIMESTAMP NULL DEFAULT NULL)"
];
}

function point() {
Expand Down Expand Up @@ -201,11 +203,11 @@ component extends="tests.resources.AbstractSchemaBuilderSpec" {
}

function softDeletes() {
return [ "CREATE TABLE `posts` (`deletedDate` TIMESTAMP)" ];
return [ "CREATE TABLE `posts` (`deletedDate` TIMESTAMP NULL DEFAULT NULL)" ];
}

function softDeletesTz() {
return [ "CREATE TABLE `posts` (`deletedDate` TIMESTAMP)" ];
return [ "CREATE TABLE `posts` (`deletedDate` TIMESTAMP NULL DEFAULT NULL)" ];
}

function string() {
Expand Down Expand Up @@ -240,6 +242,10 @@ component extends="tests.resources.AbstractSchemaBuilderSpec" {
return [ "CREATE TABLE `posts` (`posted_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)" ];
}

function timestampWithNullable() {
return [ "CREATE TABLE `posts` (`posted_date` TIMESTAMP NULL DEFAULT NULL)" ];
}

function timestamps() {
return [
"CREATE TABLE `posts` (`createdDate` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, `modifiedDate` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)"
Expand Down Expand Up @@ -472,7 +478,7 @@ component extends="tests.resources.AbstractSchemaBuilderSpec" {
function renameMultipleColumns() {
return [
"ALTER TABLE `users` CHANGE `name` `username` NVARCHAR(255) NOT NULL",
"ALTER TABLE `users` CHANGE `purchase_date` `purchased_at` TIMESTAMP"
"ALTER TABLE `users` CHANGE `purchase_date` `purchased_at` TIMESTAMP NULL DEFAULT NULL"
];
}

Expand All @@ -483,7 +489,7 @@ component extends="tests.resources.AbstractSchemaBuilderSpec" {
function modifyMultipleColumns() {
return [
"ALTER TABLE `users` CHANGE `name` `name` TEXT NOT NULL",
"ALTER TABLE `users` CHANGE `purchase_date` `purchased_date` TIMESTAMP"
"ALTER TABLE `users` CHANGE `purchase_date` `purchased_date` TIMESTAMP NULL DEFAULT NULL"
];
}

Expand All @@ -503,7 +509,7 @@ component extends="tests.resources.AbstractSchemaBuilderSpec" {
"ALTER TABLE `users` DROP COLUMN `is_active`",
"ALTER TABLE `users` ADD `tshirt_size` ENUM('S', 'M', 'L', 'XL', 'XXL') NOT NULL",
"ALTER TABLE `users` CHANGE `name` `username` NVARCHAR(255) NOT NULL",
"ALTER TABLE `users` CHANGE `purchase_date` `purchase_date` TIMESTAMP",
"ALTER TABLE `users` CHANGE `purchase_date` `purchase_date` TIMESTAMP NULL DEFAULT NULL",
"ALTER TABLE `users` ADD CONSTRAINT `unq_users_username` UNIQUE (`username`)",
"ALTER TABLE `users` ADD CONSTRAINT `unq_users_email` UNIQUE (`email`)",
"ALTER TABLE `users` DROP INDEX `idx_users_created_date`",
Expand Down
4 changes: 4 additions & 0 deletions tests/specs/Schema/OracleSchemaBuilderSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,10 @@ component extends="tests.resources.AbstractSchemaBuilderSpec" {
return [ "CREATE TABLE ""POSTS"" (""POSTED_DATE"" DATE DEFAULT CURRENT_TIMESTAMP NOT NULL)" ];
}

function timestampWithNullable() {
return [ "CREATE TABLE ""POSTS"" (""POSTED_DATE"" DATE)" ];
}

function nullable() {
return [ "CREATE TABLE ""USERS"" (""ID"" CHAR(36))" ];
}
Expand Down
4 changes: 4 additions & 0 deletions tests/specs/Schema/PostgresSchemaBuilderSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ component extends="tests.resources.AbstractSchemaBuilderSpec" {
return [ "CREATE TABLE ""posts"" (""posted_date"" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)" ];
}

function timestampWithNullable() {
return [ "CREATE TABLE ""posts"" (""posted_date"" TIMESTAMP)" ];
}

function nullable() {
return [ "CREATE TABLE ""users"" (""id"" CHAR(36))" ];
}
Expand Down
4 changes: 4 additions & 0 deletions tests/specs/Schema/SqlServerSchemaBuilderSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,10 @@ component extends="tests.resources.AbstractSchemaBuilderSpec" {
];
}

function timestampWithNullable() {
return [ "CREATE TABLE [posts] ([posted_date] DATETIME2)" ];
}

function defaultForNumber() {
return [ "CREATE TABLE [users] ([experience] INTEGER NOT NULL CONSTRAINT [df_users_experience] DEFAULT 100)" ];
}
Expand Down

0 comments on commit ceb96a1

Please sign in to comment.