@crombolo/transfer 0.26.0 → 0.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crombolo/transfer",
3
- "version": "0.26.0",
3
+ "version": "0.28.0",
4
4
  "description": "Content transfer for crombolo: bundles of content moved between environments.",
5
5
  "license": "MIT",
6
6
  "author": "Crombolo",
@@ -28,7 +28,7 @@
28
28
  "access": "public"
29
29
  },
30
30
  "dependencies": {
31
- "@crombolo/core": "0.26.0",
32
- "@crombolo/data": "0.26.0"
31
+ "@crombolo/core": "0.28.0",
32
+ "@crombolo/data": "0.28.0"
33
33
  }
34
34
  }
package/src/check.ts CHANGED
@@ -16,6 +16,9 @@
16
16
  */
17
17
  import { ObjectTypes } from '@crombolo/core'
18
18
  import {
19
+ type AppliedState,
20
+ appliedValueKey,
21
+ ContentAppliedRepository,
19
22
  ContentTypeRepository,
20
23
  currentSchemaState,
21
24
  DataTypeRepository,
@@ -89,7 +92,7 @@ export function resolutionsFor(code: string): readonly string[] {
89
92
  switch (code) {
90
93
  case 'local-edit':
91
94
  return [
92
- '--resolve <key>/<property>=take-bundle to let the bundle win for this property',
95
+ '--resolve <key>/<property>=take-bundle to let the bundle win for this property (@name and @parent for the name and place)',
93
96
  '--resolve <key>/<property>=keep-local to keep what is here for this property',
94
97
  '--resolve <key>=<choice> to answer the whole node, skip included',
95
98
  '--resolve-all=<choice> to answer every one the same way',
@@ -118,6 +121,11 @@ export function resolutionsFor(code: string): readonly string[] {
118
121
  return ['transfer the node it refers to', 'or accept a value that points at nothing']
119
122
  case 'name-collision':
120
123
  return ['rename one of them, or --resolve <key>=skip']
124
+ case 'move-refused':
125
+ return [
126
+ 'allow its type under the new parent in schema/, then import again',
127
+ '--resolve <key>/@parent=keep-local to leave it where it is',
128
+ ]
121
129
  case 'missing-template':
122
130
  return ['deploy the view, and the template attaches on the next save']
123
131
  case 'content-sealed':
@@ -209,6 +217,36 @@ export interface NodePlan {
209
217
  resolution: Resolution | undefined
210
218
  /** Values kept as they are here, by property handle; the rest come from the bundle. */
211
219
  keepLocal?: string[]
220
+ /** Cultures whose name stays as it is here (`""` for an invariant one); the rest take the bundle's. */
221
+ keepNames?: string[]
222
+ /** Where the node moves to, when the bundle moved it and the site did not (story 163). */
223
+ moveTo?: string | null
224
+ /** The bundle reordered it among its siblings and the site did not. */
225
+ reorder?: boolean
226
+ /** The site reordered it since the last import, and keeps its place. */
227
+ keepOrder?: boolean
228
+ }
229
+
230
+ /** The handle a node's name takes in findings and `--resolve`. */
231
+ export const NAME_HANDLE = '@name'
232
+ /** The handle a node's parent takes in findings and `--resolve`. */
233
+ export const PARENT_HANDLE = '@parent'
234
+
235
+ /**
236
+ * One field compared three ways (story 163): the site, the bundle, and what the
237
+ * last import applied. Without a record of that, a difference is a question.
238
+ * Pure.
239
+ */
240
+ export function threeWay(
241
+ mine: unknown,
242
+ theirs: unknown,
243
+ base: { value: unknown } | undefined,
244
+ ): 'same' | 'take' | 'keep' | 'ask' {
245
+ if (sameValue(mine, theirs)) return 'same'
246
+ if (!base) return 'ask'
247
+ if (sameValue(theirs, base.value)) return 'keep'
248
+ if (sameValue(mine, base.value)) return 'take'
249
+ return 'ask'
212
250
  }
213
251
 
214
252
  export interface TransferCheck {
@@ -426,6 +464,8 @@ export async function checkBundle(
426
464
  }
427
465
 
428
466
  const siblingNames = new Map<string, Set<string>>()
467
+ // What earlier imports applied: the third side of every comparison below.
468
+ const appliedBefore = await new ContentAppliedRepository(db).byKeys(set.nodes.map((n) => n.key))
429
469
 
430
470
  for (const node of set.nodes) {
431
471
  const name = node.variants[0]?.name ?? node.key
@@ -631,65 +671,189 @@ export async function checkBundle(
631
671
  continue
632
672
  }
633
673
 
634
- // What the bundle would actually change, property by property.
674
+ const decided = await threeWayPlan(node, name, existing, appliedBefore.get(node.key), editors)
675
+ findings.push(...decided.findings)
676
+ plan.push(decided.plan)
677
+ }
678
+
679
+ /**
680
+ * What the bundle would change on a node that is here, field by field, three
681
+ * ways (story 163): taken where only the bundle changed, kept where only the
682
+ * site did, and asked where both did — or, for a node no import has recorded,
683
+ * wherever the two differ, as before.
684
+ */
685
+ async function threeWayPlan(
686
+ node: BundleNode,
687
+ name: string,
688
+ existing: NodeRow,
689
+ base: AppliedState | undefined,
690
+ editors: ReadonlyMap<string, string>,
691
+ ): Promise<{ plan: NodePlan; findings: Finding[] }> {
692
+ const out: Finding[] = []
693
+ const answers = resolutions[node.key]
694
+ const resolution = answers?.[WHOLE_NODE] ?? options.resolveAll
695
+ const subjectType = node.kind === 'media' ? 'media' : 'document'
635
696
  const current = await repoFor(node.kind).byKey(node.key)
636
697
  const currentByKey = new Map(
637
- (current?.values ?? []).map((v) => [`${v.alias}|${v.culture ?? ''}|${v.segment ?? ''}`, v]),
698
+ (current?.values ?? []).map((v) => [appliedValueKey(v.alias, v.culture, v.segment), v]),
638
699
  )
639
- const changedValues = node.values.filter((value) => {
640
- if (!editors.has(value.property)) return false
641
- const mine = currentByKey.get(
642
- `${value.property}|${value.culture ?? ''}|${value.segment ?? ''}`,
700
+ const keepLocal: string[] = []
701
+ const keepNames: string[] = []
702
+ const changing = new Set<string>()
703
+ const taken: string[] = []
704
+ const kept: string[] = []
705
+ let taking = false
706
+ let asked = 0
707
+ const ask = (alias: string, culture: string | null, what: string) => {
708
+ const handle = propertyHandle(alias, culture)
709
+ const answer = answerFor(answers, alias, culture, options.resolveAll)
710
+ if (answer === 'keep-local') return 'keep' as const
711
+ if (answer === 'take-bundle') return 'take' as const
712
+ asked += 1
713
+ out.push({
714
+ kind: 'person',
715
+ code: 'local-edit',
716
+ subjectType,
717
+ subjectKey: node.key,
718
+ subjectName: name,
719
+ propertyAlias: alias,
720
+ culture,
721
+ message: `"${name}" is already here and its ${what}${culture ? ` (${culture})` : ''} differs from the bundle's${base ? ', and both changed since it was last imported' : ''}`,
722
+ link: link(node.key),
723
+ })
724
+ return handle
725
+ }
726
+
727
+ for (const value of node.values) {
728
+ if (!editors.has(value.property)) continue
729
+ const key = appliedValueKey(value.property, value.culture, value.segment)
730
+ const side = threeWay(
731
+ currentByKey.get(key)?.value,
732
+ value.value,
733
+ base ? { value: base.values[key] } : undefined,
643
734
  )
644
- return !sameValue(mine?.value, value.value)
645
- })
646
- const changing = [...new Set(changedValues.map((value) => value.property))]
735
+ if (side === 'same') continue
736
+ changing.add(value.property)
737
+ const handle = propertyHandle(value.property, value.culture)
738
+ const decision = side === 'ask' ? ask(value.property, value.culture, value.property) : side
739
+ if (decision === 'keep') {
740
+ keepLocal.push(handle)
741
+ if (side === 'keep') kept.push(handle)
742
+ } else if (decision === 'take') {
743
+ taking = true
744
+ if (side === 'take') taken.push(handle)
745
+ }
746
+ }
647
747
 
648
- if (changing.length === 0) {
649
- plan.push({
748
+ // Names: a node no import has recorded takes the bundle's, as it always has.
749
+ if (!base && current)
750
+ for (const variant of node.variants) {
751
+ const mine = current.variants.find((v) => (v.culture ?? '') === (variant.culture ?? ''))
752
+ if (mine?.name !== variant.name) taking = true
753
+ }
754
+ if (base && current)
755
+ for (const variant of node.variants) {
756
+ const culture = variant.culture ?? ''
757
+ const mine = current.variants.find((v) => (v.culture ?? '') === culture)?.name
758
+ const side = threeWay(mine, variant.name, { value: base.names[culture] })
759
+ if (side === 'same') continue
760
+ const decision = side === 'ask' ? ask(NAME_HANDLE, variant.culture, 'name') : side
761
+ if (decision === 'keep') {
762
+ keepNames.push(culture)
763
+ if (side === 'keep') kept.push(propertyHandle(NAME_HANDLE, variant.culture))
764
+ } else if (decision === 'take') {
765
+ taking = true
766
+ if (side === 'take') taken.push(propertyHandle(NAME_HANDLE, variant.culture))
767
+ }
768
+ }
769
+
770
+ // Place and order follow the bundle only where an import recorded them, and
771
+ // only where the bundle is placed where it says.
772
+ let moveTo: string | null | undefined
773
+ let reorder = false
774
+ let keepOrder = false
775
+ if (base && !options.under) {
776
+ const parentNow = current?.parentKey ?? null
777
+ const side = threeWay(parentNow, node.parent, { value: base.parent })
778
+ const decision = side === 'ask' ? ask(PARENT_HANDLE, null, 'place in the tree') : side
779
+ if (decision === 'take') {
780
+ const refused =
781
+ node.kind === 'document' || node.kind === 'media'
782
+ ? await (node.kind === 'media' ? mediaTypes : types).placementRefusal(
783
+ current?.contentTypeKey ?? node.contentType.key ?? '',
784
+ node.parent,
785
+ )
786
+ : undefined
787
+ if (refused)
788
+ out.push({
789
+ kind: 'blocking',
790
+ code: 'move-refused',
791
+ subjectType,
792
+ subjectKey: node.key,
793
+ subjectName: name,
794
+ propertyAlias: null,
795
+ culture: null,
796
+ message: `the bundle moves "${name}", but ${refused}`,
797
+ link: link(node.key),
798
+ })
799
+ else {
800
+ moveTo = node.parent
801
+ taking = true
802
+ taken.push(PARENT_HANDLE)
803
+ }
804
+ } else if (side === 'keep') kept.push(PARENT_HANDLE)
805
+ const parentHere = moveTo !== undefined ? moveTo : parentNow
806
+ if (parentHere === node.parent && node.kind === 'document') {
807
+ const bundleMoved = node.sortOrder !== base.bundleSortOrder
808
+ const siteMoved = existing.sortOrder !== base.sortOrder
809
+ if (bundleMoved && !siteMoved) reorder = true
810
+ else if (siteMoved) keepOrder = true
811
+ if (reorder) taking = true
812
+ }
813
+ }
814
+
815
+ if (taken.length > 0)
816
+ out.push({
817
+ kind: 'auto',
818
+ code: 'bundle-changed',
819
+ subjectType,
820
+ subjectKey: node.key,
821
+ subjectName: name,
822
+ propertyAlias: null,
823
+ culture: null,
824
+ message: `"${name}" changed in the bundle since it was last imported, and not here: ${taken.join(', ')} will be updated`,
825
+ link: link(node.key),
826
+ })
827
+ if (kept.length > 0)
828
+ out.push({
829
+ kind: 'auto',
830
+ code: 'local-kept',
831
+ subjectType,
832
+ subjectKey: node.key,
833
+ subjectName: name,
834
+ propertyAlias: null,
835
+ culture: null,
836
+ message: `"${name}" was changed here since it was last imported, and not in the bundle: ${kept.join(', ')} stays as it is`,
837
+ link: link(node.key),
838
+ })
839
+
840
+ const changes = taking || asked > 0 || reorder
841
+ return {
842
+ plan: {
650
843
  key: node.key,
651
844
  name,
652
845
  kind: node.kind,
653
- action: 'unchanged',
654
- changing: [],
846
+ action: changes ? 'update' : 'unchanged',
847
+ changing: [...changing],
655
848
  resolution,
656
- })
657
- continue
849
+ keepLocal,
850
+ ...(keepNames.length > 0 ? { keepNames } : {}),
851
+ ...(moveTo !== undefined ? { moveTo } : {}),
852
+ ...(reorder ? { reorder } : {}),
853
+ ...(keepOrder ? { keepOrder } : {}),
854
+ },
855
+ findings: out,
658
856
  }
659
-
660
- // It exists, and the bundle disagrees with it. We cannot know whether
661
- // somebody edited it here or the bundle is simply newer, so this is a
662
- // decision rather than a defect — and it is the one an import waits on.
663
- // One per property, so each can be answered on its own.
664
- const keepLocal: string[] = []
665
- let taking = false
666
- for (const value of changedValues) {
667
- const handle = propertyHandle(value.property, value.culture)
668
- const answer = answerFor(answers, value.property, value.culture, options.resolveAll)
669
- if (answer === 'keep-local') keepLocal.push(handle)
670
- else if (answer === 'take-bundle') taking = true
671
- else
672
- findings.push({
673
- kind: 'person',
674
- code: 'local-edit',
675
- subjectType: node.kind === 'media' ? 'media' : 'document',
676
- subjectKey: node.key,
677
- subjectName: name,
678
- propertyAlias: value.property,
679
- culture: value.culture ?? null,
680
- message: `"${name}" is already here and its ${value.property}${value.culture ? ` (${value.culture})` : ''} differs from the bundle's`,
681
- link: link(node.key),
682
- })
683
- }
684
- plan.push({
685
- key: node.key,
686
- name,
687
- kind: node.kind,
688
- action: taking || keepLocal.length < changedValues.length ? 'update' : 'unchanged',
689
- changing,
690
- resolution,
691
- keepLocal,
692
- })
693
857
  }
694
858
 
695
859
  const counts = { create: 0, update: 0, unchanged: 0, skip: 0 }
package/src/import.ts CHANGED
@@ -27,11 +27,19 @@
27
27
  *
28
28
  * Idempotence falls out of (2): the repository appends only what differs, so
29
29
  * importing the same bundle twice writes no values the second time.
30
+ *
31
+ * What a run applied to each node is recorded (`content_applied`, story 163),
32
+ * so the next import compares three ways: a field only the bundle changed is
33
+ * taken, one only the site changed is kept, and a node's name, place and order
34
+ * follow the bundle under the same rule.
30
35
  */
31
36
  import type { DocumentValue } from '@crombolo/core'
32
37
  import {
38
+ type AppliedState,
33
39
  appendCacheInstruction,
40
+ appliedValueKey,
34
41
  ComponentRepository,
42
+ ContentAppliedRepository,
35
43
  type ContentKind,
36
44
  ContentTypeRepository,
37
45
  currentSchemaState,
@@ -319,8 +327,10 @@ async function applyBundle(tx: Db, set: ContentSet, options: ImportOptions): Pro
319
327
  for (const node of ordered) {
320
328
  const plan = planByKey.get(node.key)
321
329
  const kept = new Set(plan?.keepLocal ?? [])
322
- // Every difference answered keep-local: nothing of the bundle's to write.
323
- if (plan?.action === 'unchanged' && kept.size > 0) {
330
+ // Nothing of the bundle's to write — every difference kept, or none at
331
+ // all — so no new version either. A replace still clears what the bundle
332
+ // does not carry.
333
+ if (plan?.action === 'unchanged' && !options.exact) {
324
334
  applied.push(plan)
325
335
  continue
326
336
  }
@@ -338,8 +348,10 @@ async function applyBundle(tx: Db, set: ContentSet, options: ImportOptions): Pro
338
348
  ),
339
349
  }
340
350
  : node
341
- // A node answered keep-local as a whole keeps its names too.
351
+ // A node answered keep-local as a whole keeps its names too, and a name
352
+ // changed only here since the last import stays.
342
353
  const keepNames = plan?.resolution === 'keep-local'
354
+ const keptCultures = new Set(plan?.keepNames ?? [])
343
355
  await repo.update({
344
356
  key: node.key,
345
357
  contentTypeKey: current.contentTypeKey,
@@ -357,12 +369,46 @@ async function applyBundle(tx: Db, set: ContentSet, options: ImportOptions): Pro
357
369
  : node.variants.map((v) => ({
358
370
  culture: v.culture,
359
371
  segment: v.segment,
360
- name: v.name,
372
+ name: keptCultures.has(v.culture ?? '')
373
+ ? (current.variants.find((c) => (c.culture ?? '') === (v.culture ?? ''))?.name ??
374
+ v.name)
375
+ : v.name,
361
376
  })),
362
377
  })
363
378
  if (plan) applied.push(plan)
364
379
  }
365
380
 
381
+ // A node the bundle moved, and the site did not, follows it.
382
+ for (const node of ordered) {
383
+ const moveTo = planByKey.get(node.key)?.moveTo
384
+ if (moveTo === undefined) continue
385
+ await repoFor(node.kind).move(node.key, moveTo)
386
+ }
387
+
388
+ // Nodes the bundle reordered, and the site did not, take the bundle's order
389
+ // among the siblings the bundle carries, in the slots those siblings hold.
390
+ const reorderUnder = new Set<string | null>()
391
+ for (const node of ordered)
392
+ if (planByKey.get(node.key)?.reorder && node.kind === 'document')
393
+ reorderUnder.add(node.parent)
394
+ for (const parent of reorderUnder) {
395
+ const documents = repoFor('document')
396
+ const carried = ordered.filter(
397
+ (node) =>
398
+ node.kind === 'document' && node.parent === parent && !planByKey.get(node.key)?.keepOrder,
399
+ )
400
+ const rows = await documents.nodes.byKeys(carried.map((n) => n.key))
401
+ const slots = rows.map((row) => row.sortOrder).sort((a, b) => a - b)
402
+ const wanted = [...carried].sort((a, b) => a.sortOrder - b.sortOrder)
403
+ await documents.sort(
404
+ parent,
405
+ wanted.flatMap((node, index) => {
406
+ const sortOrder = slots[index]
407
+ return sortOrder === undefined ? [] : [{ key: node.key, sortOrder }]
408
+ }),
409
+ )
410
+ }
411
+
366
412
  // People before the rules that name them, and after the content their
367
413
  // values may refer to.
368
414
  const people =
@@ -460,6 +506,33 @@ async function applyBundle(tx: Db, set: ContentSet, options: ImportOptions): Pro
460
506
  eventId: markers?.head ?? null,
461
507
  })
462
508
  }
509
+ // What this run applied, for the next import to compare against. A node
510
+ // that kept its own order keeps the order recorded before.
511
+ const appliedRecords = new ContentAppliedRepository(tx)
512
+ const earlier = await appliedRecords.byKeys(ordered.map((node) => node.key))
513
+ const rowsNow = new Map(
514
+ (await repoFor('document').nodes.byKeys(ordered.map((node) => node.key))).map((row) => [
515
+ row.key,
516
+ row,
517
+ ]),
518
+ )
519
+ for (const node of ordered) {
520
+ const row = rowsNow.get(node.key)
521
+ if (!row) continue
522
+ const state: AppliedState = {
523
+ values: Object.fromEntries(
524
+ node.values.map((v) => [appliedValueKey(v.property, v.culture, v.segment), v.value]),
525
+ ),
526
+ names: Object.fromEntries(node.variants.map((v) => [v.culture ?? '', v.name])),
527
+ parent: node.parent,
528
+ bundleSortOrder: node.sortOrder,
529
+ sortOrder: planByKey.get(node.key)?.keepOrder
530
+ ? (earlier.get(node.key)?.sortOrder ?? row.sortOrder)
531
+ : row.sortOrder,
532
+ }
533
+ await appliedRecords.record(node.key, runId, state)
534
+ }
535
+
463
536
  await runs.finish(runId, ordered.length + people.length)
464
537
 
465
538
  // Other nodes drop their published cache; this one already has.
package/src/revert.ts CHANGED
@@ -16,6 +16,7 @@
16
16
  */
17
17
  import {
18
18
  appendCacheInstruction,
19
+ ContentAppliedRepository,
19
20
  currentSchemaState,
20
21
  type Db,
21
22
  DocumentRepository,
@@ -411,6 +412,8 @@ export async function revertRun(
411
412
  eventId: markers?.head ?? null,
412
413
  })
413
414
  }
415
+ // What the next import compares against goes back with the content.
416
+ await new ContentAppliedRepository(tx).revert(runId)
414
417
  await runs.finish(newRunId, todo.length + removed.length)
415
418
  await runs.setStatus(runId, 'reverted')
416
419