From: l-hedgehog Date: Thu, 16 Nov 2023 14:23:47 +0000 (-0500) Subject: Open up `if_(not_)?exists` to SQLAlchemy 1.4+ X-Git-Tag: rel_1_13_0~10^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=62a3bbcd8a893515eecc637b7095c8326c7bcf74;p=thirdparty%2Fsqlalchemy%2Falembic.git Open up `if_(not_)?exists` to SQLAlchemy 1.4+ Fixes #1323 ### Description As the title describes, open up `if_(not_)?exists` support (both implementations and tests) to SQLAlchemy 1.4+ ### Checklist This pull request is: - [ ] A documentation / typographical error fix - Good to go, no issue or tests are needed - [x] A short code fix - please include the issue number, and create an issue if none exists, which must include a complete example of the issue. one line code fixes without an issue and demonstration will not be accepted. - Please include: `Fixes: #` in the commit message - please include tests. one line code fixes without tests will not be accepted. - [ ] A new feature implementation - please include the issue number, and create an issue if none exists, which must include a complete example of how the feature would look. - Please include: `Fixes: #` in the commit message - please include tests. Closes: #1358 Pull-request: https://github.com/sqlalchemy/alembic/pull/1358 Pull-request-sha: c678a3168b790c1d97fd32ed5a3c9e1051a4d958 Change-Id: I02f4aac265a10ce601afce3c69ce99dba80490dd --- diff --git a/alembic/operations/toimpl.py b/alembic/operations/toimpl.py index ba974b62..ff77ab75 100644 --- a/alembic/operations/toimpl.py +++ b/alembic/operations/toimpl.py @@ -5,7 +5,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 sqla_2 +from ..util.sqla_compat import sqla_14 if TYPE_CHECKING: from sqlalchemy.sql.schema import Table @@ -98,8 +98,8 @@ def create_index( idx = operation.to_index(operations.migration_context) kw = {} if operation.if_not_exists is not None: - if not sqla_2: - raise NotImplementedError("SQLAlchemy 2.0+ required") + if not sqla_14: + raise NotImplementedError("SQLAlchemy 1.4+ required") kw["if_not_exists"] = operation.if_not_exists operations.impl.create_index(idx, **kw) @@ -109,8 +109,8 @@ def create_index( def drop_index(operations: "Operations", operation: "ops.DropIndexOp") -> None: kw = {} if operation.if_exists is not None: - if not sqla_2: - raise NotImplementedError("SQLAlchemy 2.0+ required") + if not sqla_14: + raise NotImplementedError("SQLAlchemy 1.4+ required") kw["if_exists"] = operation.if_exists diff --git a/docs/build/unreleased/1323.rst b/docs/build/unreleased/1323.rst new file mode 100644 index 00000000..0acb202c --- /dev/null +++ b/docs/build/unreleased/1323.rst @@ -0,0 +1,8 @@ +.. change:: + :tags: usecase, operations + :tickets: 1323 + + Updated logic introduced in :ticket:`151` to allow ``if_exists`` and + ``if_not_exists`` on index operations also on SQLAlchemy + 1.4 series. Previously this feature was mistakenly requiring + the 2.0 series. diff --git a/tests/test_op.py b/tests/test_op.py index 2b924d28..688799c9 100644 --- a/tests/test_op.py +++ b/tests/test_op.py @@ -829,7 +829,7 @@ class OpTest(TestBase): op.create_index("ik_test", "t1", ["foo", "bar"]) context.assert_("CREATE INDEX ik_test ON t1 (foo, bar)") - @config.requirements.sqlalchemy_2 + @config.requirements.sqlalchemy_14 def test_create_index_if_not_exists(self): context = op_fixture() op.create_index("ik_test", "t1", ["foo", "bar"], if_not_exists=True) @@ -891,7 +891,7 @@ class OpTest(TestBase): op.drop_index("ik_test", schema="foo") context.assert_("DROP INDEX foo.ik_test") - @config.requirements.sqlalchemy_2 + @config.requirements.sqlalchemy_14 def test_drop_index_if_exists(self): context = op_fixture() op.drop_index("ik_test", if_exists=True) diff --git a/tests/test_postgresql.py b/tests/test_postgresql.py index 5c5e4f3a..6e7dba91 100644 --- a/tests/test_postgresql.py +++ b/tests/test_postgresql.py @@ -122,7 +122,7 @@ class PostgresqlOpTest(TestBase): op.create_index("i", "t", ["c1", "c2"], unique=False) context.assert_("CREATE INDEX i ON t (c1, c2)") - @config.requirements.sqlalchemy_2 + @config.requirements.sqlalchemy_14 def test_create_index_postgresql_if_not_exists(self): context = op_fixture("postgresql") op.create_index("i", "t", ["c1", "c2"], if_not_exists=True) @@ -141,7 +141,7 @@ class PostgresqlOpTest(TestBase): op.drop_index("geocoded", postgresql_concurrently=True) context.assert_("DROP INDEX CONCURRENTLY geocoded") - @config.requirements.sqlalchemy_2 + @config.requirements.sqlalchemy_14 def test_drop_index_postgresql_if_exists(self): context = op_fixture("postgresql") op.drop_index("geocoded", if_exists=True)