From: Aaron Griffin Date: Thu, 24 Apr 2025 22:30:54 +0000 (-0500) Subject: handle DropConstraint.if_exists version requirements X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F1651%2Fhead;p=thirdparty%2Fsqlalchemy%2Falembic.git handle DropConstraint.if_exists version requirements --- 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()