Skip to content

Commit

Permalink
Fix primary index names to include table names if no override provided
Browse files Browse the repository at this point in the history
  • Loading branch information
elpete committed Sep 23, 2017
1 parent f247e15 commit d63a4ae
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 75 deletions.
2 changes: 1 addition & 1 deletion models/Grammars/BaseGrammar.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ component displayname="Grammar" accessors="true" {
var references = index.getColumns().map( function( column ) {
return wrapColumn( column );
} ).toList( ", " );
return "PRIMARY KEY (#references#)";
return "CONSTRAINT #wrapValue( index.getName() )# PRIMARY KEY (#references#)";
}

function indexUnique( index ) {
Expand Down
32 changes: 22 additions & 10 deletions models/Schema/Blueprint.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ component accessors="true" {
return this;
}

function primaryKey( columns, name ) {
arguments.columns = isArray( columns ) ? columns : [ columns ];
arguments.name = isNull( name ) ? "pk_#arrayToList( columns, "_" )#" : arguments.name;
addIndex( type = "primary", columns = columns, name = name );
return this;
}

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


Expand All @@ -159,9 +166,10 @@ component accessors="true" {
= Column Types =
====================================*/

function bigIncrements( name ) {
function bigIncrements( name, indexName ) {
arguments.autoIncrement = true;
addIndex( type = "primary", columns = name );
arguments.indexName = isNull( indexName ) ? "pk_#getTable()#_#name#" : arguments.indexName;
addIndex( type = "primary", columns = name, name = indexName );
return unsignedBigInteger( argumentCollection = arguments );
}

Expand Down Expand Up @@ -212,9 +220,10 @@ component accessors="true" {
return appendColumn( argumentCollection = arguments );
}

function increments( name ) {
function increments( name, indexName ) {
arguments.autoIncrement = true;
addIndex( type = "primary", columns = name );
arguments.indexName = isNull( indexName ) ? "pk_#getTable()#_#name#" : arguments.indexName;
addIndex( type = "primary", columns = name, name = indexName );
return unsignedInteger( argumentCollection = arguments );
}

Expand All @@ -233,9 +242,10 @@ component accessors="true" {
return appendColumn( argumentCollection = arguments );
}

function mediumIncrements( name ) {
function mediumIncrements( name, indexName ) {
arguments.autoIncrement = true;
addIndex( type = "primary", columns = name );
arguments.indexName = isNull( indexName ) ? "pk_#getTable()#_#name#" : arguments.indexName;
addIndex( type = "primary", columns = name, name = indexName );
return unsignedMediumInteger( argumentCollection = arguments );
}

Expand Down Expand Up @@ -271,9 +281,10 @@ component accessors="true" {
return this;
}

function smallIncrements( name ) {
function smallIncrements( name, indexName ) {
arguments.autoIncrement = true;
addIndex( type = "primary", columns = name );
arguments.indexName = isNull( indexName ) ? "pk_#getTable()#_#name#" : arguments.indexName;
addIndex( type = "primary", columns = name, name = indexName );
return unsignedSmallInteger( argumentCollection = arguments );
}

Expand Down Expand Up @@ -305,9 +316,10 @@ component accessors="true" {
return appendColumn( argumentCollection = arguments );
}

function tinyIncrements( name ) {
function tinyIncrements( name, indexName ) {
arguments.autoIncrement = true;
addIndex( type = "primary", columns = name );
arguments.indexName = isNull( indexName ) ? "pk_#getTable()#_#name#" : arguments.indexName;
addIndex( type = "primary", columns = name, name = indexName );
return unsignedTinyInteger( argumentCollection = arguments );
}

Expand Down
135 changes: 90 additions & 45 deletions tests/specs/Schema/SchemaBuilder+GrammarSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ component extends="testbox.system.BaseSpec" {
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, 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_country_id"" FOREIGN KEY (""country_id"") REFERENCES ""countries"" (""id"") ON UPDATE NONE ON DELETE CASCADE)" );
} );

describe( "column types", function() {
Expand All @@ -51,7 +51,7 @@ component extends="testbox.system.BaseSpec" {
var statements = blueprint.toSql();
expect( statements ).toBeArray();
expect( statements ).toHaveLength( 1 );
expect( statements[ 1 ] ).toBeWithCase( "CREATE TABLE ""users"" (""id"" BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, PRIMARY KEY (""id""))" );
expect( statements[ 1 ] ).toBeWithCase( "CREATE TABLE ""users"" (""id"" BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, CONSTRAINT ""pk_users_id"" PRIMARY KEY (""id""))" );
} );

it( "bigInteger", function() {
Expand Down Expand Up @@ -260,7 +260,7 @@ component extends="testbox.system.BaseSpec" {
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, PRIMARY KEY (""id""))" );
expect( statements[ 1 ] ).toBeWithCase( "CREATE TABLE ""users"" (""id"" INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT, CONSTRAINT ""pk_users_id"" PRIMARY KEY (""id""))" );
} );

it( "integer", function() {
Expand Down Expand Up @@ -315,7 +315,7 @@ component extends="testbox.system.BaseSpec" {
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, PRIMARY KEY (""id""))" );
expect( statements[ 1 ] ).toBeWithCase( "CREATE TABLE ""users"" (""id"" INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT, CONSTRAINT ""pk_users_id"" PRIMARY KEY (""id""))" );
} );

it( "mediumInteger", function() {
Expand Down Expand Up @@ -381,7 +381,7 @@ component extends="testbox.system.BaseSpec" {
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, PRIMARY KEY (""id""))" );
expect( statements[ 1 ] ).toBeWithCase( "CREATE TABLE ""users"" (""id"" INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT, CONSTRAINT ""pk_users_id"" PRIMARY KEY (""id""))" );
} );

it( "smallInteger", function() {
Expand Down Expand Up @@ -458,7 +458,7 @@ component extends="testbox.system.BaseSpec" {
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, PRIMARY KEY (""id""))" );
expect( statements[ 1 ] ).toBeWithCase( "CREATE TABLE ""users"" (""id"" INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT, CONSTRAINT ""pk_users_id"" PRIMARY KEY (""id""))" );
} );

it( "tinyInteger", function() {
Expand Down Expand Up @@ -684,50 +684,95 @@ component extends="testbox.system.BaseSpec" {
} );
} );

it( "basic index", function() {
var schema = getBuilder();
var blueprint = schema.create( "users", function( table ) {
table.timestamp( "published_date" );
table.index( "published_date" );
}, {}, false );
var statements = blueprint.toSql();
expect( statements ).toBeArray();
expect( statements ).toHaveLength( 1 );
expect( statements[ 1 ] ).toBeWithCase( "CREATE TABLE ""users"" (""published_date"" TIMESTAMP NOT NULL, INDEX ""idx_published_date"" (""published_date""))" );
} );
describe( "basic indexes", function() {
it( "basic index", function() {
var schema = getBuilder();
var blueprint = schema.create( "users", function( table ) {
table.timestamp( "published_date" );
table.index( "published_date" );
}, {}, false );
var statements = blueprint.toSql();
expect( statements ).toBeArray();
expect( statements ).toHaveLength( 1 );
expect( statements[ 1 ] ).toBeWithCase( "CREATE TABLE ""users"" (""published_date"" TIMESTAMP NOT NULL, INDEX ""idx_published_date"" (""published_date""))" );
} );

it( "composite index", function() {
var schema = getBuilder();
var blueprint = schema.create( "users", function( table ) {
table.string( "first_name" );
table.string( "last_name" );
table.index( [ "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, INDEX ""idx_first_name_last_name"" (""first_name"",""last_name""))" );
} );
it( "composite index", function() {
var schema = getBuilder();
var blueprint = schema.create( "users", function( table ) {
table.string( "first_name" );
table.string( "last_name" );
table.index( [ "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, INDEX ""idx_first_name_last_name"" (""first_name"",""last_name""))" );
} );

it( "override index name", function() {
var schema = getBuilder();
var blueprint = schema.create( "users", function( table ) {
table.string( "first_name" );
table.string( "last_name" );
table.index( [ "first_name", "last_name" ], "index_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, INDEX ""index_full_name"" (""first_name"",""last_name""))" );
it( "override index name", function() {
var schema = getBuilder();
var blueprint = schema.create( "users", function( table ) {
table.string( "first_name" );
table.string( "last_name" );
table.index( [ "first_name", "last_name" ], "index_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, INDEX ""index_full_name"" (""first_name"",""last_name""))" );
} );
} );

xit( "primary", function() {
fail( "test not implemented yet" );
} );
xdescribe( "primary indexes", function() {
it( "column primary key", function() {
var schema = getBuilder();
var blueprint = schema.create( "users", function( table ) {
table.string( "uuid" ).primaryKey();
}, {}, false );
var statements = blueprint.toSql();
expect( statements ).toBeArray();
expect( statements ).toHaveLength( 1 );
expect( statements[ 1 ] ).toBeWithCase( "CREATE TABLE ""users"" (""uuid"" VARCHAR(255) NOT NULL PRIMARY KEY" );
} );

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_uuid"" PRIMARY KEY (""uuid""))" );
} );

xit( "composite primary key", function() {
fail( "test not implemented yet" );
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_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""))" );
} );
} );
} );

Expand Down
Loading

0 comments on commit d63a4ae

Please sign in to comment.