-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_diff.go
More file actions
488 lines (450 loc) · 13.7 KB
/
Copy pathsync_diff.go
File metadata and controls
488 lines (450 loc) · 13.7 KB
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
package main
import (
"encoding/json"
"fmt"
"sort"
"strings"
)
type SchemaChangeKind string
const (
ChangeAddModel SchemaChangeKind = "add_model"
ChangeAddField SchemaChangeKind = "add_field"
ChangeUpdateField SchemaChangeKind = "update_field"
ChangeDeleteField SchemaChangeKind = "delete_field"
ChangeAddConnection SchemaChangeKind = "add_connection"
ChangeUpdateConnection SchemaChangeKind = "update_connection"
)
type SchemaChange struct {
ID string
Kind SchemaChangeKind
Model string
Field *SyncField
Connection *SyncConnection
ReverseType string
Summary string
}
type ModelSchemaDiff struct {
Model string
Changes []SchemaChange
}
func projectSyncProfileKey(p SyncProject) string {
pt := strings.ToLower(strings.TrimSpace(p.ProjectType))
if pt == "" || pt == "general" {
return "general"
}
if p.PerTenantSeparateDatabase {
return "saas-per-tenant"
}
return "saas-shared"
}
// isEngineCompositeFieldType reports field types whose sub_field_info is owned by
// the engine (fixed html/markdown/text for multiline, etc.). Those leaves are not
// sync-worthy — comparing them produces false add_field noise when one side
// omits the built-in children from projectModelsInfo.
func isEngineCompositeFieldType(fieldType string) bool {
switch strings.ToLower(strings.TrimSpace(fieldType)) {
case "multiline", "media", "geo":
return true
default:
return false
}
}
func flattenModelFields(model SyncModel) []SyncField {
out := make([]SyncField, 0, len(model.Fields))
var walk func(fields []SyncField, parentID, pathPrefix string)
walk = func(fields []SyncField, parentID, pathPrefix string) {
for _, f := range fields {
copy := f
copy.ParentField = parentID
path := strings.TrimSpace(f.Identifier)
if pathPrefix != "" {
path = pathPrefix + "." + path
}
copy.Path = path
// Children are walked separately; clear nested payload on the flat row
// so equality checks compare this node only.
copy.SubFieldInfo = nil
out = append(out, copy)
// Engine composites: keep the parent in the diff, skip fixed leaves.
if isEngineCompositeFieldType(f.FieldType) {
continue
}
if len(f.SubFieldInfo) > 0 {
walk(f.SubFieldInfo, f.Identifier, path)
}
}
}
walk(model.Fields, "", "")
return out
}
// fieldSyncKey uniquely identifies a field within a model for sync matching.
// Nested/repeated subfields often reuse identifiers (_id, price, quantity);
// keying by identifier alone collapses them and creates false update_field diffs.
// Prefer full Path (routine.details.date_and_time) so two groups that share an
// immediate parent identifier do not collide.
func fieldSyncKey(f SyncField) string {
if p := strings.ToLower(strings.TrimSpace(f.Path)); p != "" {
return p
}
parent := strings.ToLower(strings.TrimSpace(f.ParentField))
id := strings.ToLower(strings.TrimSpace(f.Identifier))
if parent == "" {
return id
}
return parent + "." + id
}
func fieldPathDepth(f SyncField) int {
key := fieldSyncKey(f)
if key == "" {
return 0
}
return strings.Count(key, ".")
}
func fieldMap(fields []SyncField) map[string]SyncField {
m := make(map[string]SyncField, len(fields))
for _, f := range fields {
m[fieldSyncKey(f)] = f
}
return m
}
func validationBoolEqual(a, b *bool) bool {
av := a != nil && *a
bv := b != nil && *b
return av == bv
}
func validationEqualForSync(a, b *SyncFieldValidation) bool {
if a == nil && b == nil {
return true
}
var empty SyncFieldValidation
if a == nil {
a = &empty
}
if b == nil {
b = &empty
}
if !validationBoolEqual(a.Required, b.Required) ||
!validationBoolEqual(a.Unique, b.Unique) ||
!validationBoolEqual(a.Hide, b.Hide) ||
!validationBoolEqual(a.AsTitle, b.AsTitle) ||
!validationBoolEqual(a.IsMultiChoice, b.IsMultiChoice) ||
!validationBoolEqual(a.IsEmail, b.IsEmail) ||
!validationBoolEqual(a.IsGallery, b.IsGallery) ||
!validationBoolEqual(a.IsURL, b.IsURL) {
return false
}
if a.FixedListElementType != b.FixedListElementType || a.Placeholder != b.Placeholder {
return false
}
// Treat nil and empty slices as equal — draft schemaPreview often omits empty
// locals while live returns []. That alone was producing dozens of false update_field diffs.
if !stringSliceEqualForSync(a.Locals, b.Locals) {
return false
}
return anySliceEqualForSync(a.FixedListElements, b.FixedListElements)
}
func stringSliceEqualForSync(a, b []string) bool {
if len(a) == 0 && len(b) == 0 {
return true
}
if len(a) != len(b) {
return false
}
la, _ := json.Marshal(a)
lb, _ := json.Marshal(b)
return string(la) == string(lb)
}
func anySliceEqualForSync(a, b []any) bool {
if len(a) == 0 && len(b) == 0 {
return true
}
ab, _ := json.Marshal(a)
bb, _ := json.Marshal(b)
return string(ab) == string(bb)
}
func validationEqual(a, b *SyncFieldValidation) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
ab, _ := json.Marshal(a)
bb, _ := json.Marshal(b)
return string(ab) == string(bb)
}
// fieldsMatchForSync compares structural field shape for schema sync.
// Cosmetic metadata (label, serial, input_type) is ignored; nil/false validation booleans are equivalent.
func fieldsMatchForSync(a, b SyncField) bool {
return strings.EqualFold(a.Identifier, b.Identifier) &&
a.FieldType == b.FieldType &&
a.FieldSubType == b.FieldSubType &&
a.ParentField == b.ParentField &&
validationEqualForSync(a.Validation, b.Validation)
}
func fieldEqual(a, b SyncField) bool {
return strings.EqualFold(a.Identifier, b.Identifier) &&
a.Label == b.Label &&
a.FieldType == b.FieldType &&
a.FieldSubType == b.FieldSubType &&
a.InputType == b.InputType &&
a.ParentField == b.ParentField &&
a.Serial == b.Serial &&
validationEqual(a.Validation, b.Validation)
}
func connectionKey(fromModel string, conn SyncConnection) string {
knownAs := conn.KnownAs
if knownAs == "" {
knownAs = conn.Model
}
return fmt.Sprintf("%s->%s:%s", strings.ToLower(fromModel), strings.ToLower(conn.Model), strings.ToLower(knownAs))
}
func isForwardConnection(conn SyncConnection) bool {
t := strings.ToLower(strings.TrimSpace(conn.Type))
return t == "" || t == "forward"
}
func findReverseRelationType(models map[string]SyncModel, fromModel, toModel string) string {
target, ok := models[strings.ToLower(toModel)]
if !ok {
return "has_many"
}
for _, c := range target.Connections {
if !isForwardConnection(c) && strings.EqualFold(c.Model, fromModel) {
if c.Relation != "" {
return c.Relation
}
}
}
return "has_many"
}
func computeSchemaDiff(sourceModels, destModels []SyncModel) []ModelSchemaDiff {
srcMap := make(map[string]SyncModel, len(sourceModels))
dstMap := make(map[string]SyncModel, len(destModels))
for _, m := range sourceModels {
srcMap[strings.ToLower(m.Name)] = m
}
for _, m := range destModels {
dstMap[strings.ToLower(m.Name)] = m
}
modelNames := make([]string, 0, len(srcMap))
for name := range srcMap {
modelNames = append(modelNames, name)
}
sort.Strings(modelNames)
var diffs []ModelSchemaDiff
for _, modelKey := range modelNames {
src := srcMap[modelKey]
dst, destExists := dstMap[modelKey]
var changes []SchemaChange
if !destExists {
changes = append(changes, SchemaChange{
ID: fmt.Sprintf("model:%s", src.Name),
Kind: ChangeAddModel,
Model: src.Name,
Summary: fmt.Sprintf("Add model %q", src.Name),
})
}
srcFields := flattenModelFields(src)
dstFields := fieldMap(flattenModelFields(dst))
// Depth-first ancestry order: parents before children so nested adds
// (routine → details → date_and_time) apply cleanly.
sort.SliceStable(srcFields, func(i, j int) bool {
di, dj := fieldPathDepth(srcFields[i]), fieldPathDepth(srcFields[j])
if di != dj {
return di < dj
}
if srcFields[i].Serial != srcFields[j].Serial {
return srcFields[i].Serial < srcFields[j].Serial
}
return fieldSyncKey(srcFields[i]) < fieldSyncKey(srcFields[j])
})
for _, sf := range srcFields {
df, ok := dstFields[fieldSyncKey(sf)]
fieldCopy := sf
if !ok {
changes = append(changes, SchemaChange{
ID: fmt.Sprintf("field:%s:%s", src.Name, fieldSyncKey(sf)),
Kind: ChangeAddField,
Model: src.Name,
Field: &fieldCopy,
Summary: fmt.Sprintf("Add field %q (%s) on %q", sf.Label, sf.Identifier, src.Name),
})
continue
}
if !fieldsMatchForSync(sf, df) {
changes = append(changes, SchemaChange{
ID: fmt.Sprintf("field-update:%s:%s", src.Name, fieldSyncKey(sf)),
Kind: ChangeUpdateField,
Model: src.Name,
Field: &fieldCopy,
Summary: fmt.Sprintf("Update field %q on %q", sf.Identifier, src.Name),
})
}
}
// Index every dest peer edge by model+known_as (ignore Type). Flipped
// forward/backward metadata must not look like a missing relation.
dstConnByKey := make(map[string]SyncConnection)
if destExists {
for _, c := range dst.Connections {
dstConnByKey[connectionKey(dst.Name, c)] = c
}
}
for _, c := range src.Connections {
if !isForwardConnection(c) {
continue
}
key := connectionKey(src.Name, c)
connCopy := c
reverse := findReverseRelationType(srcMap, src.Name, c.Model)
forward := c.Relation
if forward == "" {
forward = "has_many"
}
knownAs := c.KnownAs
if knownAs == "" {
knownAs = c.Model
}
if destConn, ok := dstConnByKey[key]; ok {
destRel := destConn.Relation
if destRel == "" {
destRel = "has_many"
}
if isForwardConnection(destConn) && strings.EqualFold(destRel, forward) {
continue
}
changes = append(changes, SchemaChange{
ID: fmt.Sprintf("conn-fix:%s:%s", src.Name, key),
Kind: ChangeUpdateConnection,
Model: src.Name,
Connection: &connCopy,
ReverseType: reverse,
Summary: fmt.Sprintf(
"Fix relation direction %q → %q (%s ↔ %s, known_as: %q) on %q",
src.Name, c.Model, forward, reverse, knownAs, src.Name,
),
})
continue
}
changes = append(changes, SchemaChange{
ID: fmt.Sprintf("conn:%s:%s", src.Name, key),
Kind: ChangeAddConnection,
Model: src.Name,
Connection: &connCopy,
ReverseType: reverse,
Summary: fmt.Sprintf("Add relation %q → %q (%s ↔ %s, known_as: %q) on %q", src.Name, c.Model, forward, reverse, knownAs, src.Name),
})
}
if len(changes) > 0 {
diffs = append(diffs, ModelSchemaDiff{
Model: src.Name,
Changes: changes,
})
}
}
return diffs
}
// computeSchemaDeleteDiff finds fields present on destination but missing on
// source (destination-only). These are optional destructive removes — not
// included in computeSchemaDiff so additive sync stays safe by default.
func computeSchemaDeleteDiff(sourceModels, destModels []SyncModel) []ModelSchemaDiff {
srcMap := make(map[string]SyncModel, len(sourceModels))
for _, m := range sourceModels {
srcMap[strings.ToLower(m.Name)] = m
}
modelNames := make([]string, 0, len(destModels))
for _, m := range destModels {
modelNames = append(modelNames, strings.ToLower(m.Name))
}
sort.Strings(modelNames)
seen := make(map[string]struct{}, len(modelNames))
var diffs []ModelSchemaDiff
for _, modelKey := range modelNames {
if _, ok := seen[modelKey]; ok {
continue
}
seen[modelKey] = struct{}{}
var dst SyncModel
for _, m := range destModels {
if strings.EqualFold(m.Name, modelKey) {
dst = m
break
}
}
src, srcExists := srcMap[modelKey]
if !srcExists {
// Entire model missing on source — model delete is out of scope for now.
continue
}
srcFields := fieldMap(flattenModelFields(src))
dstFields := flattenModelFields(dst)
// Delete deepest children first so parent groups are not removed while
// nested ops still reference them.
sort.SliceStable(dstFields, func(i, j int) bool {
di, dj := fieldPathDepth(dstFields[i]), fieldPathDepth(dstFields[j])
if di != dj {
return di > dj
}
if dstFields[i].Serial != dstFields[j].Serial {
return dstFields[i].Serial < dstFields[j].Serial
}
return fieldSyncKey(dstFields[i]) < fieldSyncKey(dstFields[j])
})
var changes []SchemaChange
for _, df := range dstFields {
if _, ok := srcFields[fieldSyncKey(df)]; ok {
continue
}
fieldCopy := df
changes = append(changes, SchemaChange{
ID: fmt.Sprintf("field-delete:%s:%s", dst.Name, fieldSyncKey(df)),
Kind: ChangeDeleteField,
Model: dst.Name,
Field: &fieldCopy,
Summary: fmt.Sprintf("Delete field %q on %q", df.Identifier, dst.Name),
})
}
if len(changes) > 0 {
diffs = append(diffs, ModelSchemaDiff{Model: dst.Name, Changes: changes})
}
}
return diffs
}
func mergeSchemaDiffs(parts ...[]ModelSchemaDiff) []ModelSchemaDiff {
byModel := make(map[string]*ModelSchemaDiff)
order := make([]string, 0)
for _, part := range parts {
for _, md := range part {
key := strings.ToLower(md.Model)
existing, ok := byModel[key]
if !ok {
cp := ModelSchemaDiff{Model: md.Model, Changes: append([]SchemaChange{}, md.Changes...)}
byModel[key] = &cp
order = append(order, key)
continue
}
existing.Changes = append(existing.Changes, md.Changes...)
}
}
out := make([]ModelSchemaDiff, 0, len(order))
for _, key := range order {
out = append(out, *byModel[key])
}
return out
}
func partitionSchemaChanges(changes []SchemaChange) (additive, deletes []SchemaChange) {
for _, ch := range changes {
if ch.Kind == ChangeDeleteField {
deletes = append(deletes, ch)
continue
}
additive = append(additive, ch)
}
return additive, deletes
}
func printModelDiffHeader(model string, changes []SchemaChange) {
fmt.Println()
print_step(fmt.Sprintf("Model: %s (%d change(s))", model, len(changes)))
for _, ch := range changes {
fmt.Printf(" - %s\n", ch.Summary)
}
}