forked from cloudspannerecosystem/spanner-truncate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable_schema.go
101 lines (88 loc) · 3.08 KB
/
table_schema.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
//
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package main
import (
"cloud.google.com/go/spanner"
"context"
)
// deleteActionType is action type on parent delete.
type deleteActionType int
const (
deleteActionUndefined deleteActionType = iota // Undefined action type on parent delete.
deleteActionCascadeDelete // Cascade delete type on parent delete.
deleteActionNoAction // No action type on parent delete.
)
// tableSchema represents table metadata and relationships.
type tableSchema struct {
tableName string
// Parent / Child relationship.
parentTableName string
parentOnDeleteAction deleteActionType
// Foreign Key Reference.
referencedBy []string
}
func fetchTableSchemas(ctx context.Context, client *spanner.Client) ([]*tableSchema, error) {
// This query fetches the table metadata and relationships.
iter := client.Single().Query(ctx, spanner.NewStatement(`
WITH FKReferences AS (
SELECT CCU.TABLE_NAME AS Referenced, ARRAY_AGG(TC.TABLE_NAME) AS Referencing
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS as TC
INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS CCU ON TC.CONSTRAINT_NAME = CCU.CONSTRAINT_NAME
WHERE TC.TABLE_CATALOG = '' AND TC.TABLE_SCHEMA = '' AND TC.CONSTRAINT_TYPE = 'FOREIGN KEY' AND CCU.TABLE_CATALOG = '' AND CCU.TABLE_SCHEMA = ''
GROUP BY CCU.TABLE_NAME
)
SELECT T.TABLE_NAME, T.PARENT_TABLE_NAME, T.ON_DELETE_ACTION, IF(F.Referencing IS NULL, ARRAY<STRING>[], F.Referencing) AS referencedBy
FROM INFORMATION_SCHEMA.TABLES AS T
LEFT OUTER JOIN FKReferences AS F ON T.TABLE_NAME = F.Referenced
WHERE T.TABLE_CATALOG = "" AND T.TABLE_SCHEMA = ""
ORDER BY T.TABLE_NAME ASC
`))
var tables []*tableSchema
if err := iter.Do(func(r *spanner.Row) error {
var (
tableName string
parent spanner.NullString
deleteAction spanner.NullString
referencedBy []string
)
if err := r.Columns(&tableName, &parent, &deleteAction, &referencedBy); err != nil {
return err
}
var parentTableName string
if parent.Valid {
parentTableName = parent.StringVal
}
var typ deleteActionType
if deleteAction.Valid {
switch deleteAction.StringVal {
case "CASCADE":
typ = deleteActionCascadeDelete
case "NO ACTION":
typ = deleteActionNoAction
}
}
tables = append(tables, &tableSchema{
tableName: tableName,
parentTableName: parentTableName,
parentOnDeleteAction: typ,
referencedBy: referencedBy,
})
return nil
}); err != nil {
return nil, err
}
return tables, nil
}