-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdescribe_indexes.go
116 lines (104 loc) · 3.15 KB
/
describe_indexes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package pgutil
import (
"context"
"fmt"
)
type IndexDescription struct {
Name string
IsPrimaryKey bool
IsUnique bool
IsExclusion bool
IsDeferrable bool
IndexDefinition string
ConstraintType string
ConstraintDefinition string
}
func (d IndexDescription) Equals(other IndexDescription) bool {
return true &&
d.Name == other.Name &&
d.IsPrimaryKey == other.IsPrimaryKey &&
d.IsUnique == other.IsUnique &&
d.IsExclusion == other.IsExclusion &&
d.IsDeferrable == other.IsDeferrable &&
d.IndexDefinition == other.IndexDefinition &&
d.ConstraintType == other.ConstraintType &&
d.ConstraintDefinition == other.ConstraintDefinition
}
type index struct {
Namespace string
TableName string
Name string
IsPrimaryKey bool
IsUnique bool
IsExclusion *bool
IsDeferrable *bool
IndexDefinition string
ConstraintType *string
ConstraintDefinition *string
}
var scanIndexes = NewSliceScanner(func(s Scanner) (i index, _ error) {
var (
isPrimaryKey string
isUnique string
)
err := s.Scan(
&i.Namespace,
&i.TableName,
&i.Name,
&isPrimaryKey,
&isUnique,
&i.IsExclusion,
&i.IsDeferrable,
&i.IndexDefinition,
&i.ConstraintType,
&i.ConstraintDefinition,
)
i.IsPrimaryKey = truthy(isPrimaryKey)
i.IsUnique = truthy(isUnique)
return i, err
})
func describeIndexes(ctx context.Context, db DB) (map[string][]IndexDescription, error) {
indexes, err := scanIndexes(db.Query(ctx, RawQuery(`
SELECT
n.nspname AS namespace,
table_class.relname AS table_name,
index_class.relname AS name,
i.indisprimary AS is_primary_key,
i.indisunique AS is_unique,
i.indisexclusion AS is_exclusion,
con.condeferrable AS is_deferrable,
pg_catalog.pg_get_indexdef(i.indexrelid, 0, true) AS index_definition,
con.contype AS constraint_type,
pg_catalog.pg_get_constraintdef(con.oid, true) AS constraint_definition
FROM pg_catalog.pg_index i
JOIN pg_catalog.pg_class table_class ON table_class.oid = i.indrelid
JOIN pg_catalog.pg_class index_class ON index_class.oid = i.indexrelid
JOIN pg_catalog.pg_namespace n ON n.oid = table_class.relnamespace
LEFT OUTER JOIN pg_catalog.pg_constraint con ON
con.conrelid = i.indrelid AND
con.conindid = i.indexrelid AND
con.contype IN ('p', 'u', 'x')
WHERE
n.nspname NOT LIKE 'pg_%' AND
n.nspname != 'information_schema'
ORDER BY n.nspname, table_class.relname, index_class.relname
`)))
if err != nil {
return nil, err
}
indexMap := map[string][]IndexDescription{}
for _, index := range indexes {
key := fmt.Sprintf("%q.%q", index.Namespace, index.TableName)
indexMap[key] = append(indexMap[key], IndexDescription{
Name: index.Name,
IsPrimaryKey: index.IsPrimaryKey,
IsUnique: index.IsUnique,
IsExclusion: deref(index.IsExclusion),
IsDeferrable: deref(index.IsDeferrable),
IndexDefinition: index.IndexDefinition,
ConstraintType: deref(index.ConstraintType),
ConstraintDefinition: deref(index.ConstraintDefinition),
})
}
return indexMap, nil
}