diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-07-19 10:40:27 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-07-19 13:18:39 -0400 |
| commit | a739a3449ff20ba90f92305ed4bbdd36e05bc862 (patch) | |
| tree | f2e5a7d73930e4600f78db1c36c4a05f98e3d0be | |
| parent | 3bc3ea395eb16f548a11849bc15f0f78b7b2400f (diff) | |
| download | sqlalchemy-a739a3449ff20ba90f92305ed4bbdd36e05bc862.tar.gz | |
Include 'p' for get_indexes() query
Added support for reflection of indexes on PostgreSQL partitioned tables,
which was added to PostgreSQL as of version 11.
Fixes: #4771
Change-Id: I9e8e75c4d8a667b4d52d12afbd384e0a8db00466
| -rw-r--r-- | doc/build/changelog/unreleased_13/4771.rst | 6 | ||||
| -rw-r--r-- | lib/sqlalchemy/dialects/postgresql/base.py | 2 | ||||
| -rw-r--r-- | test/dialect/postgresql/test_reflection.py | 22 |
3 files changed, 27 insertions, 3 deletions
diff --git a/doc/build/changelog/unreleased_13/4771.rst b/doc/build/changelog/unreleased_13/4771.rst new file mode 100644 index 000000000..e40797def --- /dev/null +++ b/doc/build/changelog/unreleased_13/4771.rst @@ -0,0 +1,6 @@ +.. change:: + :tags: usecase, postgresql + :tickets: 4771 + + Added support for reflection of indexes on PostgreSQL partitioned tables, + which was added to PostgreSQL as of version 11. diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index 5aea02cfc..8fbd97ebe 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -3255,7 +3255,7 @@ class PGDialect(default.DefaultDialect): pg_am am on i.relam = am.oid WHERE - t.relkind IN ('r', 'v', 'f', 'm') + t.relkind IN ('r', 'v', 'f', 'm', 'p') and t.oid = :table_oid and ix.indisprimary = 'f' ORDER BY diff --git a/test/dialect/postgresql/test_reflection.py b/test/dialect/postgresql/test_reflection.py index ea72d5710..6e7d7202a 100644 --- a/test/dialect/postgresql/test_reflection.py +++ b/test/dialect/postgresql/test_reflection.py @@ -111,6 +111,7 @@ class PartitionedReflectionTest(fixtures.TablesTest, AssertsExecutionResults): metadata, Column("modulus", Integer, nullable=False), Column("data", String(30)), + Column("q", Integer), postgresql_partition_by="range(modulus)", ) @@ -124,6 +125,9 @@ class PartitionedReflectionTest(fixtures.TablesTest, AssertsExecutionResults): ), ) + if testing.against("postgresql >= 11"): + Index("my_index", dv.c.q) + def test_get_tablenames(self): assert {"data_values", "data_values_4_10"}.issubset( inspect(testing.db).get_table_names() @@ -131,11 +135,25 @@ class PartitionedReflectionTest(fixtures.TablesTest, AssertsExecutionResults): def test_reflect_cols(self): cols = inspect(testing.db).get_columns("data_values") - eq_([c["name"] for c in cols], ["modulus", "data"]) + eq_([c["name"] for c in cols], ["modulus", "data", "q"]) def test_reflect_cols_from_partition(self): cols = inspect(testing.db).get_columns("data_values_4_10") - eq_([c["name"] for c in cols], ["modulus", "data"]) + eq_([c["name"] for c in cols], ["modulus", "data", "q"]) + + @testing.only_on("postgresql >= 11") + def test_reflect_index(self): + idx = inspect(testing.db).get_indexes("data_values") + eq_( + idx, [{"column_names": ["q"], "name": "my_index", "unique": False}] + ) + + @testing.only_on("postgresql >= 11") + def test_reflect_index_from_partition(self): + idx = inspect(testing.db).get_indexes("data_values_4_10") + # note the name appears to be generated by PG, currently + # 'data_values_4_10_q_idx' + eq_(idx, [{"column_names": ["q"], "name": mock.ANY, "unique": False}]) class MaterializedViewReflectionTest( |
