-
Notifications
You must be signed in to change notification settings - Fork 0
/
describe_dependencies.go
91 lines (84 loc) · 2.72 KB
/
describe_dependencies.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
package pgutil
import "context"
type EnumDependency struct {
EnumNamespace string
EnumName string
TableNamespace string
TableName string
ColumnName string
}
var scanEnumDependencies = NewSliceScanner(func(s Scanner) (d EnumDependency, _ error) {
err := s.Scan(
&d.EnumNamespace,
&d.EnumName,
&d.TableNamespace,
&d.TableName,
&d.ColumnName,
)
return d, err
})
func DescribeEnumDependencies(ctx context.Context, db DB) ([]EnumDependency, error) {
return scanEnumDependencies(db.Query(ctx, RawQuery(`
SELECT
ns.nspname AS enum_namespace,
col.udt_name AS enum_name,
col.table_schema AS table_namespace,
col.table_name AS table_name,
col.column_name AS column_name
FROM information_schema.columns col
JOIN information_schema.tables tab
ON
tab.table_schema = col.table_schema AND
tab.table_name = col.table_name AND
tab.table_type = 'BASE TABLE'
JOIN pg_type typ ON col.udt_name = typ.typname
JOIN pg_namespace ns ON ns.oid = typ.typnamespace
WHERE
col.table_schema NOT LIKE 'pg_%' AND
col.table_schema != 'information_schema' AND
typ.typtype = 'e'
ORDER BY col.table_schema, col.table_name, col.ordinal_position
`)))
}
type ColumnDependency struct {
SourceNamespace string
SourceTableOrViewName string
SourceColumnName string
UsedNamespace string
UsedTableOrView string
}
var scanColumnDependencies = NewSliceScanner(func(s Scanner) (d ColumnDependency, _ error) {
err := s.Scan(
&d.SourceNamespace,
&d.SourceTableOrViewName,
&d.SourceColumnName,
&d.UsedNamespace,
&d.UsedTableOrView,
)
return d, err
})
func DescribeColumnDependencies(ctx context.Context, db DB) ([]ColumnDependency, error) {
return scanColumnDependencies(db.Query(ctx, RawQuery(`
SELECT
source_ns.nspname AS source_namespace,
source_table.relname AS source_table_or_view_name,
pg_attribute.attname AS source_column_name,
dependent_ns.nspname AS used_namespace,
dependent_view.relname AS used_table_or_view_name
FROM pg_depend
JOIN pg_rewrite ON pg_depend.objid = pg_rewrite.oid
JOIN pg_class AS dependent_view ON pg_rewrite.ev_class = dependent_view.oid
JOIN pg_class AS source_table ON pg_depend.refobjid = source_table.oid
JOIN pg_attribute ON
pg_depend.refobjid = pg_attribute.attrelid AND
pg_depend.refobjsubid = pg_attribute.attnum
JOIN pg_namespace dependent_ns ON dependent_ns.oid = dependent_view.relnamespace
JOIN pg_namespace source_ns ON source_ns.oid = source_table.relnamespace
WHERE
dependent_ns.nspname NOT LIKE 'pg_%' AND
dependent_ns.nspname != 'information_schema' AND
source_ns.nspname NOT LIKE 'pg_%' AND
source_ns.nspname != 'information_schemea'
ORDER BY dependent_ns.nspname, dependent_view.relname
`)))
}