summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2011-11-23 12:10:21 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2011-11-23 12:10:21 -0500
commit7a5641db0bb3d2391fd44f6c631601bf44e4bb26 (patch)
tree33895dc0d34302cc297133f4b8c7f1a4a2e2f9d5
parent0124904dd2300caf03e1c2119f4169569a931138 (diff)
downloadalembic-7a5641db0bb3d2391fd44f6c631601bf44e4bb26.tar.gz
add rename column support
-rw-r--r--alembic/ddl/base.py8
-rw-r--r--alembic/ddl/impl.py5
-rw-r--r--alembic/ddl/mssql.py14
-rw-r--r--tests/test_mssql.py16
-rw-r--r--tests/test_op.py7
5 files changed, 47 insertions, 3 deletions
diff --git a/alembic/ddl/base.py b/alembic/ddl/base.py
index 8f1ec1c..697d99e 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 3a3ffe7..ffbf6d1 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 3c489e1..d9d0fac 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 1cb7465..f229c0e 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 7503abf..f8554f2 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',