From fb76853754e71a8c736396bb5ee70ec6f688a506 Mon Sep 17 00:00:00 2001 From: Eric Peterson Date: Wed, 16 Aug 2017 21:17:45 -0600 Subject: [PATCH] Add integer, unsignedInteger, increments, and text types --- models/Grammars/Grammar.cfc | 8 +++-- models/Schema/Blueprint.cfc | 16 +++++++++ models/Schema/Column.cfc | 3 +- .../Schema/SchemaBuilder+GrammarSpec.cfc | 36 +++++++++++++++---- 4 files changed, 54 insertions(+), 9 deletions(-) diff --git a/models/Grammars/Grammar.cfc b/models/Grammars/Grammar.cfc index 00b29db7..2a6ea055 100644 --- a/models/Grammars/Grammar.cfc +++ b/models/Grammars/Grammar.cfc @@ -779,12 +779,16 @@ component displayname="Grammar" accessors="true" { return "BIGINT"; } + function typeInteger( column ) { + return "INTEGER(#column.getPrecision()#)"; + } + function typeString( column ) { return "VARCHAR(#column.getLength()#)"; } - function typeInteger( column ) { - return "INT"; + function typeText( column ) { + return "TEXT"; } function typeTimestamp( column ) { diff --git a/models/Schema/Blueprint.cfc b/models/Schema/Blueprint.cfc index 4d192784..9867f08a 100644 --- a/models/Schema/Blueprint.cfc +++ b/models/Schema/Blueprint.cfc @@ -56,6 +56,7 @@ component accessors="true" { function bigIncrements( name ) { arguments.autoIncrement = true; + addIndex( type = "primary", column = name ); return unsignedBigInteger( argumentCollection = arguments ); } @@ -75,6 +76,11 @@ component accessors="true" { return unsignedInt( argumentCollection = arguments ); } + function integer( name, precision = 10 ) { + arguments.type = "integer"; + return addColumn( argumentCollection = arguments ); + } + function tinyInteger( name, length = "" ) { arguments.type = "tinyInteger"; return addColumn( argumentCollection = arguments ); @@ -85,6 +91,11 @@ component accessors="true" { return bigInteger( argumentCollection = arguments ); } + function unsignedInteger( name ) { + arguments.unsigned = true; + return integer( argumentCollection = arguments ); + } + function unsignedInt( name ) { arguments.type = "integer"; arguments.unsigned = true; @@ -99,6 +110,11 @@ component accessors="true" { return addColumn( argumentCollection = arguments ); } + function text( name ) { + arguments.type = "text"; + return addColumn( argumentCollection = arguments ); + } + function timestamp( name ) { arguments.type = "timestamp"; return addColumn( argumentCollection = arguments ); diff --git a/models/Schema/Column.cfc b/models/Schema/Column.cfc index 07da593d..52029910 100644 --- a/models/Schema/Column.cfc +++ b/models/Schema/Column.cfc @@ -5,6 +5,7 @@ component accessors="true" { property name="name"; property name="type"; property name="length" default="255"; + property name="precision" default="10"; property name="nullable" default="false"; property name="unsigned" default="false"; property name="autoIncrement" default="false"; @@ -21,4 +22,4 @@ component accessors="true" { return getBlueprint().addIndex( argumentCollection = arguments ); } -} \ No newline at end of file +} diff --git a/tests/specs/Schema/SchemaBuilder+GrammarSpec.cfc b/tests/specs/Schema/SchemaBuilder+GrammarSpec.cfc index 927db846..7dda8ba9 100644 --- a/tests/specs/Schema/SchemaBuilder+GrammarSpec.cfc +++ b/tests/specs/Schema/SchemaBuilder+GrammarSpec.cfc @@ -30,7 +30,7 @@ component extends="testbox.system.BaseSpec" { table.timestamp( "created_date" ).setDefault( "CURRENT_TIMESTAMP" ); table.timestamp( "modified_date" ).setDefault( "CURRENT_TIMESTAMP" ); }, false ); - expect( blueprint.toSql() ).toBeWithCase( "CREATE TABLE ""users"" (""id"" INT 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"" INT 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( blueprint.toSql() ).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)" ); } ); describe( "column types", function() { @@ -39,7 +39,7 @@ component extends="testbox.system.BaseSpec" { var blueprint = schema.create( "users", function( table ) { table.bigIncrements( "id" ); }, false ); - expect( blueprint.toSql() ).toBeWithCase( "CREATE TABLE ""users"" (""id"" BIGINT UNSIGNED NOT NULL AUTO_INCREMENT)" ); + expect( blueprint.toSql() ).toBeWithCase( "CREATE TABLE ""users"" (""id"" BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, PRIMARY KEY (""id""))" ); } ); it( "bigInteger", function() { @@ -87,11 +87,27 @@ component extends="testbox.system.BaseSpec" { } ); it( "increments", function() { - fail( "test not implemented yet" ); + var schema = getBuilder(); + var blueprint = schema.create( "users", function( table ) { + table.increments( "id" ); + }, false ); + expect( blueprint.toSql() ).toBeWithCase( "CREATE TABLE ""users"" (""id"" INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT, PRIMARY KEY (""id""))" ); } ); it( "integer", function() { - fail( "test not implemented yet" ); + var schema = getBuilder(); + var blueprint = schema.create( "users", function( table ) { + table.integer( "age" ); + }, false ); + expect( blueprint.toSql() ).toBeWithCase( "CREATE TABLE ""users"" (""age"" INTEGER(10) NOT NULL)" ); + } ); + + it( "integer with precision", function() { + var schema = getBuilder(); + var blueprint = schema.create( "users", function( table ) { + table.integer( "age", 2 ); + }, false ); + expect( blueprint.toSql() ).toBeWithCase( "CREATE TABLE ""users"" (""age"" INTEGER(2) NOT NULL)" ); } ); it( "json", function() { @@ -135,7 +151,11 @@ component extends="testbox.system.BaseSpec" { } ); it( "text", function() { - fail( "test not implemented yet" ); + var schema = getBuilder(); + var blueprint = schema.create( "posts", function( table ) { + table.text( "body" ); + }, false ); + expect( blueprint.toSql() ).toBeWithCase( "CREATE TABLE ""posts"" (""body"" TEXT NOT NULL)" ); } ); it( "time", function() { @@ -179,7 +199,11 @@ component extends="testbox.system.BaseSpec" { } ); it( "unsignedInteger", function() { - fail( "test not implemented yet" ); + var schema = getBuilder(); + var blueprint = schema.create( "users", function( table ) { + table.unsignedInteger( "age" ); + }, false ); + expect( blueprint.toSql() ).toBeWithCase( "CREATE TABLE ""users"" (""age"" INTEGER(10) UNSIGNED NOT NULL)" ); } ); it( "unsignedMediumInteger", function() {