-
Notifications
You must be signed in to change notification settings - Fork 22
/
dependencies.go
394 lines (374 loc) · 13.9 KB
/
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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// Copyright 2023 The Cockroach Authors
//
// 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.
//
// SPDX-License-Identifier: Apache-2.0
package schemawatch
import (
"context"
"database/sql"
"fmt"
"github.com/cockroachdb/cdc-sink/internal/types"
"github.com/cockroachdb/cdc-sink/internal/util/ident"
"github.com/cockroachdb/cdc-sink/internal/util/retry"
"github.com/pkg/errors"
)
// depOrderTemplateMySQL computes the "referential depth" of tables based on
// foreign-key constraints.
//
// The query is structured as follows:
// - tables: Tables in the schema.
// - refs: Maps referring tables (child) to referenced tables
// (parent). Table self-references are excluded from this query.
// - roots: Tables with no FK references.
// - depths: Recursively computes the depth of each table from the root, walking
// the foreign keys constraints.
// - cycle_detect: ensure that all tables have a depth, by injecting a default depth.
// Finally, compute the maximum depth for each table in the given schema.
const depOrderTemplateMySQL = `
WITH RECURSIVE
tables
AS (
SELECT
table_catalog, table_schema, table_name
FROM
information_schema.tables
WHERE table_type = 'BASE TABLE'
),
refs
AS (
SELECT
constraint_catalog AS child_catalog,
constraint_schema AS child_schema,
table_name AS child_table_name,
unique_constraint_catalog AS parent_catalog,
unique_constraint_schema AS parent_schema,
referenced_table_name AS parent_table_name
FROM information_schema.referential_constraints
WHERE
(constraint_catalog, constraint_schema, table_name)
!= (unique_constraint_catalog, unique_constraint_schema,referenced_table_name)
),
roots
AS (
SELECT
tables.table_catalog, tables.table_schema, tables.table_name
FROM
tables
WHERE
(tables.table_catalog, tables.table_schema, tables.table_name)
NOT IN (SELECT child_catalog, child_schema, child_table_name FROM refs)
),
depths
AS (
SELECT table_catalog, table_schema, table_name, 0 AS depth FROM roots
UNION ALL
SELECT
refs.child_catalog,
refs.child_schema,
refs.child_table_name,
depths.depth + 1
FROM
depths, refs
WHERE
refs.parent_catalog = depths.table_catalog
AND refs.parent_schema = depths.table_schema
AND refs.parent_table_name = depths.table_name
),
cycle_detect
AS (
SELECT table_catalog, table_schema, table_name, -1 AS depth FROM tables
UNION ALL
SELECT table_catalog, table_schema, table_name, depth FROM depths
)
SELECT
table_name, max(depth) AS depth
FROM
cycle_detect
WHERE
table_schema = ?
GROUP BY
table_name
ORDER BY
depth, table_name`
// depOrderTemplateOra computes the "referential depth" of tables based on
// foreign-key constraints.
//
// The query is structured as follows:
// - parent_refs: Identifies all index References as a mapping of
// (owner, table) -> (owner, constraint).
// - ref_to_tbl: Resolves the constraint name references to Primary or
// Unique indexes to parent table names.
// - tbl_to_parent: Joins parent_refs and ref_to_tbl to produce
// child(owner, table) -> parent(owner, table) mappings.
// - root: Any table that is not referenced in tbl_to_parent. Contains
// extra columns to be column-compatible with tbl_to_parent.
// - combo: A union of root and tbl_to_parent. This serves as the
// source of data for the recursive query.
// - levels: A recursive query that computes the level by finding the
// child tables of the previous level. The START WITH clause selects
// tables which have no parent or which are self-referential.
// - cyclic: Adds a dummy level for every table to detect any cyclic
// structures which were excluded by levels.
// - The top-level query finds the maximum depth for each table. We
// subtract one from the magic LEVEL value to align with the PG query
// below.
const depOrderTemplateOra = `
WITH parent_refs AS (SELECT OWNER tbl_owner, TABLE_NAME tbl_name, R_OWNER parent_owner, R_CONSTRAINT_NAME ref_name
FROM ALL_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'R'),
ref_to_tbl AS (SELECT CONSTRAINT_NAME ref_name, OWNER parent_owner, TABLE_NAME parent_name
FROM ALL_CONSTRAINTS
WHERE CONSTRAINT_TYPE IN ('P', 'U')),
tbl_to_parent AS (SELECT tbl_owner, tbl_name, parent_owner, parent_name
FROM parent_refs
JOIN ref_to_tbl USING (parent_owner, ref_name)),
roots AS (SELECT OWNER tbl_owner, TABLE_NAME tbl_name, NULL parent_owner, NULL parent_name
FROM ALL_TABLES
WHERE (OWNER, TABLE_NAME) NOT IN (SELECT tbl_owner, tbl_name FROM tbl_to_parent)),
combo AS (SELECT * FROM roots UNION ALL SELECT * from tbl_to_parent),
levels AS (SELECT LEVEL lvl, tbl_owner, tbl_name, parent_owner, parent_name
FROM combo
START WITH (parent_owner IS NULL AND parent_name IS NULL)
OR (tbl_owner = parent_owner AND tbl_name = parent_name)
CONNECT BY NOCYCLE (parent_owner, parent_name) = ((PRIOR tbl_owner, PRIOR tbl_name))),
cyclic AS (SELECT 0 lvl, OWNER tbl_owner, TABLE_NAME tbl_name
FROM ALL_TABLES
UNION ALL
SELECT lvl, tbl_owner, tbl_name
FROM levels)
SELECT tbl_name, max(lvl) - 1 lvl
FROM cyclic
WHERE tbl_owner = (:owner)
GROUP BY tbl_name
ORDER BY lvl, tbl_name`
// depOrderTemplateCRDB uses the SHOW TABLES porcelain to calculate the referential depth.
// Older version of CRDB (<= 21.2) experience an infinite loop when executing
// depOrderTemplatePg.
const depOrderTemplateCRDB = `
WITH RECURSIVE
tables AS (
SELECT schema_name AS sch, table_name AS tbl
FROM [SHOW TABLES FROM %[1]s]
WHERE type='table'),
refs AS (
SELECT
constraint_schema AS child_sch, table_name AS child_tbl, referenced_table_name AS parent_tbl
FROM %[2]s.information_schema.referential_constraints
WHERE table_name != referenced_table_name
),
roots AS (
SELECT tables.sch, tables.tbl, 0 AS depth
FROM tables
WHERE (tables.sch, tables.tbl) NOT IN (SELECT (child_sch, child_tbl) FROM refs)
),
depths AS (
SELECT * FROM roots
UNION ALL
SELECT refs.child_sch, refs.child_tbl, max(depths.depth) + 1
FROM depths, refs
WHERE refs.parent_tbl = depths.tbl
GROUP BY 1, 2
)
SELECT tbl, max(depth)
FROM (SELECT *, -1 AS depth FROM tables UNION ALL SELECT * FROM depths)
GROUP BY 1
ORDER BY 2, 1`
// depOrderTemplatePg computes the "referential depth" of tables based on
// foreign-key constraints. Note that this only works with acyclic FK
// dependency graphs. This is ok because CRDB's lack of deferrable
// constraints means that a cyclic dependency graph would be unusable.
// Once CRDB has deferrable constraints, the need for computing this
// dependency ordering goes away.
//
// The query is structured as follows:
// - constraints: Used to resolve constraint names (i.e. primary or
// unique indexes) to the table that defines them.
// - tables: A list of all tables in the db.
// - refs: Maps referring tables (child) to referenced tables
// (parent). Table self-references are excluded from this query.
// - roots: Tables that contain no FK references to ensure that
// cyclical references remain unprocessed.
// - depths: A recursive CTE that builds up from the roots. In each
// step of the recursion, we select the child tables of the previous
// iteration whose parent table has a known depth and use the maximum
// parent's depth to derive the child's (updated) depth. The recursion
// halts when the previous iteration contains only leaf tables.
// - cycle_detect: Adds a sentinel depth value (-1) for all tables.
// - The top-level query then finds the maximum depth for each table.
// Any tables for which a depth cannot be computed (e.g. cyclical
// references) will return the sentinel value from cycle_detect.
//
// One limitation in this query is that the information_schema doesn't
// appear to provide any way to know about the schema in which the
// referenced table is defined.
const depOrderTemplatePg = `
WITH RECURSIVE
constraints
AS (
SELECT
table_catalog, table_schema, table_name, constraint_name
FROM
%[1]s.information_schema.table_constraints
),
tables
AS (
SELECT
table_catalog, table_schema, table_name
FROM
%[1]s.information_schema.tables
WHERE
table_type = 'BASE TABLE'
),
refs
AS (
SELECT
ref.constraint_catalog AS child_catalog,
ref.constraint_schema AS child_schema,
child.table_name AS child_table_name,
ref.unique_constraint_catalog AS parent_catalog,
ref.unique_constraint_schema AS parent_schema,
parent.table_name AS parent_table_name
FROM
%[1]s.information_schema.referential_constraints AS ref
JOIN constraints AS child ON
ref.constraint_catalog = child.table_catalog
AND ref.constraint_schema = child.table_schema
AND ref.constraint_name = child.constraint_name
JOIN constraints AS parent ON
ref.unique_constraint_catalog = parent.table_catalog
AND ref.unique_constraint_schema = parent.table_schema
AND ref.unique_constraint_name = parent.constraint_name
WHERE
(child.table_catalog, child.table_schema, child.table_name)
!= (parent.table_catalog, parent.table_schema, parent.table_name)
),
roots
AS (
SELECT
tables.table_catalog, tables.table_schema, tables.table_name
FROM
tables
WHERE
(tables.table_catalog, tables.table_schema, tables.table_name)
NOT IN (SELECT child_catalog, child_schema, child_table_name FROM refs)
),
depths
AS (
SELECT table_catalog, table_schema, table_name, 0 AS depth FROM roots
UNION ALL
SELECT
refs.child_catalog,
refs.child_schema,
refs.child_table_name,
depths.depth + 1
FROM
depths, refs
WHERE
refs.parent_catalog = depths.table_catalog
AND refs.parent_schema = depths.table_schema
AND refs.parent_table_name = depths.table_name
),
cycle_detect
AS (
SELECT table_catalog, table_schema, table_name, -1 AS depth FROM tables
UNION ALL
SELECT table_catalog, table_schema, table_name, depth FROM depths
)
SELECT
table_name, max(depth) AS depth
FROM
cycle_detect
WHERE
table_catalog = $1 AND table_schema = $2
GROUP BY
table_name
ORDER BY
depth, table_name`
// getDependencyOrder returns equivalency groups of tables defined
// within the given database. The order of the slice will satisfy
// the (acyclic) foreign-key dependency graph.
func getDependencyOrder(
ctx context.Context, tx *types.TargetPool, db ident.Schema,
) ([][]ident.Table, error) {
var args []any
var stmt string
switch tx.Product {
case types.ProductCockroachDB, types.ProductPostgreSQL:
// Extract just the database name to refer to information_schema.
parts := db.Idents(make([]ident.Ident, 0, 2))
if len(parts) != 2 {
return nil, errors.Errorf("expecting two schema parts, had %d", len(parts))
}
// We are using a different template for CRDB
// Older release (<= 21.2) may experience infinite loops using
// More recent releases may fail to report a correct depth
// when there are cross-schema dependencies.
// See https://github.com/cockroachdb/cockroach/issues/111419
// Once the issue above is fixed and we are not supporting 21.2
// we can use depOrderTemplatePg for CRDB as well.
if tx.Product == types.ProductCockroachDB {
stmt = fmt.Sprintf(depOrderTemplateCRDB, db, parts[0])
} else {
stmt = fmt.Sprintf(depOrderTemplatePg, parts[0])
args = []any{parts[0].Raw(), parts[1].Raw()}
}
case types.ProductMariaDB, types.ProductMySQL:
parts := db.Idents(make([]ident.Ident, 0, 1))
if len(parts) != 1 {
return nil, errors.Errorf("expecting one schema parts, had %d", len(parts))
}
stmt = depOrderTemplateMySQL
args = []any{parts[0].Raw()}
case types.ProductOracle:
stmt = depOrderTemplateOra
args = []any{sql.Named("owner", db.Raw())}
default:
return nil, errors.Errorf("getDependencyOrder unimplemented product: %s", tx.Product)
}
var cycles []ident.Table
var depOrder [][]ident.Table
err := retry.Retry(ctx, tx, func(ctx context.Context) error {
rows, err := tx.QueryContext(ctx, stmt, args...)
if err != nil {
return errors.Wrap(err, stmt)
}
defer rows.Close()
currentOrder := -1
for rows.Next() {
var tableName string
var nextOrder int
if err := rows.Scan(&tableName, &nextOrder); err != nil {
return err
}
tbl := ident.NewTable(db, ident.New(tableName))
// Table has no well-defined ordering.
if nextOrder < 0 {
cycles = append(cycles, tbl)
continue
}
// Allow skipping a level. This might happen if there
// are references across schemas.
for nextOrder > currentOrder {
depOrder = append(depOrder, nil)
currentOrder++
}
depOrder[currentOrder] = append(depOrder[currentOrder], tbl)
}
return nil
})
if len(cycles) > 0 {
return nil, errors.Errorf("cyclical FK references involving tables %s", cycles)
}
return depOrder, err
}