From 69502725db4829a84872697fd6569631d2a3c47f Mon Sep 17 00:00:00 2001 From: RamonWill Date: Thu, 20 Aug 2020 15:05:39 -0400 Subject: Reflect mssql/postgresql filtered/partial indexes Added support for inspection / reflection of partial indexes / filtered indexes, i.e. those which use the ``mssql_where`` or ``postgresql_where`` parameters, with :class:`_schema.Index`. The entry is both part of the dictionary returned by :meth:`.Inspector.get_indexes` as well as part of a reflected :class:`_schema.Index` construct that was reflected. Pull request courtesy Ramon Williams. **Have a nice day!** Fixes: #4966 Closes: #5504 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/5504 Pull-request-sha: b3018bac987081193b2e65cfdb6aeb7d5d270fcd Change-Id: Icbb2f93d1545700718ccb5222097185b815f5dbc --- test/dialect/postgresql/test_reflection.py | 49 ++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) (limited to 'test/dialect/postgresql/test_reflection.py') diff --git a/test/dialect/postgresql/test_reflection.py b/test/dialect/postgresql/test_reflection.py index ec9328c2f..2a214c766 100644 --- a/test/dialect/postgresql/test_reflection.py +++ b/test/dialect/postgresql/test_reflection.py @@ -25,7 +25,9 @@ from sqlalchemy.dialects.postgresql import ExcludeConstraint from sqlalchemy.dialects.postgresql import INTEGER from sqlalchemy.dialects.postgresql import INTERVAL from sqlalchemy.dialects.postgresql import TSRANGE +from sqlalchemy.schema import CreateIndex from sqlalchemy.sql.schema import CheckConstraint +from sqlalchemy.testing import AssertsCompiledSQL from sqlalchemy.testing import fixtures from sqlalchemy.testing import mock from sqlalchemy.testing.assertions import assert_raises @@ -419,7 +421,7 @@ class DomainReflectionTest(fixtures.TestBase, AssertsExecutionResults): base.PGDialect.ischema_names = ischema_names -class ReflectionTest(fixtures.TestBase): +class ReflectionTest(AssertsCompiledSQL, fixtures.TestBase): __only_on__ = "postgresql" __backend__ = True @@ -880,7 +882,7 @@ class ReflectionTest(fixtures.TestBase): @testing.provide_metadata def test_index_reflection(self): - """ Reflecting partial & expression-based indexes should warn + """ Reflecting expression-based indexes should warn """ metadata = self.metadata @@ -926,12 +928,53 @@ class ReflectionTest(fixtures.TestBase): [ "Skipped unsupported reflection of " "expression-based index idx1", - "Predicate of partial index idx2 ignored during " "reflection", "Skipped unsupported reflection of " "expression-based index idx3", ], ) + @testing.provide_metadata + def test_index_reflection_partial(self, connection): + """Reflect the filter defintion on partial indexes + """ + + metadata = self.metadata + + t1 = Table( + "table1", + metadata, + Column("id", Integer, primary_key=True), + Column("name", String(20)), + Column("x", Integer), + ) + Index("idx1", t1.c.id, postgresql_where=t1.c.name == "test") + Index("idx2", t1.c.id, postgresql_where=t1.c.x >= 5) + + metadata.create_all(connection) + + ind = testing.db.dialect.get_indexes(connection, t1, None) + + partial_definitions = [] + for ix in ind: + if "dialect_options" in ix: + partial_definitions.append( + ix["dialect_options"]["postgresql_where"] + ) + + eq_( + sorted(partial_definitions), + ["((name)::text = 'test'::text)", "(x >= 5)"], + ) + + t2 = Table("table1", MetaData(), autoload_with=connection) + idx = list(sorted(t2.indexes, key=lambda idx: idx.name))[0] + + self.assert_compile( + CreateIndex(idx), + "CREATE INDEX idx1 ON table1 (id) " + "WHERE ((name)::text = 'test'::text)", + ) + @testing.fails_if("postgresql < 8.3", "index ordering not supported") @testing.provide_metadata def test_index_reflection_with_sorting(self): -- cgit v1.2.1