-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrift.go
388 lines (328 loc) · 10.9 KB
/
drift.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
package pgutil
import (
"cmp"
"fmt"
"slices"
"sort"
"strings"
)
func Compare(a, b SchemaDescription) (statements []string) {
var (
aTableComponentModifiers = NewTableComponentModifiers(a, a.Tables)
bTableComponentModifiers = NewTableComponentModifiers(b, b.Tables)
)
uniqueStatements := map[string]ddlStatement{}
for _, statements := range [][]ddlStatement{
compareObjects(a, b, wrapWithContextValue(a, a.Extensions, NewExtensionModifier), wrapWithContextValue(b, b.Extensions, NewExtensionModifier)),
compareObjects(a, b, wrapWithContextValue(a, a.Enums, NewEnumModifier), wrapWithContextValue(b, b.Enums, NewEnumModifier)),
compareObjects(a, b, wrapWithContextValue(a, a.Functions, NewFunctionModifier), wrapWithContextValue(b, b.Functions, NewFunctionModifier)),
compareObjects(a, b, wrapWithContextValue(a, a.Tables, NewTableModifier), wrapWithContextValue(b, b.Tables, NewTableModifier)),
compareObjects(a, b, wrapWithContextValue(a, a.Sequences, NewSequenceModifier), wrapWithContextValue(b, b.Sequences, NewSequenceModifier)),
compareObjects(a, b, aTableComponentModifiers.Columns, bTableComponentModifiers.Columns),
compareObjects(a, b, aTableComponentModifiers.Constraints, bTableComponentModifiers.Constraints),
compareObjects(a, b, aTableComponentModifiers.Indexes, bTableComponentModifiers.Indexes),
compareObjects(a, b, wrapWithContextValue(a, a.Views, NewViewModifier), wrapWithContextValue(b, b.Views, NewViewModifier)),
compareObjects(a, b, wrapWithContextValue(a, a.Triggers, NewTriggerModifier), wrapWithContextValue(b, b.Triggers, NewTriggerModifier)),
} {
for _, statement := range statements {
key := strings.Join([]string{
statement.statementType,
statement.objectType,
statement.key,
}, "::")
uniqueStatements[key] = statement
}
}
var unorderedStatements []ddlStatement
for _, statement := range uniqueStatements {
unorderedStatements = append(unorderedStatements, statement)
}
filter := func(statementType, objectType string) []ddlStatement {
var filtered []ddlStatement
for _, statements := range unorderedStatements {
if statements.statementType == statementType && statements.objectType == objectType {
filtered = append(filtered, statements)
}
}
return filtered
}
// Dependency mapping:
//
// extensions : no dependencies
// enums : no dependencies
// functions : no dependencies
// tables : no dependencies
// sequences : no dependencies
// columns : depends on tables, enums, sequences
// constraints : depends on tables, columns; fk constraints depend on unique constraints
// indexes : depends on tables, columns
// views : depends on tables, columns, views
// triggers : depends on tables, columns, functions
sortByKey := func(statements []ddlStatement) {
slices.SortFunc(statements, func(a, b ddlStatement) int {
return cmp.Compare(a.key, b.key)
})
}
sortByClosure := func(cls closure) func([]ddlStatement) {
return func(statements []ddlStatement) {
statementsByKey := map[string]ddlStatement{}
for _, stmt := range statements {
statementsByKey[stmt.key] = stmt
}
// Build a graph where nodes are statement keys and edges are
// transitive references between them. Edges are directed from
// the reference to the referencee.
graph := map[string]map[string]struct{}{}
for _, stmt := range statements {
// Ensure the graph contains all keys.
graph[stmt.key] = map[string]struct{}{}
}
for _, stmt := range statements {
for reference := range cls[stmt.key] {
if _, ok := graph[reference]; ok {
graph[reference][stmt.key] = struct{}{}
}
}
}
// Build a topological ordering of the statements where ties
// are broken by keys in lexicographic order.
topologicalOrder := make([]ddlStatement, 0, len(statements))
for len(graph) > 0 {
// Gather all keys with no remaining dependencies.
//
// The textbook implementation would use a min queue to quickly select the
// key with no adjacent edges, but the size of the data here should be small
// enough that that scanning the (shrinking) graph on each iteration is fine.
var candidates []string
for key, edges := range graph {
if len(edges) == 0 {
candidates = append(candidates, key)
}
}
if len(candidates) == 0 {
panic("cycle detected in closure, cannot perform topological sort")
}
// Select the next key and add it to the topological order.
sort.Strings(candidates)
top := candidates[0]
topologicalOrder = append(topologicalOrder, statementsByKey[top])
// Remove the key from the node and edge sets.
delete(graph, top)
for _, edges := range graph {
delete(edges, top)
}
}
// Update the statements in-place to reflect the new order.
for i := range statements {
statements[i] = topologicalOrder[i]
}
}
}
createDependencyClosure, dropDependencyClosure := viewDependencyClosures(a, b)
sortCreateViews := sortByClosure(createDependencyClosure)
sortDropViews := sortByClosure(dropDependencyClosure)
order := []struct {
statementType string
objectType string
order func(statements []ddlStatement)
}{
{"drop", "trigger", sortByKey},
{"drop", "view", sortDropViews},
{"drop", "constraint", sortDropViews},
{"drop", "index", sortByKey},
{"drop", "column", sortByKey},
{"drop", "sequence", sortByKey},
{"drop", "table", sortByKey},
{"drop", "function", sortByKey},
{"drop", "enum", sortByKey},
{"drop", "extension", sortByKey},
{"create", "extension", sortByKey},
{"create", "enum", sortByKey},
{"replace", "enum", sortByKey},
{"create", "function", sortByKey},
{"replace", "function", sortByKey},
{"create", "table", sortByKey},
{"create", "sequence", sortByKey},
{"replace", "sequence", sortByKey},
{"create", "column", sortByKey},
{"replace", "column", sortByKey},
{"create", "index", sortByKey},
{"create", "constraint", sortByKey},
{"create", "view", sortCreateViews},
{"create", "trigger", sortByKey},
}
for _, o := range order {
filtered := filter(o.statementType, o.objectType)
o.order(filtered)
for _, statement := range filtered {
statements = append(statements, statement.statements...)
}
}
return statements
}
//
//
//
func viewDependencyClosures(a, b SchemaDescription) (createDependencyClosure, dropDependencyClosure closure) {
createDependencyClosure = closure{}
for _, dependency := range a.ColumnDependencies {
sourceKey := fmt.Sprintf("%q.%q", dependency.SourceNamespace, dependency.SourceTableOrViewName)
dependencyKey := fmt.Sprintf("%q.%q", dependency.UsedNamespace, dependency.UsedTableOrView)
if _, ok := createDependencyClosure[sourceKey]; !ok {
createDependencyClosure[sourceKey] = map[string]struct{}{}
}
createDependencyClosure[sourceKey][dependencyKey] = struct{}{}
}
dropDependencyClosure = closure{}
for _, dependency := range b.ColumnDependencies {
sourceKey := fmt.Sprintf("%q.%q", dependency.SourceNamespace, dependency.SourceTableOrViewName)
dependencyKey := fmt.Sprintf("%q.%q", dependency.UsedNamespace, dependency.UsedTableOrView)
if _, ok := dropDependencyClosure[dependencyKey]; !ok {
dropDependencyClosure[dependencyKey] = map[string]struct{}{}
}
dropDependencyClosure[dependencyKey][sourceKey] = struct{}{}
}
transitiveClosure(createDependencyClosure)
transitiveClosure(dropDependencyClosure)
return createDependencyClosure, dropDependencyClosure
}
//
//
// closure is a reference relationship mapping a key to a set of references.
type closure map[string]map[string]struct{}
// transitiveClosure expands the given closure in-place to directly encode
// all transitive references.
func transitiveClosure(cls closure) {
changed := true
for changed {
changed = false
for _, references := range cls {
for oldReference := range references {
for newReference := range cls[oldReference] {
if _, ok := references[newReference]; !ok {
references[newReference] = struct{}{}
changed = true
}
}
}
}
}
}
//
//
//
type keyer interface {
Key() string
}
type equaler[T any] interface {
Equals(T) bool
}
type modifier[T equaler[T]] interface {
keyer
ObjectType() string
Description() T
Create() string
Drop() string
}
type alterer[T any] interface {
AlterExisting(existingSchema SchemaDescription, existingObject T) ([]ddlStatement, bool)
}
type ddlStatement struct {
key string
statementType string
objectType string
statements []string
}
func newStatement(key string, statementType, objectType string, statements ...string) ddlStatement {
return ddlStatement{
key: key,
statementType: statementType,
objectType: objectType,
statements: statements,
}
}
func compareObjects[T equaler[T], M modifier[T]](a, b SchemaDescription, as, bs []M) (statements []ddlStatement) {
missing, additional, common := partition(as, bs)
for _, modifier := range missing {
statements = append(statements, newStatement(
modifier.Key(),
"create",
modifier.ObjectType(),
modifier.Create(),
))
}
for _, modifier := range additional {
statements = append(statements, newStatement(
modifier.Key(),
"drop",
modifier.ObjectType(),
modifier.Drop(),
))
}
for _, pair := range common {
var (
aModifier = pair.a
bModifier = pair.b
aDescription = aModifier.Description()
bDescription = bModifier.Description()
)
if aDescription.Equals(bDescription) {
continue
}
if alterer, ok := any(aModifier).(alterer[T]); ok {
if alterStatements, ok := alterer.AlterExisting(b, bDescription); ok {
statements = append(statements, alterStatements...)
continue
}
}
statements = append(statements, newStatement(bModifier.Key(), "drop", bModifier.ObjectType(), bModifier.Drop()))
statements = append(statements, newStatement(aModifier.Key(), "create", aModifier.ObjectType(), aModifier.Create()))
}
return statements
}
//
//
//
type pair[T any] struct {
a, b T
}
// missing = present in a but not b
// additional = present in b but not a
func partition[T keyer](a, b []T) (missing, additional []T, common []pair[T]) {
aMap := map[string]T{}
for _, value := range a {
aMap[value.Key()] = value
}
bMap := map[string]T{}
for _, value := range b {
bMap[value.Key()] = value
}
for key, aValue := range aMap {
if bValue, ok := bMap[key]; ok {
common = append(common, pair[T]{aValue, bValue})
} else {
missing = append(missing, aValue)
}
}
for key, bValue := range bMap {
if _, ok := aMap[key]; !ok {
additional = append(additional, bValue)
}
}
return missing, additional, common
}
//
//
//
func wrap[T, R any](s []T, f func(T) R) (wrapped []R) {
for _, value := range s {
wrapped = append(wrapped, f(value))
}
return wrapped
}
func wrapWithContextValue[C, T, R any](c C, s []T, f func(C, T) R) []R {
return wrap(s, func(v T) R { return f(c, v) })
}
func wrapWithContextValues[C1, C2, T, R any](c1 C1, c2 C2, s []T, f func(C1, C2, T) R) []R {
return wrap(s, func(v T) R { return f(c1, c2, v) })
}