Skip to content

Commit 5ed2476

Browse files
committed
docs(changeset): a cascaded child's dispatch.scope is the chunk's, and each phase is a fresh context
The FROM -> TO names the two rows a delete-hook author meets silently, with the remedy: keep cross-phase state in ctx.dispatch.scope keyed by ctx.input.id. Pinned on both shapes in engine-cascade-delete-batch.test.ts. The fallback list names the dependent the collected set never held. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QsR2jUg7BKfRBcTPyMxbLh
1 parent 74ef4e4 commit 5ed2476

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

‎.changeset/33699-cascade-delete-batch.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ What a hook on a cascaded child sees:
1818

1919
- FROM `ctx.dispatch.mode: 'record'` and `ctx.input.options` `{ where: { id } }` TO `ctx.dispatch.mode: 'per-row'`, with `ctx.dispatch.index` its position in the chunk and `ctx.input.options` carrying `where: { id: { $in: [...] } }` and `multi: true`. `ctx.input.id` and `ctx.previous` still name the child's own row, and `ctx.referentialCascadeDelete` is still `true` on every per-row context, in both phases, grandchildren included.
2020
- FROM `ctx.result` in a cascaded child's `afterDelete` being the driver's by-id answer TO `ctx.result` absent, as on every per-row delete context. Read `ctx.previous` for the deleted row.
21+
- FROM `ctx.dispatch.scope` one scratch object per cascaded child TO one object shared by every child of the chunk, in both phases (the scratch of one caller write, which the chunk now is).
22+
- FROM one context object for a cascaded child's `beforeDelete` and `afterDelete` TO a fresh context per row per phase, so a value a `beforeDelete` handler leaves on `ctx` no longer reaches that child's `afterDelete`. Keep cross-phase state in `ctx.dispatch.scope`, keyed by `ctx.input.id`.
2123
- The one behaviour that flips: the chunk runs every child's `beforeDelete`, then the one set delete, then every child's `afterDelete`. Before, each child ran before → delete → after in turn. So a `beforeDelete` hook that counts a child's remaining siblings now sees them ALL present, where it used to see one fewer at each step. Judge the whole batch instead: on `ctx.input.options.multi === true` read the target set from `ctx.input.options.where` (the shipped last-administrator guard does exactly this, and reaches the same verdict as before), or use `ctx.referentialCascadeDelete === true` to tell the cascade from a hand delete.
22-
- A chunk the caller's scope narrows falls back to today's by-id deletes for that chunk, so the caller still meets the same refusal and the whole cascade rolls back: a child the caller may not delete, which a predicate delete would skip, and a child the caller may not read, which the by-id delete refuses through its re-read of the row (the cascade asks the caller's read door for each chunk's ids before batching it). Such a chunk keeps today's shape, `'record'` with no `multi`, so one cascade can show both shapes. A driver with no `deleteMany`, a floored external driver, and a cascade into the organization object (deleted by id or not at all) keep the by-id path too.
24+
- A chunk the caller's scope narrows falls back to today's by-id deletes for that chunk, so the caller still meets the same refusal and the whole cascade rolls back: a child the caller may not delete, which a predicate delete would skip, and a child the caller may not read, which the by-id delete refuses through its re-read of the row (the cascade asks the caller's read door for each chunk's ids before batching it). Such a chunk keeps today's shape, `'record'` with no `multi`, so one cascade can show both shapes. A driver with no `deleteMany`, a floored external driver, and a cascade into the organization object (deleted by id or not at all) keep the by-id path too, and so does a child the walk finds that the cascade's collected set never held (a row a hook of the same cascade wrote after the set was collected): it is deleted as the root of its own cascade, with its own recycle-bin entry, as before.
2325

2426
Unchanged: one `data.record.deleted` realtime event per child (never the bulk `data.records.deleted`); a refusal from any child's hook, or a permission refusal, rolls the whole cascade back; the recycle bin keeps one entry per root; the bulk per-row hook ceiling is never reached (a chunk is 500, the ceiling 10,000). On a runtime that cannot roll back, a refusal raised after a chunk's set delete (a failed roll-up recompute) now finds the whole chunk deleted, where the per-child walk had deleted only the children before the refusal.

‎packages/objectql/src/engine-cascade-delete-batch.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,46 @@ describe('[F1] a cascade deletes a relation\'s children as one batch per chunk',
196196
expect(ids(driver.rowsOf('acct'))).toEqual([]);
197197
});
198198

199+
it('dispatch.scope is one object for every child of the chunk, both phases; each phase is a fresh context', async () => {
200+
const probe = async (deleteMany: boolean) => {
201+
const { engine } = await makeEngine({ deleteMany });
202+
const scopes: Array<{ event: string; id: string; scope: unknown; stashed: unknown; keyed: unknown }> = [];
203+
engine.on('beforeDelete', 'line', (ctx: any) => {
204+
ctx.stashedOnCtx = ctx.input.id;
205+
ctx.dispatch.scope[`seen:${String(ctx.input.id)}`] = 'before';
206+
scopes.push({ event: 'before', id: String(ctx.input.id), scope: ctx.dispatch.scope, stashed: undefined, keyed: undefined });
207+
});
208+
engine.on('afterDelete', 'line', (ctx: any) => {
209+
scopes.push({
210+
event: 'after', id: String(ctx.input.id), scope: ctx.dispatch.scope,
211+
stashed: ctx.stashedOnCtx, keyed: ctx.dispatch.scope[`seen:${String(ctx.input.id)}`],
212+
});
213+
});
214+
await engine.delete('acct', { where: { id: 'a1' }, context: { ...CALLER } });
215+
return scopes;
216+
};
217+
218+
// Batched: ONE scope object across the chunk's six dispatches; a value
219+
// left on ctx in beforeDelete does not reach that child's afterDelete,
220+
// and the remedy (keep it in dispatch.scope keyed by input.id) does.
221+
const batched = await probe(true);
222+
expect(batched).toHaveLength(6);
223+
expect(new Set(batched.map((s) => s.scope)).size).toBe(1);
224+
for (const s of batched.filter((x) => x.event === 'after')) {
225+
expect(s.stashed, `${s.id}: a ctx stash does not cross phases on a per-row dispatch`).toBeUndefined();
226+
expect(s.keyed, `${s.id}: dispatch.scope keyed by input.id does`).toBe('before');
227+
}
228+
229+
// Control, today's by-id shape: one scope per child, the same context in
230+
// both phases, so the ctx stash arrives.
231+
const byId = await probe(false);
232+
expect(new Set(byId.map((s) => s.scope)).size).toBe(3);
233+
for (const s of byId.filter((x) => x.event === 'after')) {
234+
expect(s.stashed).toBe(s.id);
235+
expect(s.scope).toBe(byId.find((x) => x.event === 'before' && x.id === s.id)!.scope);
236+
}
237+
});
238+
199239
it('a before-delete hook counting remaining siblings now sees them all present', async () => {
200240
const { engine } = await makeEngine();
201241
const remaining: number[] = [];

0 commit comments

Comments
 (0)