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
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
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)
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
--- /dev/null
+.. 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.
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)
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)
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)
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)