From d63a4ae2f85a7cb433cf57721ecd3bf9da8c2730 Mon Sep 17 00:00:00 2001 From: Eric Peterson Date: Sat, 23 Sep 2017 06:53:29 -0600 Subject: [PATCH] Fix primary index names to include table names if no override provided --- models/Grammars/BaseGrammar.cfc | 2 +- models/Schema/Blueprint.cfc | 32 +++-- .../Schema/SchemaBuilder+GrammarSpec.cfc | 135 ++++++++++++------ .../Schema/SchemaBuilder+MySQLGrammarSpec.cfc | 53 ++++--- 4 files changed, 147 insertions(+), 75 deletions(-) diff --git a/models/Grammars/BaseGrammar.cfc b/models/Grammars/BaseGrammar.cfc index 5cdb3099..56bdaaab 100644 --- a/models/Grammars/BaseGrammar.cfc +++ b/models/Grammars/BaseGrammar.cfc @@ -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 ) { diff --git a/models/Schema/Blueprint.cfc b/models/Schema/Blueprint.cfc index eed0e800..26dac172 100644 --- a/models/Schema/Blueprint.cfc +++ b/models/Schema/Blueprint.cfc @@ -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 ======*/ @@ -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 ); } @@ -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 ); } @@ -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 ); } @@ -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 ); } @@ -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 ); } diff --git a/tests/specs/Schema/SchemaBuilder+GrammarSpec.cfc b/tests/specs/Schema/SchemaBuilder+GrammarSpec.cfc index 7287c425..673e4105 100644 --- a/tests/specs/Schema/SchemaBuilder+GrammarSpec.cfc +++ b/tests/specs/Schema/SchemaBuilder+GrammarSpec.cfc @@ -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() { @@ -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() { @@ -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() { @@ -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() { @@ -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() { @@ -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() { @@ -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""))" ); + } ); } ); } ); diff --git a/tests/specs/Schema/SchemaBuilder+MySQLGrammarSpec.cfc b/tests/specs/Schema/SchemaBuilder+MySQLGrammarSpec.cfc index e6ded250..fc0bb2ca 100644 --- a/tests/specs/Schema/SchemaBuilder+MySQLGrammarSpec.cfc +++ b/tests/specs/Schema/SchemaBuilder+MySQLGrammarSpec.cfc @@ -88,27 +88,42 @@ component extends="testbox.system.BaseSpec" { } ); xdescribe( "indexes", function() { - it( "unique (off of column)", function() { - var schema = getBuilder(); - var blueprint = schema.create( "users", function( table ) { - table.string( "username" ).unique(); - }, {}, false ); - var statements = blueprint.toSql(); - expect( statements ).toBeArray(); - expect( statements ).toHaveLength( 1 ); - expect( statements[ 1 ] ).toBeWithCase( "CREATE TABLE `users` (`username` VARCHAR(255) NOT NULL, CONSTRAINT unique_username UNIQUE (`username`))" ); + describe( "unique", function() { + it( "unique (off of column)", function() { + var schema = getBuilder(); + var blueprint = schema.create( "users", function( table ) { + table.string( "username" ).unique(); + }, {}, false ); + var statements = blueprint.toSql(); + expect( statements ).toBeArray(); + expect( statements ).toHaveLength( 1 ); + expect( statements[ 1 ] ).toBeWithCase( "CREATE TABLE `users` (`username` VARCHAR(255) NOT NULL, CONSTRAINT unique_username UNIQUE (`username`))" ); + } ); + + it( "unique (off of table)", function() { + var schema = getBuilder(); + var blueprint = schema.create( "users", function( table ) { + table.string( "username" ); + table.unique( "username" ); + }, {}, false ); + var statements = blueprint.toSql(); + expect( statements ).toBeArray(); + expect( statements ).toHaveLength( 1 ); + expect( statements[ 1 ] ).toBeWithCase( "CREATE TABLE `users` (`username` VARCHAR(255) NOT NULL, UNIQUE (`username`))" ); + } ); } ); - it( "unique (off of table)", function() { - var schema = getBuilder(); - var blueprint = schema.create( "users", function( table ) { - table.string( "username" ); - table.unique( "username" ); - }, {}, false ); - var statements = blueprint.toSql(); - expect( statements ).toBeArray(); - expect( statements ).toHaveLength( 1 ); - expect( statements[ 1 ] ).toBeWithCase( "CREATE TABLE `users` (`username` VARCHAR(255) NOT NULL, UNIQUE (`username`))" ); + describe( "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 (`uuid`)" ); + } ); } ); } );