From: Mike Bayer Date: Wed, 23 Nov 2011 17:10:21 +0000 (-0500) Subject: add rename column support X-Git-Tag: rel_0_1_0~45 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7a5641db0bb3d2391fd44f6c631601bf44e4bb26;p=thirdparty%2Fsqlalchemy%2Falembic.git add rename column support --- diff --git a/alembic/ddl/base.py b/alembic/ddl/base.py index 8f1ec1c5..697d99ec 100644 --- a/alembic/ddl/base.py +++ b/alembic/ddl/base.py @@ -70,6 +70,14 @@ def visit_column_nullable(element, compiler, **kw): "NULL" if element.nullable else "SET NOT NULL" ) +@compiles(ColumnName) +def visit_column_name(element, compiler, **kw): + return "%s %s RENAME TO %s" % ( + alter_table(compiler, element.table_name, element.schema), + alter_column(compiler, element.column_name), + format_column_name(compiler, element.newname) + ) + def quote_dotted(name, quote): """quote the elements of a dotted name""" diff --git a/alembic/ddl/impl.py b/alembic/ddl/impl.py index 3a3ffe72..ffbf6d12 100644 --- a/alembic/ddl/impl.py +++ b/alembic/ddl/impl.py @@ -81,6 +81,11 @@ class DefaultImpl(object): self._exec(base.ColumnType( table_name, column_name, type_, schema=schema )) + # do the new name last ;) + if name is not None: + self._exec(base.ColumnName( + table_name, column_name, name, schema=schema + )) def add_column(self, table_name, column): self._exec(base.AddColumn(table_name, column)) diff --git a/alembic/ddl/mssql.py b/alembic/ddl/mssql.py index 3c489e19..d9d0fac2 100644 --- a/alembic/ddl/mssql.py +++ b/alembic/ddl/mssql.py @@ -1,5 +1,5 @@ from alembic.ddl.impl import DefaultImpl -from alembic.ddl.base import alter_table, AddColumn +from alembic.ddl.base import alter_table, AddColumn, ColumnName, format_table_name, format_column_name from sqlalchemy.ext.compiler import compiles class MSSQLImpl(DefaultImpl): @@ -25,9 +25,17 @@ class MSSQLImpl(DefaultImpl): def visit_add_column(element, compiler, **kw): return "%s %s" % ( alter_table(compiler, element.table_name, element.schema), - mysql_add_column(compiler, element.column, **kw) + mssql_add_column(compiler, element.column, **kw) ) -def mysql_add_column(compiler, column, **kw): +def mssql_add_column(compiler, column, **kw): return "ADD %s" % compiler.get_column_specification(column, **kw) + +@compiles(ColumnName, 'mssql') +def visit_rename_column(element, compiler, **kw): + return "EXEC sp_rename '%s.%s', '%s', 'COLUMN'" % ( + format_table_name(compiler, element.table_name, element.schema), + format_column_name(compiler, element.column_name), + format_column_name(compiler, element.newname) + ) diff --git a/tests/test_mssql.py b/tests/test_mssql.py index 1cb74651..f229c0e3 100644 --- a/tests/test_mssql.py +++ b/tests/test_mssql.py @@ -15,3 +15,19 @@ def test_add_column_with_default(): context = _op_fixture("mssql") op.add_column('t1', Column('c1', Integer, nullable=False, server_default="12")) context.assert_("ALTER TABLE t1 ADD c1 INTEGER NOT NULL DEFAULT '12'") + +def test_alter_column_rename_mssql(): + context = _op_fixture('mssql') + op.alter_column("t", "c", name="x") + context.assert_( + "EXEC sp_rename 't.c', 'x', 'COLUMN'" + ) + +# TODO: when we add schema support +#def test_alter_column_rename_mssql_schema(): +# context = _op_fixture('mssql') +# op.alter_column("t", "c", name="x", schema="y") +# context.assert_( +# "EXEC sp_rename 'y.t.c', 'x', 'COLUMN'" +# ) + diff --git a/tests/test_op.py b/tests/test_op.py index 7503abf5..f8554f2a 100644 --- a/tests/test_op.py +++ b/tests/test_op.py @@ -54,6 +54,13 @@ def test_alter_column_not_nullable(): "ALTER TABLE t ALTER COLUMN c SET NOT NULL" ) +def test_alter_column_rename(): + context = _op_fixture() + op.alter_column("t", "c", name="x") + context.assert_( + "ALTER TABLE t ALTER COLUMN c RENAME TO x" + ) + def test_add_foreign_key(): context = _op_fixture() op.create_foreign_key('fk_test', 't1', 't2',