Skip to content

Commit

Permalink
fix(Column): Explicitly name default constraint for MSSQL
Browse files Browse the repository at this point in the history
* update MSSQL for DEFAULT constraint

* Fix failing test for `last`

* Update MSSQL tests and wrap default constriant name

* Allow for a table level `default` for removing default constraints with generated names

This method won't actually create a constraint, but it is used for removing constraints
with auto-generated names.

For instance, if you used the default name in
```
schema.create( "my_table", function( table ) {
    table.boolean( "trueOrFalse" ).default( 0 );
} );

```
it may not be obvious that the constraint name is `df_my_table_trueOrFalse`.

This method let's you stay oblivious to the auto-generated name when dropping the constraint.

```
schema.alter( "my_table", function( table ) {
    table.dropConstraint( table.default( "trueOrFalse" ) );
    table.dropColumn( "trueOrFalse" );
} );
```
  • Loading branch information
elpete committed Jul 20, 2018
1 parent 5b0fe28 commit 288bd66
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion models/Grammars/BaseGrammar.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ component displayname="Grammar" accessors="true" {
generateNullConstraint( column ),
generateUniqueConstraint( column, blueprint ),
generateAutoIncrement( column, blueprint ),
generateDefault( column ),
generateDefault( column, blueprint ),
generateComment( column, blueprint )
], function( item ) {
return item != "";
Expand Down
4 changes: 4 additions & 0 deletions models/Grammars/MSSQLGrammar.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ component extends="qb.models.Grammars.BaseGrammar" {
return column.getAutoIncrement() ? "IDENTITY" : "";
}

function generateDefault( column, blueprint ) {
return column.getDefault() != "" ? "CONSTRAINT #wrapValue( "df_#blueprint.getTable()#_#column.getName()#" )# DEFAULT #column.getDefault()#" : "";
}

function generateComment( column ) {
return "";
}
Expand Down
21 changes: 20 additions & 1 deletion models/Schema/Blueprint.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,20 @@ component accessors="true" {
return appendIndex( type = "unique", columns = columns, name = name );
}

/**
* Create a default constraint from a column.
*
* @columns The column that makes up the default constraint.
* @name The name of the default constraint.
* Default: A generated name consisting of the table name and column name.
*
* @returns The created TableIndex instance.
*/
function default( column, name ) {
arguments.name = isNull( name ) ? "df_#getTable()#_#column#" : arguments.name;
return createIndex( type = "default", columns = column, name = name );
}


/*======================================
= Alter Commands =
Expand Down Expand Up @@ -386,6 +400,12 @@ component accessors="true" {
}

function appendIndex() {
var newIndex = createIndex( argumentCollection = arguments );
variables.indexes.append( newIndex );
return newIndex;
}

function createIndex() {
var newIndex = new TableIndex( this );
var indexMetadata = getMetadata( newIndex );
var functionNames = indexMetadata.functions.map( function( func ) {
Expand All @@ -396,7 +416,6 @@ component accessors="true" {
invoke( newIndex, "set#arg#", { 1 = arguments[ arg ] } );
}
}
variables.indexes.append( newIndex );
return newIndex;
}

Expand Down
2 changes: 0 additions & 2 deletions tests/resources/AbstractSchemaBuilderSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -850,8 +850,6 @@ component extends="testbox.system.BaseSpec" {
if ( ! isArray( statements ) ) {
statements = [ statements ];
}
debug( statements );
debug( expected );
expect( statements ).toBeArray();
expect( statements ).toHaveLength( arrayLen( expected ) );
for ( var i = 1; i <= expected.len(); i++ ) {
Expand Down
2 changes: 1 addition & 1 deletion tests/specs/Query/Abstract/QueryExecutionSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ component extends="testbox.system.BaseSpec" {
var builder = getBuilder();
var expectedQuery = queryNew( "id,name", "integer,varchar", [ { id = 1, name = "foo" }, { id = 2, name = "test" } ] );
builder.$( "runQuery" ).$args(
sql = "SELECT * FROM ""users"" WHERE ""name"" = ? LIMIT 1",
sql = "SELECT * FROM ""users""",
options = {}
).$results( expectedQuery );

Expand Down
4 changes: 2 additions & 2 deletions tests/specs/Schema/MSSQLSchemaBuilderSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ component extends="tests.resources.AbstractSchemaBuilderSpec" {
}

function complicatedTable() {
return [ "CREATE TABLE [users] ([id] INTEGER NOT NULL IDENTITY, [username] NVARCHAR(255) NOT NULL, [first_name] NVARCHAR(255) NOT NULL, [last_name] NVARCHAR(255) NOT NULL, [password] NVARCHAR(100) NOT NULL, [country_id] INTEGER NOT NULL, [created_date] DATETIME2 NOT NULL DEFAULT CURRENT_TIMESTAMP, [modified_date] DATETIME2 NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT [pk_users_id] PRIMARY KEY ([id]), CONSTRAINT [fk_users_country_id] FOREIGN KEY ([country_id]) REFERENCES [countries] ([id]) ON UPDATE NO ACTION ON DELETE CASCADE)" ];
return [ "CREATE TABLE [users] ([id] INTEGER NOT NULL IDENTITY, [username] NVARCHAR(255) NOT NULL, [first_name] NVARCHAR(255) NOT NULL, [last_name] NVARCHAR(255) NOT NULL, [password] NVARCHAR(100) NOT NULL, [country_id] INTEGER NOT NULL, [created_date] DATETIME2 NOT NULL CONSTRAINT [df_users_created_date] DEFAULT CURRENT_TIMESTAMP, [modified_date] DATETIME2 NOT NULL CONSTRAINT [df_users_modified_date] DEFAULT CURRENT_TIMESTAMP, CONSTRAINT [pk_users_id] PRIMARY KEY ([id]), CONSTRAINT [fk_users_country_id] FOREIGN KEY ([country_id]) REFERENCES [countries] ([id]) ON UPDATE NO ACTION ON DELETE CASCADE)" ];
}

function bigIncrements() {
Expand Down Expand Up @@ -231,7 +231,7 @@ component extends="tests.resources.AbstractSchemaBuilderSpec" {
}

function default() {
return [ "CREATE TABLE [users] ([active] NCHAR(1) NOT NULL DEFAULT 'Y')" ];
return [ "CREATE TABLE [users] ([active] NCHAR(1) NOT NULL CONSTRAINT [df_users_active] DEFAULT 'Y')" ];
}

function nullable() {
Expand Down

0 comments on commit 288bd66

Please sign in to comment.