From a1feddafbd9806988ecc5c266d8a8fdb9f0d985d Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 20 Jan 2012 15:25:30 -0500 Subject: [PATCH] - [bug] Fixed alteration of column type on MSSQL to not include the keyword "TYPE". --- CHANGES | 3 +++ alembic/ddl/mssql.py | 9 ++++++++- tests/test_mssql.py | 7 +++++++ tests/test_op.py | 6 +++--- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/CHANGES b/CHANGES index c34c8bc7..52f48444 100644 --- a/CHANGES +++ b/CHANGES @@ -5,6 +5,9 @@ "scripts/alembic" as setuptools creates this for us. [#22] +- [bug] Fixed alteration of column type on + MSSQL to not include the keyword "TYPE". + 0.1.1 ===== - [bug] Clean up file write operations so that diff --git a/alembic/ddl/mssql.py b/alembic/ddl/mssql.py index 8102e012..2f8c9ebc 100644 --- a/alembic/ddl/mssql.py +++ b/alembic/ddl/mssql.py @@ -1,7 +1,7 @@ from alembic.ddl.impl import DefaultImpl from alembic.ddl.base import alter_table, AddColumn, ColumnName, \ format_table_name, format_column_name, ColumnNullable, alter_column,\ - format_server_default,ColumnDefault, format_type + format_server_default,ColumnDefault, format_type, ColumnType from alembic import util from sqlalchemy.ext.compiler import compiles @@ -158,4 +158,11 @@ def visit_rename_column(element, compiler, **kw): format_column_name(compiler, element.newname) ) +@compiles(ColumnType, 'mssql') +def visit_column_type(element, compiler, **kw): + return "%s %s %s" % ( + alter_table(compiler, element.table_name, element.schema), + alter_column(compiler, element.column_name), + format_type(compiler, element.type_) + ) diff --git a/tests/test_mssql.py b/tests/test_mssql.py index 88ed9b52..ed4751ee 100644 --- a/tests/test_mssql.py +++ b/tests/test_mssql.py @@ -58,6 +58,13 @@ class OpTest(TestCase): "EXEC sp_rename 't.c', 'x', 'COLUMN'" ) + def test_alter_column_new_type(self): + context = op_fixture('mssql') + op.alter_column("t", "c", type_=Integer) + context.assert_( + 'ALTER TABLE t ALTER COLUMN c INTEGER' + ) + def test_drop_column_w_default(self): context = op_fixture('mssql') op.drop_column('t1', 'c1', mssql_drop_default=True) diff --git a/tests/test_op.py b/tests/test_op.py index fe8bf296..c85869a8 100644 --- a/tests/test_op.py +++ b/tests/test_op.py @@ -124,7 +124,7 @@ def test_alter_column_schema_type_unnamed(): context = op_fixture('mssql') op.alter_column("t", "c", type_=Boolean()) context.assert_( - 'ALTER TABLE t ALTER COLUMN c TYPE BIT', + 'ALTER TABLE t ALTER COLUMN c BIT', 'ALTER TABLE t ADD CHECK (c IN (0, 1))' ) @@ -132,7 +132,7 @@ def test_alter_column_schema_type_named(): context = op_fixture('mssql') op.alter_column("t", "c", type_=Boolean(name="xyz")) context.assert_( - 'ALTER TABLE t ALTER COLUMN c TYPE BIT', + 'ALTER TABLE t ALTER COLUMN c BIT', 'ALTER TABLE t ADD CONSTRAINT xyz CHECK (c IN (0, 1))' ) @@ -141,7 +141,7 @@ def test_alter_column_schema_type_existing_type(): op.alter_column("t", "c", type_=String(10), existing_type=Boolean(name="xyz")) context.assert_( 'ALTER TABLE t DROP CONSTRAINT xyz', - 'ALTER TABLE t ALTER COLUMN c TYPE VARCHAR(10)' + 'ALTER TABLE t ALTER COLUMN c VARCHAR(10)' ) def test_add_foreign_key(): -- 2.47.2