use old index retrieval code

This reverts changes to the index introspection done in
https://github.com/bernardopires/django-tenant-schemas/pull/385
This commit is contained in:
Frédéric Péters 2018-01-10 20:05:27 +01:00
parent d05d6603ff
commit 7abe3cfb0b
1 changed files with 20 additions and 34 deletions

View File

@ -135,35 +135,24 @@ class DatabaseSchemaIntrospection(BaseDatabaseIntrospection):
_get_index_constraints_query = """
SELECT
indexname, array_agg(attname), indisunique, indisprimary,
array_agg(ordering), amname, exprdef
FROM (
SELECT
c2.relname as indexname, idx.*, attr.attname, am.amname,
CASE
WHEN idx.indexprs IS NOT NULL THEN
pg_get_indexdef(idx.indexrelid)
END AS exprdef,
CASE am.amname
WHEN 'btree' THEN
CASE (option & 1)
WHEN 1 THEN 'DESC' ELSE 'ASC'
END
END as ordering
FROM (
SELECT
*, unnest(i.indkey) as key, unnest(i.indoption) as option
FROM pg_index i
) idx
LEFT JOIN pg_class c ON idx.indrelid = c.oid
LEFT JOIN pg_class c2 ON idx.indexrelid = c2.oid
LEFT JOIN pg_am am ON c2.relam = am.oid
LEFT JOIN pg_attribute attr ON attr.attrelid = c.oid AND attr.attnum = idx.key
LEFT JOIN pg_namespace n ON c.relnamespace = n.oid
WHERE c.relname = %(table)s
AND n.nspname = %(schema)s
) s2
GROUP BY indexname, indisunique, indisprimary, amname, exprdef;
c2.relname,
ARRAY(
SELECT (
SELECT attname
FROM pg_catalog.pg_attribute
WHERE attnum = i AND attrelid = c.oid
)
FROM unnest(idx.indkey) i
),
idx.indisunique,
idx.indisprimary
FROM pg_catalog.pg_class c, pg_catalog.pg_class c2,
pg_catalog.pg_index idx, pg_catalog.pg_namespace n
WHERE c.oid = idx.indrelid
AND idx.indexrelid = c2.oid
AND n.oid = c.relnamespace
AND c.relname = %(table)s
AND n.nspname = %(schema)s
"""
def get_field_type(self, data_type, description):
@ -291,17 +280,14 @@ class DatabaseSchemaIntrospection(BaseDatabaseIntrospection):
'table': table_name,
})
for index, columns, unique, primary, orders, type_, definition in cursor.fetchall():
for index, columns, unique, primary in cursor.fetchall():
if index not in constraints:
constraints[index] = {
"columns": columns if columns != [None] else [],
"orders": orders if orders != [None] else [],
"columns": list(columns),
"primary_key": primary,
"unique": unique,
"foreign_key": None,
"check": False,
"index": True,
"type": type_,
"definition": definition,
}
return constraints