fix(delete): stop rewriting live rows' text to say a surviving issue was deleted
`bd delete <wisp> --cascade --force` deleted the wisp, LEFT ITS DEPENDENTS
ALIVE, and then edited those dependents' neighbours to say they were gone.
After the call a surviving issue's description, notes, design or acceptance
criteria read `[deleted:bd-xxxxx]` about a row that was still in the table.
`bd wisp gc` hardcodes cascade, so this was reachable from routine
housekeeping rather than from an exotic flag combination.
The cause was one set computed twice, from different inputs:
issueops/delete_role.go FindAllDependentsInTx(ctx, tx, ids) <- all ids
issueops/delete.go FindAllDependentsInTx(ctx, tx, regularIDs) <- wisps dropped
The first produced the set whose citations were rewritten. The second produced
the set that was actually deleted. A row reachable only through a named WISP
root landed in the first and not the second, so the cascade silently
under-deleted what the caller asked for AND the rewrite corrupted text on rows
nobody had named. Two harms, one drift.
Converged on deleting the closure rather than narrowing the rewrite: that is
what --cascade promises, what issueops/deleter.go's DeleteRequest.Cascade says
the closure is ("in both planes"), and what the unit-of-work body — the other
Deleter implementation — already did through domain.deleteMany. The store body
was the outlier.
The two sets can no longer drift because there is now only one. A new
issueops.DeletionSet is resolved ONCE by ResolveDeletionSetInTx, and the same
value is handed to the neighbourhood read, to DeleteResolvedSetInTx and to
RewriteDeletedReferencesInTx. Nothing downstream recomputes it.
Two further cross-plane defects in the same neighbourhood, both found by the
new cases rather than by reading:
- The store body's dependents guard read the DURABLE half of the request
only, so a wisp named with a durable dependent was neither refused when
unforced nor reported as orphaning when forced. It now asks about every
named id in both planes, which is what the leaf always said and what the
uow body always did. The storage-seam guard inside DeleteIssuesInTx stays
durable-only on purpose and says why: the server-backed store peels wisps
off before reaching it, so widening it there would make the two stores
refuse differently.
- The uow body left a DANGLING edge: there is no foreign key from
dependencies to wisps, so deleting a wisp did not remove the sync-plane
rows pointing at it. Force promises the dependent is orphaned, not that it
keeps an edge into a row that no longer exists. The store body has always
deleted those rows explicitly; domain.deleteMany now does too.
Three conformance cases pin the quadrant, on all three wirings. All three were
RED on the store legs before this change:
CascadeFromAWispRootDeletesTheClosure
"Deleted = 1, want 3 — the wisp, the durable row that depends on it,
and ITS dependent"
GuardsAWispNamedWithADurableDependent
"Delete(dryRun=false) unforced over a wisp with a durable dependent:
error = <nil>, want ErrDependentsOutsideRequest"
NeverCallsALiveRowDeleted
"del-live-dependent STILL EXISTS and the survivor's description calls it
deleted: \"unblocks [deleted:del-live-dependent], tracked under
[deleted:del-live-root]\" — the rewrite set is wider than the deletion set"
The third is the one that makes this class hard to reintroduce: it reads a
surviving row's stored text back and requires every `[deleted:<id>]` marker to
name a row that is really gone, and every id that is still there to still be
named verbatim. No count can see that — an implementation whose rewrite set is
a superset of its deletion set reports a perfectly plausible
ReferencesUpdated.
The suite was 59 cases green on three legs while two of them contradicted the
third: no existing case seeded a cross-plane dependent edge. ErasesAcrossBothPlanes
used an edge-free issue/wisp pair and every cascade and guard case was
durable-only.
This commit is contained in:
parent
8729c18edd
commit
903cbe0313
8 changed files with 365 additions and 71 deletions
|
|
@ -258,6 +258,162 @@ func RunDeleterCascadeDeletesTheClosure(t *testing.T, ctx context.Context, fixtu
|
|||
deleterAssertIssueRows(t, ctx, fixture, 1, bystander)
|
||||
}
|
||||
|
||||
// RunDeleterCascadeFromAWispRootDeletesTheClosure pins the CROSS-PLANE quadrant
|
||||
// of the cascade (issueops/deleter.go, DeleteRequest.Cascade: "also deletes the
|
||||
// TRANSITIVE CLOSURE of everything that depends on the named rows, IN BOTH
|
||||
// PLANES").
|
||||
//
|
||||
// RunDeleterCascadeDeletesTheClosure above proves the closure over a durable
|
||||
// chain and ErasesAcrossBothPlanes proves a mixed request over an EDGE-FREE
|
||||
// pair, so between them nothing asserted what a cascade rooted at a WISP does
|
||||
// to the durable rows hanging off it. That gap is not academic: `bd wisp gc`
|
||||
// deletes wisps with cascade hardcoded, so this is the quadrant routine
|
||||
// housekeeping runs through.
|
||||
//
|
||||
// The chain is two edges deep on purpose, and its second link is durable, so
|
||||
// this cannot be satisfied by a body that merely follows one cross-plane edge.
|
||||
func RunDeleterCascadeFromAWispRootDeletesTheClosure(t *testing.T, ctx context.Context, fixture DeleterFixture) {
|
||||
t.Helper()
|
||||
root := deleterSeedWisp(t, ctx, fixture, "wcasc", "root")
|
||||
dependent := deleterSeedIssue(t, ctx, fixture, "wcasc", "dependent")
|
||||
grandchild := deleterSeedIssue(t, ctx, fixture, "wcasc", "grandchild")
|
||||
bystander := deleterSeedIssue(t, ctx, fixture, "wcasc", "bystander")
|
||||
deleterAddEdge(t, ctx, fixture, dependent, root)
|
||||
deleterAddEdge(t, ctx, fixture, grandchild, dependent)
|
||||
|
||||
result := deleterDelete(t, ctx, fixture, publicops.DeleteRequest{
|
||||
IDs: []string{root},
|
||||
Cascade: true,
|
||||
Force: true,
|
||||
})
|
||||
if result.Deleted != 3 {
|
||||
t.Errorf("Deleted = %d, want 3 — the wisp, the durable row that depends on it, and ITS dependent", result.Deleted)
|
||||
}
|
||||
deleterAssertWispRows(t, ctx, fixture, 0, root)
|
||||
deleterAssertIssueRows(t, ctx, fixture, 0, dependent, grandchild)
|
||||
deleterAssertIssueRows(t, ctx, fixture, 1, bystander)
|
||||
}
|
||||
|
||||
// RunDeleterGuardsAWispNamedWithADurableDependent pins that the dependents
|
||||
// guard has NO WISP EXEMPTION (issueops/deleter.go, DeleteRequest.Force:
|
||||
// "WITHOUT Cascade AND WITHOUT Force, a NAMED ROW that some row OUTSIDE the
|
||||
// request depends on is refused" — a named row, not a named durable row).
|
||||
//
|
||||
// RunDeleterRefusesDependentsOutsideTheRequest and
|
||||
// RunDeleterForceOrphansDependents are both durable-only, so a body that
|
||||
// partitioned the request and asked the guard only about the durable half
|
||||
// passed them while silently orphaning a workspace's graph through the other
|
||||
// half. Both halves of the clause are asserted here — the unforced refusal and
|
||||
// the forced orphan report — because a body that reported the orphan without
|
||||
// refusing, or refused without reporting, is wrong in a different direction
|
||||
// and the two share one scan.
|
||||
func RunDeleterGuardsAWispNamedWithADurableDependent(t *testing.T, ctx context.Context, fixture DeleterFixture) {
|
||||
t.Helper()
|
||||
wisp := deleterSeedWisp(t, ctx, fixture, "wguard", "wisp")
|
||||
dependent := deleterSeedIssue(t, ctx, fixture, "wguard", "dependent")
|
||||
deleterAddEdge(t, ctx, fixture, dependent, wisp)
|
||||
|
||||
for _, dryRun := range []bool{false, true} {
|
||||
result, err := fixture.Deleter.Delete(ctx, publicops.DeleteRequest{
|
||||
IDs: []string{wisp},
|
||||
DryRun: dryRun,
|
||||
})
|
||||
if !errors.Is(err, publicops.ErrDependentsOutsideRequest) {
|
||||
t.Fatalf("Delete(dryRun=%v) unforced over a wisp with a durable dependent: error = %v, want ErrDependentsOutsideRequest", dryRun, err)
|
||||
}
|
||||
var blocked *publicops.DependentsOutsideRequestError
|
||||
if !errors.As(err, &blocked) {
|
||||
t.Fatalf("Delete(dryRun=%v) error = %v, want *DependentsOutsideRequestError", dryRun, err)
|
||||
}
|
||||
if blocked.IssueID != wisp {
|
||||
t.Errorf("DependentsOutsideRequestError.IssueID = %q, want the named wisp %q", blocked.IssueID, wisp)
|
||||
}
|
||||
if !reflect.DeepEqual(blocked.Dependents, []string{dependent}) {
|
||||
t.Errorf("DependentsOutsideRequestError.Dependents = %v, want [%s]", blocked.Dependents, dependent)
|
||||
}
|
||||
if result.Deleted != 0 {
|
||||
t.Errorf("refused delete reported Deleted = %d, want 0", result.Deleted)
|
||||
}
|
||||
deleterAssertWispRows(t, ctx, fixture, 1, wisp)
|
||||
deleterAssertIssueRows(t, ctx, fixture, 1, dependent)
|
||||
}
|
||||
|
||||
result := deleterDelete(t, ctx, fixture, publicops.DeleteRequest{
|
||||
IDs: []string{wisp},
|
||||
Force: true,
|
||||
})
|
||||
if result.Deleted != 1 {
|
||||
t.Errorf("Deleted = %d, want 1 — force deletes the NAMED wisp and nothing else", result.Deleted)
|
||||
}
|
||||
if !reflect.DeepEqual(result.Orphaned, []string{dependent}) {
|
||||
t.Errorf("Orphaned = %v, want [%s] — the cross-plane edge orphans a durable row too", result.Orphaned, dependent)
|
||||
}
|
||||
deleterAssertWispRows(t, ctx, fixture, 0, wisp)
|
||||
deleterAssertIssueRows(t, ctx, fixture, 1, dependent)
|
||||
// The orphan loses its cross-plane edge with the wisp it pointed at; a
|
||||
// surviving edge into a deleted wisp would be dangling rather than orphaned.
|
||||
deleterAssertEdgeRows(t, ctx, fixture, 0, dependent, wisp)
|
||||
}
|
||||
|
||||
// RunDeleterNeverCallsALiveRowDeleted pins the invariant that makes the
|
||||
// under-deleting cascade impossible to reintroduce quietly: THE SET WHOSE
|
||||
// CITATIONS ARE REWRITTEN IS THE SET THAT WAS DELETED. A `[deleted:<id>]`
|
||||
// marker in a surviving row is a claim about the workspace, and this case
|
||||
// checks the claim against the rows.
|
||||
//
|
||||
// It is written as a biconditional rather than a count, because the counts
|
||||
// cannot see it: an implementation whose rewrite set is a strict SUPERSET of
|
||||
// its deletion set reports a perfectly plausible ReferencesUpdated and leaves
|
||||
// a neighbor's description saying a LIVE issue is gone. No other case here
|
||||
// reads a surviving row's text back and compares it against what is actually
|
||||
// stored.
|
||||
//
|
||||
// The fixture is the cheapest arrangement in which the two sets can differ: a
|
||||
// wisp root, a durable row reachable only through it, and a third row that the
|
||||
// durable one DEPENDS ON — so the third row is a graph neighbor of the deletion
|
||||
// set (its text is in scope for the rewrite) without being a dependent of
|
||||
// anything (so it is never in the closure and always survives).
|
||||
func RunDeleterNeverCallsALiveRowDeleted(t *testing.T, ctx context.Context, fixture DeleterFixture) {
|
||||
t.Helper()
|
||||
root := deleterSeedWisp(t, ctx, fixture, "live", "root")
|
||||
dependent := deleterSeedIssue(t, ctx, fixture, "live", "dependent")
|
||||
|
||||
survivor := deleterIssue(fixture, "live", "survivor", false)
|
||||
survivor.Description = "unblocks " + dependent + ", tracked under " + root
|
||||
deleterSeed(t, ctx, fixture, survivor)
|
||||
deleterAddEdge(t, ctx, fixture, dependent, root)
|
||||
deleterAddEdge(t, ctx, fixture, dependent, survivor.ID)
|
||||
|
||||
deleterDelete(t, ctx, fixture, publicops.DeleteRequest{
|
||||
IDs: []string{root},
|
||||
Cascade: true,
|
||||
Force: true,
|
||||
Actor: "deleter-contract",
|
||||
})
|
||||
|
||||
deleterAssertIssueRows(t, ctx, fixture, 1, survivor.ID)
|
||||
text := deleterText(t, ctx, fixture, survivor.ID, "description")
|
||||
for _, cited := range []struct {
|
||||
id string
|
||||
table string
|
||||
}{
|
||||
{dependent, "issues"},
|
||||
{root, "wisps"},
|
||||
} {
|
||||
marker := "[deleted:" + cited.id + "]"
|
||||
gone := deleterRowCount(t, ctx, fixture, cited.table, cited.id) == 0
|
||||
switch {
|
||||
case gone && !strings.Contains(text, marker):
|
||||
t.Errorf("%s is gone but the survivor's description still cites it verbatim: %q", cited.id, text)
|
||||
case !gone && strings.Contains(text, marker):
|
||||
t.Errorf("%s STILL EXISTS and the survivor's description calls it deleted: %q — "+
|
||||
"the rewrite set is wider than the deletion set", cited.id, text)
|
||||
case !gone && !strings.Contains(text, cited.id):
|
||||
t.Errorf("%s still exists but the survivor's description no longer names it: %q", cited.id, text)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// RunDeleterErasesAcrossBothPlanes pins that one request reaches BOTH tiers
|
||||
// (issueops/deleter.go, DeleteRequest.IDs: "in either plane").
|
||||
//
|
||||
|
|
@ -565,6 +721,21 @@ func deleterAssertWispRows(t *testing.T, ctx context.Context, fixture DeleterFix
|
|||
}
|
||||
}
|
||||
|
||||
// deleterRowCount REPORTS how many rows one id has in one plane instead of
|
||||
// asserting a number, for the case whose assertion depends on which answer the
|
||||
// implementation gave.
|
||||
//
|
||||
//nolint:gosec // G201: table is chosen by the caller from the two plane tables.
|
||||
func deleterRowCount(t *testing.T, ctx context.Context, fixture DeleterFixture, table, id string) int {
|
||||
t.Helper()
|
||||
var got int
|
||||
query := fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE id = ?", table)
|
||||
if err := fixture.QueryScalar(ctx, query, []any{id}, &got); err != nil {
|
||||
t.Fatalf("counting %s rows for %s: %v", table, id, err)
|
||||
}
|
||||
return got
|
||||
}
|
||||
|
||||
// deleterAssertEdgeRows counts the stored edges from dependent to blocker.
|
||||
func deleterAssertEdgeRows(t *testing.T, ctx context.Context, fixture DeleterFixture, want int, dependent, blocker string) {
|
||||
t.Helper()
|
||||
|
|
|
|||
|
|
@ -40,6 +40,15 @@ func TestDeleterContract(t *testing.T) {
|
|||
t.Run("ErasesAcrossBothPlanes", func(t *testing.T) {
|
||||
conformance.RunDeleterErasesAcrossBothPlanes(t, ctx, fixture)
|
||||
})
|
||||
t.Run("CascadeFromAWispRootDeletesTheClosure", func(t *testing.T) {
|
||||
conformance.RunDeleterCascadeFromAWispRootDeletesTheClosure(t, ctx, fixture)
|
||||
})
|
||||
t.Run("GuardsAWispNamedWithADurableDependent", func(t *testing.T) {
|
||||
conformance.RunDeleterGuardsAWispNamedWithADurableDependent(t, ctx, fixture)
|
||||
})
|
||||
t.Run("NeverCallsALiveRowDeleted", func(t *testing.T) {
|
||||
conformance.RunDeleterNeverCallsALiveRowDeleted(t, ctx, fixture)
|
||||
})
|
||||
t.Run("CollapsesDuplicateIDs", func(t *testing.T) {
|
||||
conformance.RunDeleterCollapsesDuplicateIDs(t, ctx, fixture)
|
||||
})
|
||||
|
|
|
|||
|
|
@ -130,6 +130,17 @@ func (u *issueUseCaseImpl) deleteMany(ctx context.Context, params DeleteIssuesPa
|
|||
if _, err := u.depRepo.DeleteAllForIDs(ctx, wispIDs, DepInsertOpts{UseWispsTable: true}); err != nil {
|
||||
return result, fmt.Errorf("delete: drop wisp deps: %w", err)
|
||||
}
|
||||
// The SYNC-PLANE edges pointing at a deleted wisp, which are not the same
|
||||
// rows as the line above and are not reached by a foreign key: there is no
|
||||
// FK from dependencies to wisps, so `dependencies.depends_on_wisp_id` rows
|
||||
// survive their target unless they are deleted explicitly. Without this a
|
||||
// forced delete of a wisp left its durable dependent holding an edge into
|
||||
// a row that no longer exists — dangling, not orphaned, which is not what
|
||||
// issueops.DeleteRequest.Force promises. The store body has always done
|
||||
// this (issueops.deleteIssueRowInTx -> DeleteWispFromDependenciesInTx).
|
||||
if _, err := u.depRepo.DeleteAllForIDs(ctx, wispIDs, DepInsertOpts{}); err != nil {
|
||||
return result, fmt.Errorf("delete: drop sync-plane edges into deleted wisps: %w", err)
|
||||
}
|
||||
if _, err := u.labelRepo.DeleteAllForIDs(ctx, regularIDs, LabelOpts{}); err != nil {
|
||||
return result, fmt.Errorf("delete: drop labels: %w", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,6 +41,15 @@ func TestDeleterContract(t *testing.T) {
|
|||
t.Run("ErasesAcrossBothPlanes", func(t *testing.T) {
|
||||
conformance.RunDeleterErasesAcrossBothPlanes(t, ctx, fixture)
|
||||
})
|
||||
t.Run("CascadeFromAWispRootDeletesTheClosure", func(t *testing.T) {
|
||||
conformance.RunDeleterCascadeFromAWispRootDeletesTheClosure(t, ctx, fixture)
|
||||
})
|
||||
t.Run("GuardsAWispNamedWithADurableDependent", func(t *testing.T) {
|
||||
conformance.RunDeleterGuardsAWispNamedWithADurableDependent(t, ctx, fixture)
|
||||
})
|
||||
t.Run("NeverCallsALiveRowDeleted", func(t *testing.T) {
|
||||
conformance.RunDeleterNeverCallsALiveRowDeleted(t, ctx, fixture)
|
||||
})
|
||||
t.Run("CollapsesDuplicateIDs", func(t *testing.T) {
|
||||
conformance.RunDeleterCollapsesDuplicateIDs(t, ctx, fixture)
|
||||
})
|
||||
|
|
|
|||
|
|
@ -73,48 +73,98 @@ func deleteIssueRowInTx(ctx context.Context, tx *sql.Tx, id string, isWisp bool)
|
|||
return nil
|
||||
}
|
||||
|
||||
//nolint:gosec // G201: inClause contains only ? placeholders
|
||||
// DeletionSet is the EXACT set of rows one delete removes, split by the plane
|
||||
// each row lives in.
|
||||
//
|
||||
// IT EXISTS BECAUSE THE SET USED TO BE COMPUTED TWICE. DeleteInTx expanded the
|
||||
// cascade from every named id while DeleteIssuesInTx expanded it from the
|
||||
// DURABLE named ids only, so `bd delete <wisp> --cascade` left a durable row
|
||||
// reachable only through that wisp alive and then rewrote its neighbors' text
|
||||
// to say `[deleted:<id>]` about it. Resolving once and passing the value down
|
||||
// is what makes that class of drift impossible rather than merely fixed: the
|
||||
// neighborhood read, the deletion and the reference rewrite all take THIS
|
||||
// value.
|
||||
type DeletionSet struct {
|
||||
// WispIDs and RegularIDs partition All by plane. The two tiers are deleted
|
||||
// through different tables and their associated rows counted from
|
||||
// different ones, so the split is carried rather than recomputed.
|
||||
WispIDs []string
|
||||
RegularIDs []string
|
||||
// All is the whole set in one slice. It is what a caller scopes a
|
||||
// neighborhood read or a citation rewrite to, and it is the answer to
|
||||
// "which rows is this delete about" that nothing else may recompute.
|
||||
All []string
|
||||
}
|
||||
|
||||
// ResolveDeletionSetInTx decides WHICH rows a delete removes: the named ids,
|
||||
// plus — under cascade — the transitive closure of everything that depends on
|
||||
// them, in BOTH planes.
|
||||
//
|
||||
// THE CASCADE IS ROOTED AT EVERY NAMED ID, WISPS INCLUDED. Rooting it at the
|
||||
// durable half is what made `bd wisp gc` (which hardcodes cascade) silently
|
||||
// under-delete, and it is the half of the drift that cost rows rather than
|
||||
// text. It matches what DeleteRequest.Cascade promises — "the TRANSITIVE
|
||||
// CLOSURE of everything that depends on the named rows, in both planes" — and
|
||||
// what domain.issueUseCase.deleteMany, the other Deleter body, already did.
|
||||
//
|
||||
// It does not read the caller's slice destructively: the non-cascade set is a
|
||||
// copy, because DeleteRequest promises IDs is never sorted in place.
|
||||
func ResolveDeletionSetInTx(ctx context.Context, tx DBTX, ids []string, cascade bool) (DeletionSet, error) {
|
||||
all := append([]string(nil), ids...)
|
||||
if cascade {
|
||||
closure, err := FindAllDependentsInTx(ctx, tx, ids)
|
||||
if err != nil {
|
||||
return DeletionSet{}, fmt.Errorf("expand cascade: %w", err)
|
||||
}
|
||||
all = workapi.SortedDeleteIDs(closure)
|
||||
}
|
||||
if len(all) == 0 {
|
||||
return DeletionSet{}, nil
|
||||
}
|
||||
wispIDs, regularIDs, err := PartitionWispIDsInTx(ctx, tx, all)
|
||||
if err != nil {
|
||||
return DeletionSet{}, fmt.Errorf("partition delete ids: %w", err)
|
||||
}
|
||||
return DeletionSet{WispIDs: wispIDs, RegularIDs: regularIDs, All: all}, nil
|
||||
}
|
||||
|
||||
func DeleteIssuesInTx(ctx context.Context, tx *sql.Tx, ids []string, cascade bool, force bool, dryRun bool) (*types.DeleteIssuesResult, error) {
|
||||
if len(ids) == 0 {
|
||||
return &types.DeleteIssuesResult{}, nil
|
||||
}
|
||||
|
||||
initialWispIDs, regularIDs, err := PartitionWispIDsInTx(ctx, tx, ids)
|
||||
set, err := ResolveDeletionSetInTx(ctx, tx, ids, cascade)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
idSet := make(map[string]bool, len(ids))
|
||||
for _, id := range ids {
|
||||
idSet[id] = true
|
||||
}
|
||||
|
||||
result := &types.DeleteIssuesResult{}
|
||||
|
||||
expandedRegularIDs := regularIDs
|
||||
if cascade {
|
||||
allToDelete, err := FindAllDependentsInTx(ctx, tx, regularIDs)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("find dependents: %w", err)
|
||||
var orphaned []string
|
||||
if !cascade {
|
||||
// Without cascade the set is exactly the named ids, so set.RegularIDs
|
||||
// is their durable half.
|
||||
//
|
||||
// The guard here is the STORAGE SEAM's, and it stays durable-only: the
|
||||
// server-backed store peels wisps off before it ever reaches this
|
||||
// function (dolt/issues.go DeleteIssues), so widening it would make the
|
||||
// embedded store refuse where the server-backed one cannot. The ROLE's
|
||||
// guard, which does cover both planes, is in DeleteInTx.
|
||||
idSet := make(map[string]bool, len(ids))
|
||||
for _, id := range ids {
|
||||
idSet[id] = true
|
||||
}
|
||||
expandedRegularIDs = make([]string, 0, len(allToDelete))
|
||||
for id := range allToDelete {
|
||||
expandedRegularIDs = append(expandedRegularIDs, id)
|
||||
}
|
||||
} else {
|
||||
// One scan of the dependency planes answers both modes: the guard
|
||||
// needs to know WHICH id is blocked, and the forced path needs the
|
||||
// union of what it orphans. ExternalDependentsBySourceInTx gives the
|
||||
// per-source shape both read.
|
||||
external, err := ExternalDependentsBySourceInTx(ctx, tx, regularIDs, idSet)
|
||||
external, err := ExternalDependentsBySourceInTx(ctx, tx, set.RegularIDs, idSet)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get dependents: %w", err)
|
||||
}
|
||||
if !force {
|
||||
for _, id := range regularIDs {
|
||||
for _, id := range set.RegularIDs {
|
||||
if deps := external[id]; len(deps) > 0 {
|
||||
result.OrphanedIssues = deps
|
||||
return result, &publicops.DependentsOutsideRequestError{IssueID: id, Dependents: deps}
|
||||
return &types.DeleteIssuesResult{OrphanedIssues: deps},
|
||||
&publicops.DependentsOutsideRequestError{IssueID: id, Dependents: deps}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
|
@ -124,58 +174,73 @@ func DeleteIssuesInTx(ctx context.Context, tx *sql.Tx, ids []string, cascade boo
|
|||
orphans[id] = true
|
||||
}
|
||||
}
|
||||
result.OrphanedIssues = workapi.SortedDeleteIDs(orphans)
|
||||
orphaned = workapi.SortedDeleteIDs(orphans)
|
||||
}
|
||||
}
|
||||
|
||||
cascadeWispIDs, finalRegularIDs, err := PartitionWispIDsInTx(ctx, tx, expandedRegularIDs)
|
||||
result, err := DeleteResolvedSetInTx(ctx, tx, set, dryRun)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("partition expanded delete IDs: %w", err)
|
||||
return nil, err
|
||||
}
|
||||
result.OrphanedIssues = orphaned
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// DeleteResolvedSetInTx deletes EXACTLY set — no expansion, no re-partition,
|
||||
// no guard — and reports the associated rows that went with it.
|
||||
//
|
||||
// It is split out of DeleteIssuesInTx so the role body can read the
|
||||
// neighborhood BEFORE the delete and rewrite it AFTER against the SAME
|
||||
// DeletionSet the delete was handed. A caller that recomputed either end of
|
||||
// that instead is the bug DeletionSet documents.
|
||||
//
|
||||
//nolint:gosec // G201: inClause contains only ? placeholders
|
||||
func DeleteResolvedSetInTx(ctx context.Context, tx *sql.Tx, set DeletionSet, dryRun bool) (*types.DeleteIssuesResult, error) {
|
||||
result := &types.DeleteIssuesResult{}
|
||||
if len(set.All) == 0 {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
allWispIDs := append(append([]string{}, initialWispIDs...), cascadeWispIDs...)
|
||||
allDeletedSet := make(map[string]bool, len(finalRegularIDs)+len(allWispIDs))
|
||||
for _, id := range finalRegularIDs {
|
||||
allDeletedSet[id] = true
|
||||
}
|
||||
for _, id := range allWispIDs {
|
||||
allDeletedSet[id] = true
|
||||
deletedSet := make(map[string]bool, len(set.All))
|
||||
for _, id := range set.All {
|
||||
deletedSet[id] = true
|
||||
}
|
||||
|
||||
var depsCount, labelsCount, eventsCount int
|
||||
if depsCount, err = CountRowsForIssueIDsInTx(ctx, tx, "dependencies", finalRegularIDs); err != nil {
|
||||
var err error
|
||||
if depsCount, err = CountRowsForIssueIDsInTx(ctx, tx, "dependencies", set.RegularIDs); err != nil {
|
||||
return nil, fmt.Errorf("count dependencies: %w", err)
|
||||
}
|
||||
wispDepsCount, err := CountRowsForIssueIDsInTx(ctx, tx, "wisp_dependencies", cascadeWispIDs)
|
||||
wispDepsCount, err := CountRowsForIssueIDsInTx(ctx, tx, "wisp_dependencies", set.WispIDs)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("count wisp dependencies: %w", err)
|
||||
}
|
||||
depsCount += wispDepsCount
|
||||
|
||||
if labelsCount, err = CountRowsForIssueIDsInTx(ctx, tx, "labels", finalRegularIDs); err != nil {
|
||||
if labelsCount, err = CountRowsForIssueIDsInTx(ctx, tx, "labels", set.RegularIDs); err != nil {
|
||||
return nil, fmt.Errorf("count labels: %w", err)
|
||||
}
|
||||
wispLabelsCount, err := CountRowsForIssueIDsInTx(ctx, tx, "wisp_labels", cascadeWispIDs)
|
||||
wispLabelsCount, err := CountRowsForIssueIDsInTx(ctx, tx, "wisp_labels", set.WispIDs)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("count wisp labels: %w", err)
|
||||
}
|
||||
labelsCount += wispLabelsCount
|
||||
|
||||
if eventsCount, err = CountRowsForIssueIDsInTx(ctx, tx, "events", finalRegularIDs); err != nil {
|
||||
if eventsCount, err = CountRowsForIssueIDsInTx(ctx, tx, "events", set.RegularIDs); err != nil {
|
||||
return nil, fmt.Errorf("count events: %w", err)
|
||||
}
|
||||
wispEventsCount, err := CountRowsForIssueIDsInTx(ctx, tx, "wisp_events", cascadeWispIDs)
|
||||
wispEventsCount, err := CountRowsForIssueIDsInTx(ctx, tx, "wisp_events", set.WispIDs)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("count wisp events: %w", err)
|
||||
}
|
||||
eventsCount += wispEventsCount
|
||||
|
||||
for i := 0; i < len(expandedRegularIDs); i += deleteBatchSize {
|
||||
for i := 0; i < len(set.All); i += deleteBatchSize {
|
||||
end := i + deleteBatchSize
|
||||
if end > len(expandedRegularIDs) {
|
||||
end = len(expandedRegularIDs)
|
||||
if end > len(set.All) {
|
||||
end = len(set.All)
|
||||
}
|
||||
batch := expandedRegularIDs[i:end]
|
||||
batch := set.All[i:end]
|
||||
batchInClause, batchArgs := buildSQLInClause(batch)
|
||||
|
||||
for _, depTable := range []string{"dependencies", "wisp_dependencies"} {
|
||||
|
|
@ -194,7 +259,7 @@ func DeleteIssuesInTx(ctx context.Context, tx *sql.Tx, ids []string, cascade boo
|
|||
_ = rows.Close()
|
||||
return nil, fmt.Errorf("scan inbound dependency: %w", err)
|
||||
}
|
||||
if !allDeletedSet[issID] {
|
||||
if !deletedSet[issID] {
|
||||
depsCount++
|
||||
}
|
||||
}
|
||||
|
|
@ -208,30 +273,30 @@ func DeleteIssuesInTx(ctx context.Context, tx *sql.Tx, ids []string, cascade boo
|
|||
result.DependenciesCount = depsCount
|
||||
result.LabelsCount = labelsCount
|
||||
result.EventsCount = eventsCount
|
||||
result.DeletedCount = len(finalRegularIDs) + len(allWispIDs)
|
||||
result.DeletedCount = len(set.RegularIDs) + len(set.WispIDs)
|
||||
|
||||
if dryRun {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
affectedIssues, affectedWisps, aerr := AffectedByDeletionInTx(ctx, tx, finalRegularIDs, allWispIDs)
|
||||
affectedIssues, affectedWisps, aerr := AffectedByDeletionInTx(ctx, tx, set.RegularIDs, set.WispIDs)
|
||||
if aerr != nil {
|
||||
return nil, fmt.Errorf("affected by batch delete: %w", aerr)
|
||||
}
|
||||
|
||||
for _, id := range allWispIDs {
|
||||
for _, id := range set.WispIDs {
|
||||
if err := deleteIssueRowInTx(ctx, tx, id, true); err != nil {
|
||||
return nil, fmt.Errorf("delete wisp %s: %w", id, err)
|
||||
}
|
||||
}
|
||||
|
||||
totalRegularsDeleted := 0
|
||||
for i := 0; i < len(finalRegularIDs); i += deleteBatchSize {
|
||||
for i := 0; i < len(set.RegularIDs); i += deleteBatchSize {
|
||||
end := i + deleteBatchSize
|
||||
if end > len(finalRegularIDs) {
|
||||
end = len(finalRegularIDs)
|
||||
if end > len(set.RegularIDs) {
|
||||
end = len(set.RegularIDs)
|
||||
}
|
||||
batch := finalRegularIDs[i:end]
|
||||
batch := set.RegularIDs[i:end]
|
||||
batchInClause, batchArgs := buildSQLInClause(batch)
|
||||
|
||||
deleteResult, err := tx.ExecContext(ctx,
|
||||
|
|
@ -250,7 +315,7 @@ func DeleteIssuesInTx(ctx context.Context, tx *sql.Tx, ids []string, cascade boo
|
|||
return nil, fmt.Errorf("delete leases: %w", err)
|
||||
}
|
||||
}
|
||||
result.DeletedCount = totalRegularsDeleted + len(allWispIDs)
|
||||
result.DeletedCount = totalRegularsDeleted + len(set.WispIDs)
|
||||
|
||||
if err := RecomputeIsBlockedInTx(ctx, tx, affectedIssues, affectedWisps); err != nil {
|
||||
return nil, fmt.Errorf("recompute is_blocked after batch delete: %w", err)
|
||||
|
|
|
|||
|
|
@ -73,9 +73,15 @@ func DeleteInTx(ctx context.Context, tx *sql.Tx, req publicops.DeleteRequest) (p
|
|||
// The guard runs only when the request did not already say what to do
|
||||
// about dependents. Under Cascade there is nothing outside the set by
|
||||
// construction, which is why the expansion below is not asked about it.
|
||||
//
|
||||
// IT ASKS ABOUT EVERY NAMED ID, IN BOTH PLANES. It used to ask only about
|
||||
// the durable half, so a wisp named with a durable dependent was neither
|
||||
// refused unforced nor reported as orphaning when forced, and the
|
||||
// cross-plane edge it removed went uncounted. The leaf says "a NAMED ROW
|
||||
// that some row OUTSIDE the request depends on is refused" with no wisp
|
||||
// exemption, and the unit-of-work body has always read it that way.
|
||||
if !req.Cascade {
|
||||
_, regularIDs := partitionByWispSet(ids, wispSet)
|
||||
external, err := ExternalDependentsBySourceInTx(ctx, tx, regularIDs, idSet)
|
||||
external, err := ExternalDependentsBySourceInTx(ctx, tx, ids, idSet)
|
||||
if err != nil {
|
||||
return publicops.DeleteResult{}, fmt.Errorf("delete: check dependents: %w", err)
|
||||
}
|
||||
|
|
@ -101,31 +107,34 @@ func DeleteInTx(ctx context.Context, tx *sql.Tx, req publicops.DeleteRequest) (p
|
|||
}
|
||||
}
|
||||
|
||||
// THE DELETION SET IS RESOLVED ONCE, HERE, and the same value reaches all
|
||||
// three of the reads and writes that must agree about it: the
|
||||
// neighborhood read, the deletion, and the citation rewrite.
|
||||
//
|
||||
// It used to be resolved twice — once here for the rewrite from ALL ids,
|
||||
// once inside DeleteIssuesInTx for the deletion from the DURABLE ids — and
|
||||
// the two drifted. A cascade rooted at a wisp put the wisp's dependents in
|
||||
// the rewrite set and not in the delete, so those rows survived and their
|
||||
// neighbors' descriptions were rewritten to call them deleted. See
|
||||
// DeletionSet.
|
||||
set, err := ResolveDeletionSetInTx(ctx, tx, ids, req.Cascade)
|
||||
if err != nil {
|
||||
return publicops.DeleteResult{}, fmt.Errorf("delete: %w", err)
|
||||
}
|
||||
|
||||
// The neighborhood is read BEFORE the deletion, because after it the
|
||||
// edges that identify a neighbor are gone. It is read against the whole
|
||||
// deletion set — the cascade closure, not just the named ids — so a row
|
||||
// citing a cascade-deleted id is rewritten too.
|
||||
deletionSet := ids
|
||||
if req.Cascade {
|
||||
closure, err := FindAllDependentsInTx(ctx, tx, ids)
|
||||
if err != nil {
|
||||
return publicops.DeleteResult{}, fmt.Errorf("delete: expand cascade: %w", err)
|
||||
}
|
||||
deletionSet = make([]string, 0, len(closure))
|
||||
for id := range closure {
|
||||
deletionSet = append(deletionSet, id)
|
||||
}
|
||||
}
|
||||
neighbors, err := deleteNeighborsInTx(ctx, tx, deletionSet)
|
||||
neighbors, err := deleteNeighborsInTx(ctx, tx, set.All)
|
||||
if err != nil {
|
||||
return publicops.DeleteResult{}, err
|
||||
}
|
||||
|
||||
// force=true unconditionally: this body has ALREADY answered the guard
|
||||
// question above and holds the orphan list it produced, so letting the
|
||||
// shared batch delete ask it a second time would be one more scan of the
|
||||
// dependency tables on the path that is about to write.
|
||||
deleted, err := DeleteIssuesInTx(ctx, tx, ids, req.Cascade, true, req.DryRun)
|
||||
// No guard argument to pass: this body has ALREADY answered the guard
|
||||
// question above and holds the orphan list it produced, and
|
||||
// DeleteResolvedSetInTx deletes what it is handed without asking again.
|
||||
deleted, err := DeleteResolvedSetInTx(ctx, tx, set, req.DryRun)
|
||||
if err != nil {
|
||||
return publicops.DeleteResult{}, err
|
||||
}
|
||||
|
|
@ -138,7 +147,10 @@ func DeleteInTx(ctx context.Context, tx *sql.Tx, req publicops.DeleteRequest) (p
|
|||
return result, nil
|
||||
}
|
||||
|
||||
rewritten, err := RewriteDeletedReferencesInTx(ctx, tx, deletionSet, neighbors, req.Actor)
|
||||
// set.All, not a set recomputed here: a `[deleted:<id>]` marker is a claim
|
||||
// that the row is gone, and the only set that can honestly back that claim
|
||||
// is the one DeleteResolvedSetInTx was handed.
|
||||
rewritten, err := RewriteDeletedReferencesInTx(ctx, tx, set.All, neighbors, req.Actor)
|
||||
if err != nil {
|
||||
return publicops.DeleteResult{}, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,6 +44,15 @@ func TestDeleterContract(t *testing.T) {
|
|||
t.Run("ErasesAcrossBothPlanes", func(t *testing.T) {
|
||||
conformance.RunDeleterErasesAcrossBothPlanes(t, ctx, fixture)
|
||||
})
|
||||
t.Run("CascadeFromAWispRootDeletesTheClosure", func(t *testing.T) {
|
||||
conformance.RunDeleterCascadeFromAWispRootDeletesTheClosure(t, ctx, fixture)
|
||||
})
|
||||
t.Run("GuardsAWispNamedWithADurableDependent", func(t *testing.T) {
|
||||
conformance.RunDeleterGuardsAWispNamedWithADurableDependent(t, ctx, fixture)
|
||||
})
|
||||
t.Run("NeverCallsALiveRowDeleted", func(t *testing.T) {
|
||||
conformance.RunDeleterNeverCallsALiveRowDeleted(t, ctx, fixture)
|
||||
})
|
||||
t.Run("CollapsesDuplicateIDs", func(t *testing.T) {
|
||||
conformance.RunDeleterCollapsesDuplicateIDs(t, ctx, fixture)
|
||||
})
|
||||
|
|
|
|||
|
|
@ -70,6 +70,14 @@ type DeleteRequest struct {
|
|||
// gate is: a second front door that inherited the capability without the
|
||||
// guard would orphan a workspace's graph by omission.
|
||||
//
|
||||
// THE GUARD READS BOTH PLANES ON BOTH ENDS OF THE EDGE, and there is no
|
||||
// wisp exemption at either end. A named WISP with a durable dependent is
|
||||
// refused exactly as a named issue is, and a durable row's wisp dependent
|
||||
// counts too. Half of that was an implementation gap for one release: the
|
||||
// store-backed body asked the guard about the durable half of the request
|
||||
// only, so `bd delete <wisp>` orphaned a durable dependent without saying
|
||||
// so and without being refused.
|
||||
//
|
||||
// A dependent INSIDE the request is not a dependent for this purpose — a
|
||||
// batch that names both ends of an edge is deleting the edge too, and
|
||||
// refusing it would make `bd delete a b` fail on exactly the pair a caller
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue