Skip to content

Commit 1554220

Browse files
committed
test(service-automation): pin the set exemption across a predicate delete's matched rows; changeset
Claude-Session: https://claude.ai/code/session_01FEkQ5ktxGoJfsedLmkFjdk Co-authored-by: Claude <noreply@anthropic.com>
1 parent a124e62 commit 1554220

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
'@objectstack/objectql': patch
3+
---
4+
5+
fix(objectql): a predicate delete walks the rows it matches as one set: each relation pointing at the object is asked once per 500 matched rows, not once per row (#27004)
6+
7+
Clause-②: yes (widening) — a predicate delete (`engine.delete(object, { where, multi: true })`, a flow's `delete_record` with `multi: true`) whose matched rows are held only by OTHER matched rows (a report and its manager through a `restrict` self-lookup, both matched) now deletes them; the row-by-row walk refused it, or not, depending on which row it met first. Nothing is narrowed.
8+
Metadata-protocol: none
9+
10+
- **Before.** The predicate delete ran the by-id referential walk once per matched row, so its cost was matched rows times referencing relations: 1,001 rows of an object three relations point at took 4,004 dependents probes, about 1.7 s on SQLite.
11+
- **Now.** One walk, rooted at the whole matched set (a by-id delete is a set of one). Each referencing relation is probed once per 500 matched rows with `$in` (a multiple lookup ORs each id's `$contains`), and each row is then judged and written as before: `restrict` refuses, `cascade` deletes (each child through its own delete, hooks and all), `set_null` clears, a multiple lookup loses only the deleted members and an emptied set is written as `[]`. The same 1,001-row delete takes 12 probes.
12+
- **Unchanged.** A refusal's code and wording: it still names one record and counts the rows that refuse over that record (for a required multiple lookup, the rows the removal would empty). The atomic and split plans, the 500-id `$in` delete batches, the roll-up recompute after the delete, and the filter of every probe a by-id delete sends.
13+
- **Who sees it.** A bulk delete that removes rows holding each other: a matched row that another matched row holds is in the delete's own set, so it neither refuses nor is cleared first (the rule a cascade already applies to the rows it deletes). A holder outside the matched rows still refuses.

‎packages/services/service-automation/src/builtin/delete-record-predicate-walk-batched.integration.test.ts‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
* the walk existed. The walk now takes a root set; a by-id delete is a set of
1414
* one.
1515
*
16+
* One walk of one set also means the matched rows are members of it: a
17+
* matched row held only by other matched rows refuses nothing and is cleared
18+
* of nothing, the #22305 set exemption, where the row-by-row walk refused or
19+
* not depending on which row it met first.
20+
*
1621
* Composition: the real stack `delete-record-predicate-referential-walk`
1722
* boots (`ObjectKernel`, `ObjectQLPlugin`, `driver-sql` on better-sqlite3
1823
* `:memory:`, the real `delete_record` executor). Driver calls are counted on
@@ -86,6 +91,17 @@ const MEMBERS = {
8691
},
8792
};
8893

94+
/** A self-reference with an authored `restrict`: a report holds its manager. */
95+
const STAFF = {
96+
name: 'pin_batch_staff',
97+
label: 'Pin batch staff',
98+
fields: {
99+
title: { name: 'title', label: 'Title', type: 'text' },
100+
grp: { name: 'grp', label: 'Group', type: 'text' },
101+
manager: { name: 'manager', label: 'Manager', type: 'lookup', reference: 'pin_batch_staff', deleteBehavior: 'restrict' },
102+
},
103+
};
104+
89105
function makeLogger(): any {
90106
const l: any = { info() {}, warn() {}, error() {}, debug() {}, trace() {}, fatal() {} };
91107
l.child = () => l;
@@ -110,7 +126,7 @@ describe('[#27004] a multi delete_record walks its matched rows as a set', () =>
110126
driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true });
111127
await driver.connect();
112128
ql.registerDriver(driver, true);
113-
for (const o of [PARENT, DETAIL, LINK, TAGS, HOLDER, MEMBERS]) ql.registry.registerObject(o as any, 'pin-batch-delete', 'pin-batch-delete');
129+
for (const o of [PARENT, DETAIL, LINK, TAGS, HOLDER, MEMBERS, STAFF]) ql.registry.registerObject(o as any, 'pin-batch-delete', 'pin-batch-delete');
114130
await ql.syncSchemas();
115131
await installDefaultOrganization(ql);
116132

@@ -226,4 +242,21 @@ describe('[#27004] a multi delete_record walks its matched rows as a set', () =>
226242
expect(members.get('m-keeps')).toEqual([ids[10], other]);
227243
expect(members.get('m-empties')).toEqual([ids[20], ids[550]]);
228244
});
245+
246+
it('a matched row held only by other matched rows is in the set: the delete goes through, whatever the order', async () => {
247+
// The manager first, so a walk of one row at a time met the report while
248+
// it still stood beside a set of one, and refused.
249+
const manager = await insert(STAFF.name, { title: 'm', grp: 'team' });
250+
const report = await insert(STAFF.name, { title: 'r', grp: 'team', manager });
251+
const removed = await ql.delete(STAFF.name, { where: { grp: 'team' }, multi: true, context: SYS });
252+
expect(removed).toBe(2);
253+
expect(await rows(STAFF.name, { id: { $in: [manager, report] } })).toEqual([]);
254+
255+
// A holder outside the matched rows still refuses, and nothing is deleted.
256+
const held = await insert(STAFF.name, { title: 'h', grp: 'team' });
257+
await insert(STAFF.name, { title: 'o', grp: 'elsewhere', manager: held });
258+
const err: any = await ql.delete(STAFF.name, { where: { grp: 'team' }, multi: true, context: SYS }).catch((e: unknown) => e);
259+
expect(err).toMatchObject({ code: 'DELETE_RESTRICTED', dependentObject: STAFF.name, dependentCount: 1 });
260+
expect((await rows(STAFF.name, { grp: 'team' })).map((r) => r.id)).toEqual([held]);
261+
});
229262
});

0 commit comments

Comments
 (0)