diff options
author | mike bayer <mike_mp@zzzcomputing.com> | 2021-08-23 18:19:16 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@ci3.zzzcomputing.com> | 2021-08-23 18:19:16 +0000 |
commit | 18136dd42a0820fbecacea8cb0e7f47b002ce68a (patch) | |
tree | 04e969a5fdb2912c8c7cdf43571ac2a8b5206248 | |
parent | 1977047a11124221461ad8daee502bc4c74d5f6c (diff) | |
parent | 84bbbebe41042eacb2b0e15d9a0959ee60fbcc67 (diff) | |
download | alembic-18136dd42a0820fbecacea8cb0e7f47b002ce68a.tar.gz |
Merge "Fix postgresql_include in create_index"
-rw-r--r-- | alembic/ddl/postgresql.py | 11 | ||||
-rw-r--r-- | docs/build/unreleased/874.rst | 9 | ||||
-rw-r--r-- | tests/test_postgresql.py | 13 |
3 files changed, 33 insertions, 0 deletions
diff --git a/alembic/ddl/postgresql.py b/alembic/ddl/postgresql.py index c894649..9fb9ac9 100644 --- a/alembic/ddl/postgresql.py +++ b/alembic/ddl/postgresql.py @@ -16,6 +16,7 @@ from sqlalchemy import types as sqltypes from sqlalchemy.dialects.postgresql import BIGINT from sqlalchemy.dialects.postgresql import ExcludeConstraint from sqlalchemy.dialects.postgresql import INTEGER +from sqlalchemy.schema import CreateIndex from sqlalchemy.sql.elements import ColumnClause from sqlalchemy.sql.elements import UnaryExpression from sqlalchemy.types import NULLTYPE @@ -71,6 +72,16 @@ class PostgresqlImpl(DefaultImpl): ) identity_attrs_ignore = ("on_null", "order") + def create_index(self, index): + # this likely defaults to None if not present, so get() + # should normally not return the default value. being + # defensive in any case + postgresql_include = index.kwargs.get("postgresql_include", None) or () + for col in postgresql_include: + if col not in index.table.c: + index.table.append_column(Column(col, sqltypes.NullType)) + self._exec(CreateIndex(index)) + def prep_table_for_batch(self, batch_impl, table): for constraint in table.constraints: diff --git a/docs/build/unreleased/874.rst b/docs/build/unreleased/874.rst new file mode 100644 index 0000000..36a73ef --- /dev/null +++ b/docs/build/unreleased/874.rst @@ -0,0 +1,9 @@ +.. change:: + :tags: bug, postgresql + :tickets: 874 + + Fixed issue where usage of the PostgreSQL ``postgresql_include`` option + within a :meth:`.Operations.create_index` would raise a KeyError, as the + additional column(s) need to be added to the table object used by the + construct internally. The issue is equivalent to the SQL Server issue fixed + in :ticket:`513`. Pull request courtesy Steven Bronson. diff --git a/tests/test_postgresql.py b/tests/test_postgresql.py index 500678a..b41c383 100644 --- a/tests/test_postgresql.py +++ b/tests/test_postgresql.py @@ -103,6 +103,19 @@ class PostgresqlOpTest(TestBase): "CREATE INDEX CONCURRENTLY geocoded ON locations (coordinates)" ) + @config.requirements.sqlalchemy_14 + def test_create_index_postgresql_include(self): + context = op_fixture("postgresql") + op.create_index( + "i", "t", ["c1", "c2"], unique=False, postgresql_include=["inc"] + ) + context.assert_("CREATE INDEX i ON t (c1, c2) INCLUDE (inc)") + + def test_create_index_postgresql_include_is_none(self): + context = op_fixture("postgresql") + op.create_index("i", "t", ["c1", "c2"], unique=False) + context.assert_("CREATE INDEX i ON t (c1, c2)") + def test_drop_index_postgresql_concurrently(self): context = op_fixture("postgresql") op.drop_index("geocoded", "locations", postgresql_concurrently=True) |