Skip to content

Commit

Permalink
fix(SchemaBuilder): Fix dropping foreign keys in MySQL
Browse files Browse the repository at this point in the history
  • Loading branch information
elpete committed Apr 27, 2018
1 parent defede1 commit 8895447
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions models/Grammars/BaseGrammar.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,10 @@ component displayname="Grammar" accessors="true" {
return "ALTER TABLE #wrapTable( blueprint.getTable() )# DROP INDEX #wrapValue( commandParameters.name )#";
}

function compileDropForeignKey( blueprint, commandParameters ) {
return "ALTER TABLE #wrapTable( blueprint.getTable() )# DROP CONSTRAINT #wrapValue( commandParameters.name )#";
}

/*===== End of Constraints ======*/

/*====================================
Expand Down
4 changes: 4 additions & 0 deletions models/Grammars/MySQLGrammar.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ component extends="qb.models.Grammars.BaseGrammar" {
return "SELECT 1 FROM `information_schema`.`columns` WHERE `table_name` = ? AND `column_name` = ?";
}

function compileDropForeignKey( blueprint, commandParameters ) {
return "ALTER TABLE #wrapTable( blueprint.getTable() )# DROP FOREIGN KEY #wrapValue( commandParameters.name )#";
}

function compileDropAllObjects( options ) {
var tables = getAllTableNames( options );
var tableList = arrayToList( arrayMap( tables, function( table ) {
Expand Down
10 changes: 10 additions & 0 deletions models/Schema/Blueprint.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,16 @@ component accessors="true" {
return this;
}

function dropForeignKey( name ) {
if ( ! isSimpleValue( name ) ) {
dropForeignKey( name.getName() );
}
else {
addCommand( "dropForeignKey", { name = name } );
}
return this;
}

function renameConstraint( oldName, newName ) {
if ( ! isSimpleValue( arguments.oldName ) ) {
arguments.oldName = dropConstraint( arguments.oldName.getName() );
Expand Down
8 changes: 8 additions & 0 deletions tests/resources/AbstractSchemaBuilderSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,14 @@ component extends="testbox.system.BaseSpec" {
}, {}, false );
}, dropConstraintFromIndex() );
} );

it( "drop foreign key", function() {
testCase( function( schema ) {
return schema.alter( "users", function( table ) {
table.dropForeignKey( "fk_posts_author_id" );
}, {}, false );
}, dropForeignKey() );
} );
} );
} );

Expand Down
4 changes: 4 additions & 0 deletions tests/specs/Schema/MSSQLSchemaBuilderSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ component extends="tests.resources.AbstractSchemaBuilderSpec" {
return [ "ALTER TABLE [users] DROP CONSTRAINT [unq_users_username]" ];
}

function dropForeignKey() {
return [ "ALTER TABLE [users] DROP CONSTRAINT [fk_posts_author_id]" ];
}

function basicIndex() {
return [ "CREATE TABLE [users] ([published_date] DATETIME2 NOT NULL, INDEX [idx_users_published_date] ([published_date]))" ];
}
Expand Down
4 changes: 4 additions & 0 deletions tests/specs/Schema/MySQLSchemaBuilderSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ component extends="tests.resources.AbstractSchemaBuilderSpec" {
return [ "ALTER TABLE `users` DROP INDEX `unq_users_username`" ];
}

function dropForeignKey() {
return [ "ALTER TABLE `users` DROP FOREIGN KEY `fk_posts_author_id`" ];
}

function basicIndex() {
return [ "CREATE TABLE `users` (`published_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, INDEX `idx_users_published_date` (`published_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 @@ -312,6 +312,10 @@ component extends="tests.resources.AbstractSchemaBuilderSpec" {
return [ "ALTER TABLE ""USERS"" DROP CONSTRAINT ""UNQ_USERS_USERNAME""" ];
}

function dropForeignKey() {
return [ "ALTER TABLE ""USERS"" DROP CONSTRAINT ""FK_POSTS_AUTHOR_ID""" ];
}

function basicIndex() {
return [
"CREATE TABLE ""USERS"" (""PUBLISHED_DATE"" DATE NOT NULL)",
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 @@ -291,6 +291,10 @@ component extends="tests.resources.AbstractSchemaBuilderSpec" {
return [ "ALTER TABLE ""users"" DROP CONSTRAINT ""unq_users_username""" ];
}

function dropForeignKey() {
return [ "ALTER TABLE ""users"" DROP CONSTRAINT ""fk_posts_author_id""" ];
}

function basicIndex() {
return [
"CREATE TABLE ""users"" (""published_date"" TIMESTAMP NOT NULL)",
Expand Down

0 comments on commit 8895447

Please sign in to comment.