Skip to content

Commit a089188

Browse files
committed
fix(objectql): a predicate delete runs the referential walk for every matched row (#26871)
Claude-Session: https://claude.ai/code/session_01FEkQ5ptsvnYe5ZErgvP66h Co-authored-by: Claude <noreply@anthropic.com>
1 parent 9e22d92 commit a089188

2 files changed

Lines changed: 66 additions & 3 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ function makeDriver(opts: { transactional?: boolean } = {}) {
6868
if (!where || typeof where !== 'object') return true;
6969
for (const [k, v] of Object.entries(where)) {
7070
if (k.startsWith('$')) continue;
71+
// [#26871] The predicate delete removes the rows it walked by id set.
72+
if (v && typeof v === 'object' && Array.isArray((v as any).$in)) {
73+
if (!(v as any).$in.some((x: unknown) => String(x) === String(row[k]))) return false;
74+
continue;
75+
}
7176
const exp = v && typeof v === 'object' && '$eq' in (v as any) ? (v as any).$eq : v;
7277
if ((row[k] ?? null) !== (exp ?? null)) return false;
7378
}

‎packages/objectql/src/engine.ts‎

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18210,8 +18210,9 @@ export class ObjectQL implements IObjectQLEngine {
1821018210
* required lookup that escalation is decided per ROW after the dependents
1821118211
* probe (#9688): a row that keeps another member takes the member removal,
1821218212
* a row the removal would EMPTY keeps the refusal, and the refusal counts
18213-
* only those rows. Only runs for single-id deletes — multi/predicate
18214-
* deletes skip cascade (logged).
18213+
* only those rows. A by-id delete runs it for its one record; a predicate
18214+
* delete (`multi: true`) runs it for every row it matched ([#26871], see
18215+
* `delete()`'s predicate arm).
1821518216
*
1821618217
* [#22305] Two phases, so the walk knows its own set before it judges
1821718218
* anything:
@@ -19683,7 +19684,64 @@ export class ObjectQL implements IObjectQLEngine {
1968319684
// [#2982] The AST asserted present and already used for the
1968419685
// pre-phase row read above.
1968519686
// `deleteMany` presence is part of the ladder verdict resolved above.
19686-
result = await driver.deleteMany!(object, opCtx.ast!, hookContext.input.options as any);
19687+
//
19688+
// [#26871] A predicate delete honours `deleteBehavior` as the
19689+
// by-id branch does: a `restrict` refuses, a `cascade` (a
19690+
// `master_detail` by default) deletes the dependents, a
19691+
// `set_null` clears them. This arm used to go straight to
19692+
// `deleteMany`, so a flow's `delete_record` with `multi: true`
19693+
// skipped every declared rule the same delete done record by
19694+
// record honours.
19695+
//
19696+
// `planCascadeAtomicity` is the by-id branch's own question:
19697+
// `'none'` means no relation of the delete family points at
19698+
// this object, so the walk could write nothing and the arm keeps
19699+
// its single `deleteMany`, no rows read and no transaction.
19700+
const plan = this.planCascadeAtomicity(object);
19701+
if (plan === 'none') {
19702+
result = await driver.deleteMany!(object, opCtx.ast!, hookContext.input.options as any);
19703+
} else {
19704+
// Otherwise: resolve the matched rows through the composed,
19705+
// row-scoped AST, run `cascadeDeleteRelations` for each, then
19706+
// delete exactly those rows by id. By id, not by re-running the
19707+
// predicate: the walk's own writes (a `set_null` on a
19708+
// self-reference) can move a row into or out of the predicate,
19709+
// and the rows deleted must be the rows walked. The ids were
19710+
// read under this caller's scope inside the same unit of work,
19711+
// as the by-id branch deletes its id after reading its
19712+
// pre-image under that scope. The unit of work is the by-id
19713+
// branch's, for the same reasons (#7413).
19714+
const runPredicateDelete = async (writeContext?: ExecutionContext): Promise<unknown> => {
19715+
const writeOptions = this.buildDriverOptions(object, writeContext, hookContext.input.options as any);
19716+
const matched = ((await driver.find(object, { ...opCtx.ast!, fields: ['id'] }, writeOptions)) ?? []) as Array<Record<string, unknown>>;
19717+
const matchedIds = matched.map((row) => row?.id).filter((v): v is string | number => v != null);
19718+
for (const matchedId of matchedIds) {
19719+
await this.cascadeDeleteRelations(object, matchedId, writeContext);
19720+
}
19721+
// The id set rides an `$in`; batched so one statement never
19722+
// outgrows a driver's bind-parameter limit.
19723+
const batchSize = 500;
19724+
let removed = 0;
19725+
for (let i = 0; i < matchedIds.length; i += batchSize) {
19726+
const batchAst: QueryAST = { object, where: { id: { $in: matchedIds.slice(i, i + batchSize) } } };
19727+
const count = await driver.deleteMany!(object, batchAst, writeOptions);
19728+
// A non-count answer goes back as the driver gave it, so
19729+
// the `isDeleteResultShape` check below refuses it.
19730+
if (typeof count !== 'number') return count;
19731+
removed += count;
19732+
}
19733+
return removed;
19734+
};
19735+
if (plan === 'atomic') {
19736+
result = await this.transaction(
19737+
async (trxCtx: any) => await runPredicateDelete(trxCtx as ExecutionContext),
19738+
opCtx.context,
19739+
);
19740+
} else {
19741+
this.warnCascadeNotAtomic(object);
19742+
result = await runPredicateDelete(opCtx.context);
19743+
}
19744+
}
1968719745
isPredicateWrite = true;
1968819746
}
1968919747

0 commit comments

Comments
 (0)