diff options
author | Pete Hollobon <phollobon@renshawbay.com> | 2015-06-04 16:45:41 +0100 |
---|---|---|
committer | Pete Hollobon <phollobon@renshawbay.com> | 2015-06-04 16:45:41 +0100 |
commit | af19435b9c1cec28d0ac93aa832b45601af51597 (patch) | |
tree | 06fbb33f1b5eed5dee47be44aff0f950cbffac53 | |
parent | b03ee45f32e53416ce1c73fa15966db17ed4bbf9 (diff) | |
download | sqlalchemy-pr/179.tar.gz |
Add reflection of PostgreSQL index access methods (USING clause)pr/179
-rw-r--r-- | lib/sqlalchemy/dialects/postgresql/base.py | 16 | ||||
-rw-r--r-- | test/dialect/postgresql/test_reflection.py | 19 |
2 files changed, 32 insertions, 3 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index b9690e214..fa11956ad 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -2625,7 +2625,7 @@ class PGDialect(default.DefaultDialect): i.relname as relname, ix.indisunique, ix.indexprs, ix.indpred, a.attname, a.attnum, NULL, ix.indkey%s, - i.reloptions + i.reloptions, am.amname FROM pg_class t join pg_index ix on t.oid = ix.indrelid @@ -2633,6 +2633,9 @@ class PGDialect(default.DefaultDialect): left outer join pg_attribute a on t.oid = a.attrelid and %s + left outer join + pg_am am + on i.relam = am.oid WHERE t.relkind IN ('r', 'v', 'f', 'm') and t.oid = :table_oid @@ -2653,7 +2656,7 @@ class PGDialect(default.DefaultDialect): i.relname as relname, ix.indisunique, ix.indexprs, ix.indpred, a.attname, a.attnum, c.conrelid, ix.indkey::varchar, - i.reloptions + i.reloptions, am.amname FROM pg_class t join pg_index ix on t.oid = ix.indrelid @@ -2666,6 +2669,9 @@ class PGDialect(default.DefaultDialect): on (ix.indrelid = c.conrelid and ix.indexrelid = c.conindid and c.contype in ('p', 'u', 'x')) + left outer join + pg_am am + on i.relam = am.oid WHERE t.relkind IN ('r', 'v', 'f', 'm') and t.oid = :table_oid @@ -2682,7 +2688,7 @@ class PGDialect(default.DefaultDialect): sv_idx_name = None for row in c.fetchall(): - idx_name, unique, expr, prd, col, col_num, conrelid, idx_key, options = row + idx_name, unique, expr, prd, col, col_num, conrelid, idx_key, options, amname = row if expr: if idx_name != sv_idx_name: @@ -2710,6 +2716,8 @@ class PGDialect(default.DefaultDialect): index['duplicates_constraint'] = idx_name if options: index['options'] = dict([option.split("=") for option in options]) + if amname and amname != 'btree': + index['amname'] = amname result = [] for name, idx in indexes.items(): @@ -2722,6 +2730,8 @@ class PGDialect(default.DefaultDialect): entry['duplicates_constraint'] = idx['duplicates_constraint'] if 'options' in idx: entry.setdefault('dialect_options', {})["postgresql_with"] = idx['options'] + if 'amname' in idx: + entry.setdefault('dialect_options', {})["postgresql_using"] = idx['amname'] result.append(entry) return result diff --git a/test/dialect/postgresql/test_reflection.py b/test/dialect/postgresql/test_reflection.py index 70b81219e..2321a3054 100644 --- a/test/dialect/postgresql/test_reflection.py +++ b/test/dialect/postgresql/test_reflection.py @@ -12,6 +12,7 @@ from sqlalchemy import Table, Column, MetaData, Integer, String, \ from sqlalchemy import exc import sqlalchemy as sa from sqlalchemy.dialects.postgresql import base as postgresql +from sqlalchemy.dialects.postgresql import ARRAY class ForeignTableReflectionTest(fixtures.TablesTest, AssertsExecutionResults): @@ -691,6 +692,24 @@ class ReflectionTest(fixtures.TestBase): 'dialect_options': {"postgresql_with": {"fillfactor": "50"}}}]) conn.close() + @testing.provide_metadata + def test_index_reflection_with_access_method(self): + """reflect indexes with storage options set""" + + metadata = self.metadata + + t1 = Table('t', metadata, + Column('id', Integer, primary_key=True), + Column('x', ARRAY(Integer)) + ) + metadata.create_all() + conn = testing.db.connect().execution_options(autocommit=True) + conn.execute("CREATE INDEX idx1 ON t USING gin (x)") + + ind = testing.db.dialect.get_indexes(conn, "t", None) + eq_(ind, [{'unique': False, 'column_names': ['x'], 'name': 'idx1', + 'dialect_options': {'postgresql_using': 'gin'}}]) + conn.close() @testing.provide_metadata def test_foreign_key_option_inspection(self): |