From 1c3c0d388d36fa60b9aaa013873a415488ed7796 Mon Sep 17 00:00:00 2001 From: Aaron Griffin Date: Thu, 24 Apr 2025 17:30:54 -0500 Subject: [PATCH] handle DropConstraint.if_exists version requirements --- alembic/operations/toimpl.py | 4 +++- tests/test_op.py | 10 ++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/alembic/operations/toimpl.py b/alembic/operations/toimpl.py index c8190965..96704d58 100644 --- a/alembic/operations/toimpl.py +++ b/alembic/operations/toimpl.py @@ -7,7 +7,7 @@ from sqlalchemy import schema as sa_schema from . import ops from .base import Operations -from ..util.sqla_compat import _copy +from ..util.sqla_compat import _copy, sqla_2 if TYPE_CHECKING: from sqlalchemy.sql.schema import Table @@ -199,6 +199,8 @@ def drop_constraint( ) -> None: kw = {} if operation.if_exists is not None: + if not sqla_2: + raise NotImplementedError("SQLAlchemy 2.0 required") kw["if_exists"] = operation.if_exists operations.impl.drop_constraint( operations.schema_obj.generic_constraint( diff --git a/tests/test_op.py b/tests/test_op.py index 4522e44c..39960318 100644 --- a/tests/test_op.py +++ b/tests/test_op.py @@ -823,8 +823,14 @@ class OpTest(TestBase): 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") + if sqla_compat.sqla_2: + op.drop_constraint("foo_bar_bat", "t1", if_exists=True) + context.assert_("ALTER TABLE t1 DROP CONSTRAINT IF EXISTS foo_bar_bat") + else: + with expect_raises_message( + NotImplementedError, "SQLAlchemy 2.0 required" + ): + op.drop_constraint("foo_bar_bat", "t1", if_exists=True) def test_create_index(self): context = op_fixture() -- 2.47.2