]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Render inline constraints for add_column
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 6 Feb 2020 15:56:17 +0000 (10:56 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 6 Feb 2020 15:56:17 +0000 (10:56 -0500)
Fixed long-standing bug where an inline column CHECK constraint would not
be rendered within an "ADD COLUMN" operation.  The DDL compiler is now
consulted for inline constraints within the :meth:`.Operations.add_column`
method as is done for regular CREATE TABLE operations.

Change-Id: Ib176d13dd9151e65a1ca27357f7da0d4523f0b2f
Fixes: #655
alembic/ddl/base.py
alembic/operations/toimpl.py
docs/build/unreleased/655.rst [new file with mode: 0644]
tests/test_op.py

index 9f3a622f15d01ddc2e97473a5a47e832f5f87da0..b8d9dce41cf4c4881e9e64a45c7c1498f5ddb477 100644 (file)
@@ -216,4 +216,12 @@ def alter_column(compiler, name):
 
 
 def add_column(compiler, column, **kw):
-    return "ADD COLUMN %s" % compiler.get_column_specification(column, **kw)
+    text = "ADD COLUMN %s" % compiler.get_column_specification(column, **kw)
+
+    const = " ".join(
+        compiler.process(constraint) for constraint in column.constraints
+    )
+    if const:
+        text += " " + const
+
+    return text
index 24a2e3706316e24981289636375991d204893459..3114a66fe0c945be98e9a45e9a57398e1d70358f 100644 (file)
@@ -130,6 +130,7 @@ def add_column(operations, operation):
 
     t = operations.schema_obj.table(table_name, column, schema=schema)
     operations.impl.add_column(table_name, column, schema=schema, **kw)
+
     for constraint in t.constraints:
         if not isinstance(constraint, sa_schema.PrimaryKeyConstraint):
             operations.impl.add_constraint(constraint)
diff --git a/docs/build/unreleased/655.rst b/docs/build/unreleased/655.rst
new file mode 100644 (file)
index 0000000..9d9ebfc
--- /dev/null
@@ -0,0 +1,10 @@
+.. change::
+    :tags: bug, operations
+    :tickets: 655
+
+    Fixed long-standing bug where an inline column CHECK constraint would not
+    be rendered within an "ADD COLUMN" operation.  The DDL compiler is now
+    consulted for inline constraints within the :meth:`.Operations.add_column`
+    method as is done for regular CREATE TABLE operations.
+
+
index c3fb76e243a0ef14e334923482053c9dea0910dd..98daa5fee2022ae7f1e48634269bd3b8b6c1f7f8 100644 (file)
@@ -1,6 +1,7 @@
 """Test against the builders in the op.* module."""
 
 from sqlalchemy import Boolean
+from sqlalchemy import CheckConstraint
 from sqlalchemy import Column
 from sqlalchemy import event
 from sqlalchemy import exc
@@ -110,6 +111,16 @@ class OpTest(TestBase):
         op.add_column("t1", Column("c1", Integer, nullable=False))
         context.assert_("ALTER TABLE t1 ADD COLUMN c1 INTEGER NOT NULL")
 
+    def test_add_column_w_check(self):
+        context = op_fixture()
+        op.add_column(
+            "t1",
+            Column("c1", Integer, CheckConstraint("c1 > 5"), nullable=False),
+        )
+        context.assert_(
+            "ALTER TABLE t1 ADD COLUMN c1 INTEGER NOT NULL CHECK (c1 > 5)"
+        )
+
     def test_add_column_schema(self):
         context = op_fixture()
         op.add_column(