From: Mike Bayer Date: Thu, 6 Feb 2020 15:56:17 +0000 (-0500) Subject: Render inline constraints for add_column X-Git-Tag: rel_1_4_1~10^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7f8434c47bc80dd31fd29333aa16103e0707b38c;p=thirdparty%2Fsqlalchemy%2Falembic.git Render inline constraints for add_column 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 --- diff --git a/alembic/ddl/base.py b/alembic/ddl/base.py index 9f3a622f..b8d9dce4 100644 --- a/alembic/ddl/base.py +++ b/alembic/ddl/base.py @@ -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 diff --git a/alembic/operations/toimpl.py b/alembic/operations/toimpl.py index 24a2e370..3114a66f 100644 --- a/alembic/operations/toimpl.py +++ b/alembic/operations/toimpl.py @@ -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 index 00000000..9d9ebfcc --- /dev/null +++ b/docs/build/unreleased/655.rst @@ -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. + + diff --git a/tests/test_op.py b/tests/test_op.py index c3fb76e2..98daa5fe 100644 --- a/tests/test_op.py +++ b/tests/test_op.py @@ -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(