]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Open up `if_(not_)?exists` to SQLAlchemy 1.4+
authorl-hedgehog <l-hedgehog@outlook.com>
Thu, 16 Nov 2023 14:23:47 +0000 (09:23 -0500)
committerFederico Caselli <cfederico87@gmail.com>
Thu, 16 Nov 2023 19:35:59 +0000 (20:35 +0100)
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: #<issue number>` 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: #<issue number>` 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

alembic/operations/toimpl.py
docs/build/unreleased/1323.rst [new file with mode: 0644]
tests/test_op.py
tests/test_postgresql.py

index ba974b6228c35a66fd4b1c103418f21b55f2e409..ff77ab75e98a585918fa36661c71a7238737275b 100644 (file)
@@ -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 (file)
index 0000000..0acb202
--- /dev/null
@@ -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.
index 2b924d288981941ef5359eaa79df1718deb71085..688799c974faaa68e3c63cc19e881f54e8768da1 100644 (file)
@@ -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)
index 5c5e4f3a4f7a9d269df6bf5c71f5c941f94c7f4f..6e7dba91a1c2bb568028a3e813a2ab4a71f144c5 100644 (file)
@@ -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)