diff --git a/models/Grammars/BaseGrammar.cfc b/models/Grammars/BaseGrammar.cfc index b9d0af05..ac5f8070 100644 --- a/models/Grammars/BaseGrammar.cfc +++ b/models/Grammars/BaseGrammar.cfc @@ -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 ======*/ /*==================================== diff --git a/models/Grammars/MySQLGrammar.cfc b/models/Grammars/MySQLGrammar.cfc index fea624a3..bbf618d1 100644 --- a/models/Grammars/MySQLGrammar.cfc +++ b/models/Grammars/MySQLGrammar.cfc @@ -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 ) { diff --git a/models/Schema/Blueprint.cfc b/models/Schema/Blueprint.cfc index 2ddbf79a..43a2b6f0 100644 --- a/models/Schema/Blueprint.cfc +++ b/models/Schema/Blueprint.cfc @@ -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() ); diff --git a/tests/resources/AbstractSchemaBuilderSpec.cfc b/tests/resources/AbstractSchemaBuilderSpec.cfc index f4c78030..556aa5e5 100644 --- a/tests/resources/AbstractSchemaBuilderSpec.cfc +++ b/tests/resources/AbstractSchemaBuilderSpec.cfc @@ -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() ); + } ); } ); } ); diff --git a/tests/specs/Schema/MSSQLSchemaBuilderSpec.cfc b/tests/specs/Schema/MSSQLSchemaBuilderSpec.cfc index dd132a35..caea4843 100644 --- a/tests/specs/Schema/MSSQLSchemaBuilderSpec.cfc +++ b/tests/specs/Schema/MSSQLSchemaBuilderSpec.cfc @@ -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]))" ]; } diff --git a/tests/specs/Schema/MySQLSchemaBuilderSpec.cfc b/tests/specs/Schema/MySQLSchemaBuilderSpec.cfc index c4d093b2..6567c450 100644 --- a/tests/specs/Schema/MySQLSchemaBuilderSpec.cfc +++ b/tests/specs/Schema/MySQLSchemaBuilderSpec.cfc @@ -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`))" ]; } diff --git a/tests/specs/Schema/OracleSchemaBuilderSpec.cfc b/tests/specs/Schema/OracleSchemaBuilderSpec.cfc index 198a0304..7b9d7bec 100644 --- a/tests/specs/Schema/OracleSchemaBuilderSpec.cfc +++ b/tests/specs/Schema/OracleSchemaBuilderSpec.cfc @@ -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)", diff --git a/tests/specs/Schema/PostgresSchemaBuilderSpec.cfc b/tests/specs/Schema/PostgresSchemaBuilderSpec.cfc index 06348080..30775e2e 100644 --- a/tests/specs/Schema/PostgresSchemaBuilderSpec.cfc +++ b/tests/specs/Schema/PostgresSchemaBuilderSpec.cfc @@ -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)",