14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/dm-is-reflective/adapters/postgres_adapter.rb', line 14
def indices storage
sql = <<-SQL
SELECT a.attname, i.relname, ix.indisprimary, ix.indisunique
FROM pg_class t, pg_class i, pg_index ix, pg_attribute a
WHERE t.oid = ix.indrelid
AND i.oid = ix.indexrelid
AND a.attrelid = t.oid
AND a.attnum = ANY(ix.indkey)
AND t.relkind = 'r'
AND t.relname = ?
SQL
select(Ext::String.compress_lines(sql), storage).group_by(&:attname).
inject({}) do |r, (column, idxs)|
key = !!idxs.find(&:indisprimary)
idx_uni, idx_com = idxs.partition(&:indisunique).map{ |i|
if i.empty?
nil
elsif i.size == 1
i.first.relname.to_sym
else
i.map{ |ii| ii.relname.to_sym }
end
}
r[column.to_sym] = reflective_indices_hash(key, idx_uni, idx_com)
r
end
end
|