Skip to content

Commit

Permalink
Rename columns
Browse files Browse the repository at this point in the history
MySQL has an unfortunate syntax that requires the definition to be repeated.
We may be able to discover this from the table, but right now we're punting and asking the user to redeclare the column definition.

Fun fact, in MySQL, renameColumn will let you modifyColumn at the same time.
  • Loading branch information
elpete committed Sep 22, 2017
1 parent daa13fa commit 0bb926e
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 8 deletions.
12 changes: 12 additions & 0 deletions models/Grammars/BaseGrammar.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,18 @@ component displayname="Grammar" accessors="true" {
} ), " " );
}

function compileRenameColumn( blueprint, commandParameters ) {
return arrayToList( arrayFilter( [
"ALTER TABLE",
wrapTable( blueprint.getTable() ),
"CHANGE",
wrapColumn( commandParameters.from ),
compileCreateColumn( commandParameters.to )
], function( item ) {
return item != "";
} ), " " );
}

/*===== End of Blueprint: Alter ======*/

/*====================================
Expand Down
13 changes: 13 additions & 0 deletions models/Grammars/OracleGrammar.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,17 @@ component extends="qb.models.Grammars.BaseGrammar" {
return super.wrapValue( uCase( arguments.value ) );
}

function compileRenameColumn( blueprint, commandParameters ) {
return arrayToList( arrayFilter( [
"ALTER TABLE",
wrapTable( blueprint.getTable() ),
"RENAME COLUMN",
wrapColumn( commandParameters.from ),
"TO",
wrapColumn( commandParameters.to.getName() )
], function( item ) {
return item != "";
} ), " " );
}

}
5 changes: 5 additions & 0 deletions models/Schema/Blueprint.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ component accessors="true" {
return dropColumn;
}

function renameColumn( name, newColumnDefinition ) {
addCommand( "renameColumn", { from = name, to = newColumnDefinition } );
return this;
}

/*===== End of Alter Commands ======*/


Expand Down
12 changes: 8 additions & 4 deletions models/Schema/Column.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@ component accessors="true" {
}

function comment( comment ) {
return setComment( comment );
setComment( comment );
return this;
}

function default( value ) {
return setDefault( value );
setDefault( value );
return this;
}

function nullable() {
return setNullable( true );
setNullable( true );
return this;
}

function references( column ) {
Expand All @@ -38,7 +41,8 @@ component accessors="true" {
}

function unsigned() {
return setUnsigned( true );
setUnsigned( true );
return this;
}

}
28 changes: 25 additions & 3 deletions tests/specs/Schema/SchemaBuilder+GrammarSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ component extends="testbox.system.BaseSpec" {
} );
} );

describe( "rename", function() {
describe( "rename tables", function() {
it( "rename table", function() {
var schema = getBuilder();
var blueprint = schema.rename( "workers", "employees", {}, false );
Expand All @@ -639,9 +639,31 @@ component extends="testbox.system.BaseSpec" {
expect( statements ).toHaveLength( 1 );
expect( statements[ 1 ] ).toBeWithCase( "ALTER TABLE ""workers"" RENAME TO ""employees""" );
} );
} );

xit( "rename column", function() {
fail( "test not implemented yet" );
describe( "rename columns", function() {
it( "renames a column", function() {
var schema = getBuilder();
var blueprint = schema.alter( "users", function( table ) {
table.renameColumn( "name", table.string( "username" ) );
}, {}, false );
var statements = blueprint.toSql();
expect( statements ).toBeArray();
expect( statements ).toHaveLength( 1 );
expect( statements[ 1 ] ).toBeWithCase( "ALTER TABLE ""users"" CHANGE ""name"" ""username"" VARCHAR(255) NOT NULL" );
} );

it( "renames multiple columns", function() {
var schema = getBuilder();
var blueprint = schema.alter( "users", function( table ) {
table.renameColumn( "name", table.string( "username" ) );
table.renameColumn( "purchase_date", table.timestamp( "purchased_at" ).nullable() );
}, {}, false );
var statements = blueprint.toSql();
expect( statements ).toBeArray();
expect( statements ).toHaveLength( 2 );
expect( statements[ 1 ] ).toBeWithCase( "ALTER TABLE ""users"" CHANGE ""name"" ""username"" VARCHAR(255) NOT NULL" );
expect( statements[ 2 ] ).toBeWithCase( "ALTER TABLE ""users"" CHANGE ""purchase_date"" ""purchased_at"" TIMESTAMP" );
} );
} );

Expand Down
28 changes: 27 additions & 1 deletion tests/specs/Schema/SchemaBuilder+MySQLGrammarSpec.cfc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
component extends="testbox.system.BaseSpec" {

function run() {
describe( "schema builder + basic grammar", function() {
describe( "schema builder + mysql grammar", function() {
describe( "rename", function() {
it( "rename table", function() {
var schema = getBuilder();
Expand All @@ -12,6 +12,32 @@ component extends="testbox.system.BaseSpec" {
expect( statements[ 1 ] ).toBeWithCase( "RENAME TABLE `workers` TO `employees`" );
} );
} );

describe( "rename columns", function() {
it( "renames a column", function() {
var schema = getBuilder();
var blueprint = schema.alter( "users", function( table ) {
table.renameColumn( "name", table.string( "username" ) );
}, {}, false );
var statements = blueprint.toSql();
expect( statements ).toBeArray();
expect( statements ).toHaveLength( 1 );
expect( statements[ 1 ] ).toBeWithCase( "ALTER TABLE `users` CHANGE `name` `username` VARCHAR(255) NOT NULL" );
} );

it( "renames multiple columns", function() {
var schema = getBuilder();
var blueprint = schema.alter( "users", function( table ) {
table.renameColumn( "name", table.string( "username" ) );
table.renameColumn( "purchase_date", table.timestamp( "purchased_at" ).nullable() );
}, {}, false );
var statements = blueprint.toSql();
expect( statements ).toBeArray();
expect( statements ).toHaveLength( 2 );
expect( statements[ 1 ] ).toBeWithCase( "ALTER TABLE `users` CHANGE `name` `username` VARCHAR(255) NOT NULL" );
expect( statements[ 2 ] ).toBeWithCase( "ALTER TABLE `users` CHANGE `purchase_date` `purchased_at` TIMESTAMP" );
} );
} );
} );
}

Expand Down
41 changes: 41 additions & 0 deletions tests/specs/Schema/SchemaBuilder+OracleGrammarSpec.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
component extends="testbox.system.BaseSpec" {

function run() {
describe( "schema builder + oracle grammar", function() {
describe( "rename columns", function() {
it( "renames a column", function() {
var schema = getBuilder();
var blueprint = schema.alter( "users", function( table ) {
table.renameColumn( "name", table.string( "username" ) );
}, {}, false );
var statements = blueprint.toSql();
expect( statements ).toBeArray();
expect( statements ).toHaveLength( 1 );
expect( statements[ 1 ] ).toBeWithCase( "ALTER TABLE ""USERS"" RENAME COLUMN ""NAME"" TO ""USERNAME""" );
} );

it( "renames multiple columns", function() {
var schema = getBuilder();
var blueprint = schema.alter( "users", function( table ) {
table.renameColumn( "name", table.string( "username" ) );
table.renameColumn( "purchase_date", table.timestamp( "purchased_at" ).nullable() );
}, {}, false );
var statements = blueprint.toSql();
expect( statements ).toBeArray();
expect( statements ).toHaveLength( 2 );
expect( statements[ 1 ] ).toBeWithCase( "ALTER TABLE ""USERS"" RENAME COLUMN ""NAME"" TO ""USERNAME""" );
expect( statements[ 2 ] ).toBeWithCase( "ALTER TABLE ""USERS"" RENAME COLUMN ""PURCHASE_DATE"" TO ""PURCHASED_AT""" );
} );
} );
} );
}

private function getBuilder() {
var grammar = getMockBox()
.createMock( "qb.models.Grammars.OracleGrammar" );
var builder = getMockBox().createMock( "qb.models.Schema.SchemaBuilder" )
.init( grammar );
return builder;
}

}

0 comments on commit 0bb926e

Please sign in to comment.