Skip to content

Commit

Permalink
Fix foreign key dynamic names
Browse files Browse the repository at this point in the history
  • Loading branch information
elpete committed Sep 23, 2017
1 parent 25ba4e7 commit 7b0f9f0
Show file tree
Hide file tree
Showing 4 changed files with 76 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 @@ -1003,7 +1003,7 @@ component displayname="Grammar" accessors="true" {
return wrapColumn( column );
} ).toList( ", " );
return arrayToList( [
"CONSTRAINT #wrapValue( "fk_#lcase( index.getForeignKey() )#" )#",
"CONSTRAINT #wrapValue( index.getName() )#",
"FOREIGN KEY (#wrapColumn( index.getForeignKey() )#)",
"REFERENCES #wrapTable( index.getTable() )# (#references#)",
"ON UPDATE #ucase( index.getOnUpdate() )#",
Expand Down
9 changes: 6 additions & 3 deletions models/Schema/Column.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ component accessors="true" {
}

function references( columns ) {
arguments.type = "foreign";
arguments.foreignKey = getName();
return getBlueprint().addIndex( argumentCollection = arguments );
return getBlueprint().addIndex(
type = "foreign",
columns = columns,
foreignKey = getName(),
name = "fk_#getBlueprint().getTable()#_#getName()#"
);
}

function unsigned() {
Expand Down
15 changes: 15 additions & 0 deletions models/Schema/TableIndex.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ component accessors="true" {
return this;
}

function onTable( table ) {
setTable( table );
return this;
}

function onDelete( option ) {
setOnDelete( option );
return this;
}

function onCascade( option ) {
setOnCascade( option );
return this;
}

function setColumns( columns ) {
variables.columns = isArray( arguments.columns ) ? arguments.columns : [ arguments.columns ];
return this;
Expand Down
57 changes: 54 additions & 3 deletions tests/specs/Schema/SchemaBuilder+GrammarSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ component extends="testbox.system.BaseSpec" {
table.string( "first_name" );
table.string( "last_name" );
table.string( "password", 100 );
table.unsignedInteger( "country_id" ).references( "id" ).setTable( "countries" ).setOnDelete( "cascade" );
table.unsignedInteger( "country_id" ).references( "id" ).onTable( "countries" ).onDelete( "cascade" );
table.timestamp( "created_date" ).setDefault( "CURRENT_TIMESTAMP" );
table.timestamp( "modified_date" ).setDefault( "CURRENT_TIMESTAMP" );
}, {}, false );
var statements = blueprint.toSql();
expect( statements ).toBeArray();
expect( statements ).toHaveLength( 1 );
expect( statements[ 1 ] ).toBeWithCase( "CREATE TABLE ""users"" (""id"" INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT, ""username"" VARCHAR(255) NOT NULL, ""first_name"" VARCHAR(255) NOT NULL, ""last_name"" VARCHAR(255) NOT NULL, ""password"" VARCHAR(100) NOT NULL, ""country_id"" INTEGER(10) UNSIGNED NOT NULL, ""created_date"" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, ""modified_date"" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT ""pk_users_id"" PRIMARY KEY (""id""), CONSTRAINT ""fk_country_id"" FOREIGN KEY (""country_id"") REFERENCES ""countries"" (""id"") ON UPDATE NONE ON DELETE CASCADE)" );
expect( statements[ 1 ] ).toBeWithCase( "CREATE TABLE ""users"" (""id"" INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT, ""username"" VARCHAR(255) NOT NULL, ""first_name"" VARCHAR(255) NOT NULL, ""last_name"" VARCHAR(255) NOT NULL, ""password"" VARCHAR(100) NOT NULL, ""country_id"" INTEGER(10) UNSIGNED NOT NULL, ""created_date"" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, ""modified_date"" TIMESTAMP 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 NONE ON DELETE CASCADE)" );
} );

describe( "column types", function() {
Expand Down Expand Up @@ -724,7 +724,7 @@ component extends="testbox.system.BaseSpec" {
} );
} );

describe( "primary indexes", function() {
describe( "primary key indexes", function() {
it( "column primary key", function() {
var schema = getBuilder();
var blueprint = schema.create( "users", function( table ) {
Expand Down Expand Up @@ -774,6 +774,57 @@ component extends="testbox.system.BaseSpec" {
expect( statements[ 1 ] ).toBeWithCase( "CREATE TABLE ""users"" (""first_name"" VARCHAR(255) NOT NULL, ""last_name"" VARCHAR(255) NOT NULL, CONSTRAINT ""pk_full_name"" PRIMARY KEY (""first_name"", ""last_name""))" );
} );
} );

xdescribe( "foreign key indexes", function() {
it( "column foreign key", function() {
var schema = getBuilder();
var blueprint = schema.create( "posts", function( table ) {
table.unsignedInteger( "author_id" ).references( "id" ).onTable( "users" );
}, {}, false );
var statements = blueprint.toSql();
expect( statements ).toBeArray();
expect( statements ).toHaveLength( 1 );
expect( statements[ 1 ] ).toBeWithCase( "CREATE TABLE ""posts"" (""author_id"" INTEGER(10) UNSIGNED NOT NULL, CONSTRAINT ""fk_posts_author_id"" FOREIGN KEY (""author_id"") REFERENCES ""users"" (""id"") ON UPDATE NONE ON DELETE CASCADE)" );
} );

it( "table primary key", function() {
var schema = getBuilder();
var blueprint = schema.create( "users", function( table ) {
table.string( "uuid" );
table.primaryKey( "uuid" );
}, {}, false );
var statements = blueprint.toSql();
expect( statements ).toBeArray();
expect( statements ).toHaveLength( 1 );
expect( statements[ 1 ] ).toBeWithCase( "CREATE TABLE ""users"" (""uuid"" VARCHAR(255) NOT NULL, CONSTRAINT ""pk_users_uuid"" PRIMARY KEY (""uuid""))" );
} );

it( "composite primary key", function() {
var schema = getBuilder();
var blueprint = schema.create( "users", function( table ) {
table.string( "first_name" );
table.string( "last_name" );
table.primaryKey( [ "first_name", "last_name" ] );
}, {}, false );
var statements = blueprint.toSql();
expect( statements ).toBeArray();
expect( statements ).toHaveLength( 1 );
expect( statements[ 1 ] ).toBeWithCase( "CREATE TABLE ""users"" (""first_name"" VARCHAR(255) NOT NULL, ""last_name"" VARCHAR(255) NOT NULL, CONSTRAINT ""pk_users_first_name_last_name"" PRIMARY KEY (""first_name"", ""last_name""))" );
} );

it( "override primary key index name", function() {
var schema = getBuilder();
var blueprint = schema.create( "users", function( table ) {
table.string( "first_name" );
table.string( "last_name" );
table.primaryKey( [ "first_name", "last_name" ], "pk_full_name" );
}, {}, false );
var statements = blueprint.toSql();
expect( statements ).toBeArray();
expect( statements ).toHaveLength( 1 );
expect( statements[ 1 ] ).toBeWithCase( "CREATE TABLE ""users"" (""first_name"" VARCHAR(255) NOT NULL, ""last_name"" VARCHAR(255) NOT NULL, CONSTRAINT ""pk_full_name"" PRIMARY KEY (""first_name"", ""last_name""))" );
} );
} );
} );

describe( "rename tables", function() {
Expand Down

0 comments on commit 7b0f9f0

Please sign in to comment.