feat(apply): per-record selection + deletes-before-updates ordering
RecordDiff.Key() gives a stable normalized identifier ("TYPE name.") for
every diff kind, exposed as recordView.Key. ApplyRequest now takes
Updates/Prunes key lists instead of two booleans, so callers can apply a
subset of records. service.Apply builds the applied set with selected
prunes (Delete) added before selected updates (Add/Update) — an
invariant, not an option — since the provider rejects an Add/Update
whose name still conflicts with an existing record (e.g. a CNAME cannot
be created while an A on the same name still exists).
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BwxdSt4reTm7Dj1oxRvpP3
This commit is contained in:
@@ -125,39 +125,69 @@ func TestZoneRecordsReadsProviderDirectly(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyRespectsPruneGuard(t *testing.T) {
|
||||
// зона содержит лишнюю запись b (нет в шаблоне) → Prune-кандидат
|
||||
// TestApplySelectsByKeyAndOrdersPrunesBeforeUpdates covers the two goals of
|
||||
// selective apply: (1) only diffs whose key is present in the request are
|
||||
// applied, and (2) when both an update and a prune are selected, the prune
|
||||
// (Delete) must land BEFORE the update in the applied Changeset — this is the
|
||||
// regression guard for the provider rejecting an Add/Update whose name still
|
||||
// conflicts with an existing record (e.g. a CNAME cannot be created while an
|
||||
// A on the same name still exists).
|
||||
func TestApplySelectsByKeyAndOrdersPrunesBeforeUpdates(t *testing.T) {
|
||||
// zone: a needs updating (9.9.9.9 -> 1.1.1.1), b is an extra record not in
|
||||
// the template (prune candidate).
|
||||
actual := []model.Record{
|
||||
{Type: model.A, Name: "a.example.com.", TTL: 300, Values: []string{"1.1.1.1"}},
|
||||
{Type: model.A, Name: "a.example.com.", TTL: 300, Values: []string{"9.9.9.9"}},
|
||||
{Type: model.A, Name: "b.example.com.", TTL: 300, Values: []string{"2.2.2.2"}},
|
||||
}
|
||||
tmpl := dto.TemplateDoc{Records: []dto.RecordDTO{
|
||||
{Type: "A", Name: "a.example.com.", TTL: 300, Values: []string{"1.1.1.1"}}, // in sync
|
||||
{Type: "A", Name: "a.example.com.", TTL: 300, Values: []string{"1.1.1.1"}}, // update
|
||||
}}
|
||||
|
||||
// applyPrunes=false → удаление b НЕ применяется
|
||||
const updKey = "A a.example.com."
|
||||
const pruneKey = "A b.example.com."
|
||||
|
||||
// Only the prune selected -> only the delete is applied.
|
||||
svc, fp := setup(t, actual, tmpl)
|
||||
if _, err := svc.Apply(context.Background(), uuid.New(), uuid.New(), ApplyRequest{ApplyUpdates: true, ApplyPrunes: false}); err != nil {
|
||||
if _, err := svc.Apply(context.Background(), uuid.New(), uuid.New(), ApplyRequest{Prunes: []string{pruneKey}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, d := range fp.applied.Diffs {
|
||||
if d.Kind == diff.Delete {
|
||||
t.Fatalf("prune must be skipped when ApplyPrunes=false, applied: %+v", fp.applied.Diffs)
|
||||
}
|
||||
if len(fp.applied.Diffs) != 1 || fp.applied.Diffs[0].Kind != diff.Delete {
|
||||
t.Fatalf("expected only the selected prune applied, got %+v", fp.applied.Diffs)
|
||||
}
|
||||
|
||||
// applyPrunes=true → удаление b применяется
|
||||
// Only the update selected -> only the update is applied.
|
||||
svc2, fp2 := setup(t, actual, tmpl)
|
||||
if _, err := svc2.Apply(context.Background(), uuid.New(), uuid.New(), ApplyRequest{ApplyUpdates: true, ApplyPrunes: true}); err != nil {
|
||||
if _, err := svc2.Apply(context.Background(), uuid.New(), uuid.New(), ApplyRequest{Updates: []string{updKey}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var sawDelete bool
|
||||
for _, d := range fp2.applied.Diffs {
|
||||
if d.Kind == diff.Delete && d.Name == "b.example.com." {
|
||||
sawDelete = true
|
||||
}
|
||||
if len(fp2.applied.Diffs) != 1 || fp2.applied.Diffs[0].Kind != diff.Update {
|
||||
t.Fatalf("expected only the selected update applied, got %+v", fp2.applied.Diffs)
|
||||
}
|
||||
if !sawDelete {
|
||||
t.Fatalf("prune must be applied when ApplyPrunes=true, applied: %+v", fp2.applied.Diffs)
|
||||
|
||||
// Nothing selected -> nothing applied.
|
||||
svc3, fp3 := setup(t, actual, tmpl)
|
||||
if _, err := svc3.Apply(context.Background(), uuid.New(), uuid.New(), ApplyRequest{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(fp3.applied.Diffs) != 0 {
|
||||
t.Fatalf("expected nothing applied when nothing is selected, got %+v", fp3.applied.Diffs)
|
||||
}
|
||||
|
||||
// CRITICAL: both selected -> the prune (Delete) must be applied FIRST,
|
||||
// the update SECOND. Regressing this order reintroduces the
|
||||
// CNAME/A-conflict bug where the provider rejects the update because the
|
||||
// stale conflicting record hasn't been deleted yet.
|
||||
svc4, fp4 := setup(t, actual, tmpl)
|
||||
if _, err := svc4.Apply(context.Background(), uuid.New(), uuid.New(), ApplyRequest{Updates: []string{updKey}, Prunes: []string{pruneKey}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(fp4.applied.Diffs) != 2 {
|
||||
t.Fatalf("expected both selected diffs applied, got %+v", fp4.applied.Diffs)
|
||||
}
|
||||
if fp4.applied.Diffs[0].Kind != diff.Delete {
|
||||
t.Fatalf("expected prune (Delete) FIRST in applied order, got %+v", fp4.applied.Diffs)
|
||||
}
|
||||
if fp4.applied.Diffs[1].Kind != diff.Update {
|
||||
t.Fatalf("expected update SECOND in applied order, got %+v", fp4.applied.Diffs)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user