From: Aaron Griffin Date: Thu, 24 Apr 2025 18:01:32 +0000 (-0500) Subject: Support DROP CONSTRAINT IF EXISTS X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3e381a52c7860934ad969c0429635abdf73cd1e3;p=thirdparty%2Fsqlalchemy%2Falembic.git Support DROP CONSTRAINT IF EXISTS --- diff --git a/alembic/ddl/impl.py b/alembic/ddl/impl.py index 9331f363..a1070223 100644 --- a/alembic/ddl/impl.py +++ b/alembic/ddl/impl.py @@ -387,8 +387,8 @@ class DefaultImpl(metaclass=ImplMeta): if const._create_rule is None or const._create_rule(self): self._exec(schema.AddConstraint(const)) - def drop_constraint(self, const: Constraint) -> None: - self._exec(schema.DropConstraint(const)) + def drop_constraint(self, const: Constraint, **kw: Any) -> None: + self._exec(schema.DropConstraint(const, **kw)) def rename_table( self, diff --git a/alembic/op.pyi b/alembic/op.pyi index 14e6c39a..ed7b9ded 100644 --- a/alembic/op.pyi +++ b/alembic/op.pyi @@ -957,6 +957,7 @@ def drop_constraint( type_: Optional[str] = None, *, schema: Optional[str] = None, + if_exists: Optional[bool] = None, ) -> None: r"""Drop a constraint of the given name, typically via DROP CONSTRAINT. @@ -968,6 +969,10 @@ def drop_constraint( quoting of the schema outside of the default behavior, use the SQLAlchemy construct :class:`~sqlalchemy.sql.elements.quoted_name`. + :param if_exists: If True, adds IF EXISTS operator when + dropping the constraint + + .. versionadded:: 1.15.4 """ diff --git a/alembic/operations/base.py b/alembic/operations/base.py index d7823a5b..afcb8ff9 100644 --- a/alembic/operations/base.py +++ b/alembic/operations/base.py @@ -1393,6 +1393,7 @@ class Operations(AbstractOperations): type_: Optional[str] = None, *, schema: Optional[str] = None, + if_exists: Optional[bool] = None, ) -> None: r"""Drop a constraint of the given name, typically via DROP CONSTRAINT. @@ -1404,6 +1405,10 @@ class Operations(AbstractOperations): quoting of the schema outside of the default behavior, use the SQLAlchemy construct :class:`~sqlalchemy.sql.elements.quoted_name`. + :param if_exists: If True, adds IF EXISTS operator when + dropping the constraint + + .. versionadded:: 1.15.4 """ # noqa: E501 ... diff --git a/alembic/operations/ops.py b/alembic/operations/ops.py index 8c0bdf85..c8018995 100644 --- a/alembic/operations/ops.py +++ b/alembic/operations/ops.py @@ -140,12 +140,14 @@ class DropConstraintOp(MigrateOperation): type_: Optional[str] = None, *, schema: Optional[str] = None, + if_exists: Optional[bool] = None, _reverse: Optional[AddConstraintOp] = None, ) -> None: self.constraint_name = constraint_name self.table_name = table_name self.constraint_type = type_ self.schema = schema + self.if_exists = if_exists self._reverse = _reverse def reverse(self) -> AddConstraintOp: @@ -203,6 +205,7 @@ class DropConstraintOp(MigrateOperation): type_: Optional[str] = None, *, schema: Optional[str] = None, + if_exists: Optional[bool] = None, ) -> None: r"""Drop a constraint of the given name, typically via DROP CONSTRAINT. @@ -214,10 +217,14 @@ class DropConstraintOp(MigrateOperation): quoting of the schema outside of the default behavior, use the SQLAlchemy construct :class:`~sqlalchemy.sql.elements.quoted_name`. + :param if_exists: If True, adds IF EXISTS operator when + dropping the constraint + + .. versionadded:: 1.15.4 """ - op = cls(constraint_name, table_name, type_=type_, schema=schema) + op = cls(constraint_name, table_name, type_=type_, schema=schema, if_exists=if_exists) return operations.invoke(op) @classmethod diff --git a/alembic/operations/toimpl.py b/alembic/operations/toimpl.py index 528c0542..c8190965 100644 --- a/alembic/operations/toimpl.py +++ b/alembic/operations/toimpl.py @@ -197,13 +197,17 @@ def create_constraint( def drop_constraint( operations: "Operations", operation: "ops.DropConstraintOp" ) -> None: + kw = {} + if operation.if_exists is not None: + kw["if_exists"] = operation.if_exists operations.impl.drop_constraint( operations.schema_obj.generic_constraint( operation.constraint_name, operation.table_name, operation.constraint_type, schema=operation.schema, - ) + ), + **kw ) diff --git a/tests/test_op.py b/tests/test_op.py index d9e65091..4522e44c 100644 --- a/tests/test_op.py +++ b/tests/test_op.py @@ -821,6 +821,11 @@ class OpTest(TestBase): op.drop_constraint("foo_bar_bat", "t1", schema="foo") context.assert_("ALTER TABLE foo.t1 DROP CONSTRAINT foo_bar_bat") + def test_drop_constraint_if_exists(self): + context = op_fixture() + op.drop_constraint("foo_bar_bat", "t1", if_exists=True) + context.assert_("ALTER TABLE t1 DROP CONSTRAINT IF EXISTS foo_bar_bat") + def test_create_index(self): context = op_fixture() op.create_index("ik_test", "t1", ["foo", "bar"])