Skip to content

Commit

Permalink
Remove constraints by name or index object
Browse files Browse the repository at this point in the history
  • Loading branch information
elpete committed Sep 23, 2017
1 parent 61306e0 commit dffee36
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
13 changes: 10 additions & 3 deletions models/Grammars/BaseGrammar.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -867,9 +867,16 @@ component displayname="Grammar" accessors="true" {
===================================*/

function compileAddConstraint( blueprint, commandParameters ) {
if ( blueprint.getCreating() ) {
return "";
}
var constraints = blueprint.getIndexes().map( function( index ) {
return invoke( this, "index#index.getType()#", { index = index } );
} ).filter( function( item ) {
return item != "";
} ).toList( ", " );
return "ALTER TABLE #wrapTable( blueprint.getTable() )# ADD #constraints#";
}

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

/*===== End of Constraints ======*/
Expand Down
17 changes: 17 additions & 0 deletions models/Schema/Blueprint.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@ component accessors="true" {
return newColumn;
}

function addConstraint( constraint ) {
addCommand( "addConstraint" );
return this;
}

function removeConstraint( name ) {
if ( ! isSimpleValue( name ) ) {
name.getIndexes().each( function( index ) {
removeConstraint( index.getName() );
} );
}
else {
addCommand( "removeConstraint", { name = name } );
}
return this;
}

function addIndex() {
var newIndex = new TableIndex( this );
var indexMetadata = getMetadata( newIndex );
Expand Down
4 changes: 2 additions & 2 deletions tests/specs/Schema/SchemaBuilder+GrammarSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ component extends="testbox.system.BaseSpec" {
} );
} );

xdescribe( "in alter", function() {
describe( "in alter", function() {
it( "add constraint", function() {
var schema = getBuilder();
var blueprint = schema.alter( "users", function( table ) {
Expand All @@ -657,7 +657,7 @@ component extends="testbox.system.BaseSpec" {
var statements = blueprint.toSql();
expect( statements ).toBeArray();
expect( statements ).toHaveLength( 1 );
expect( statements[ 1 ] ).toBeWithCase( "ALTER TABLE ""users"" ADD CONSTRAINT ""unique_username"" (""username"")" );
expect( statements[ 1 ] ).toBeWithCase( "ALTER TABLE ""users"" ADD CONSTRAINT ""unique_username"" UNIQUE (""username"")" );
} );

it( "remove constraint", function() {
Expand Down

0 comments on commit dffee36

Please sign in to comment.