-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdescribe_enums.go
40 lines (34 loc) · 964 Bytes
/
describe_enums.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
package pgutil
import (
"context"
"github.com/lib/pq"
"golang.org/x/exp/slices"
)
type EnumDescription struct {
Namespace string
Name string
Labels []string
}
func (d EnumDescription) Equals(other EnumDescription) bool {
return true &&
d.Namespace == other.Namespace &&
d.Name == other.Name &&
slices.Equal(d.Labels, other.Labels)
}
var scanEnums = NewSliceScanner(func(s Scanner) (l EnumDescription, _ error) {
err := s.Scan(&l.Namespace, &l.Name, pq.Array(&l.Labels))
return l, err
})
func DescribeEnums(ctx context.Context, db DB) ([]EnumDescription, error) {
return scanEnums(db.Query(ctx, RawQuery(`
SELECT
n.nspname AS namespace,
t.typname AS name,
array_agg(e.enumlabel ORDER BY e.enumsortorder) AS labels
FROM pg_catalog.pg_enum e
JOIN pg_catalog.pg_type t ON t.oid = e.enumtypid
JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
GROUP BY n.nspname, t.typname
ORDER BY n.nspname, t.typname
`)))
}