]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
add rename column support
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 23 Nov 2011 17:10:21 +0000 (12:10 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 23 Nov 2011 17:10:21 +0000 (12:10 -0500)
alembic/ddl/base.py
alembic/ddl/impl.py
alembic/ddl/mssql.py
tests/test_mssql.py
tests/test_op.py

index 8f1ec1c5d40360b40cde3a44e7a0fed6f797d574..697d99ecaaae7574d39355369495ef2ba3d25930 100644 (file)
@@ -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"""
 
index 3a3ffe729c39fcf59db631ad2438aeeeefc195c7..ffbf6d1282e42292a7239077bf077f8d3921ea18 100644 (file)
@@ -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))
index 3c489e19b3b087b45a7b845efa2a6cc09f51f6ad..d9d0fac2cf88f4d6af30892ca8c64e4c1d31b3c7 100644 (file)
@@ -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)
+    )
index 1cb746513fe56478b378b23f3f290b27cf76247d..f229c0e31edbb503ed79a50aba5e3d8fb506bcda 100644 (file)
@@ -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'"
+#    )
+
index 7503abf57ea7a939646175570d1e5af6f78d4ec3..f8554f2a8539470c513d9c7dd8186c26fedada57 100644 (file)
@@ -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',