|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * A `delete_record` node with `multi: true` and a filter honours every |
| 5 | + * declared `deleteBehavior` of the rows it matches: a `restrict` refuses the |
| 6 | + * whole delete, a `master_detail` child is cascaded, a `set_null` lookup is |
| 7 | + * cleared. |
| 8 | + * |
| 9 | + * The node forwards the author's bulk intent to `data.delete({ where, multi: |
| 10 | + * true })`. The engine's predicate arm used to go straight to |
| 11 | + * `driver.deleteMany`, so a flow deleting parents by filter skipped the |
| 12 | + * referential walk the by-id delete runs: no restrict refused, master_detail |
| 13 | + * children and lookups were left pointing at deleted rows. The same delete |
| 14 | + * done record by record through the data door refused or cascaded. The arm |
| 15 | + * now resolves the rows it matches and runs the by-id walk for each, in one |
| 16 | + * unit of work. |
| 17 | + * |
| 18 | + * Composition: the real stack the other `*.integration.test.ts` files in this |
| 19 | + * package boot: `ObjectKernel`, `ObjectQLPlugin`, `driver-sql` on |
| 20 | + * better-sqlite3 `:memory:`, and the real `AutomationServicePlugin` running |
| 21 | + * the flow. Rows are counted out of the DRIVER, not through the engine. |
| 22 | + */ |
| 23 | + |
| 24 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 25 | +import { ObjectKernel } from '@objectstack/core'; |
| 26 | +import { ObjectQLPlugin, type ObjectQL } from '@objectstack/objectql'; |
| 27 | +import { SqlDriver } from '@objectstack/driver-sql'; |
| 28 | +import { AutomationServicePlugin } from '../plugin.js'; |
| 29 | +import { AutomationEngine } from '../engine.js'; |
| 30 | +import { registerCrudNodes } from './crud-nodes.js'; |
| 31 | +import { installDefaultOrganization } from '../default-organization.testkit.js'; |
| 32 | + |
| 33 | +const SYS = { isSystem: true } as const; |
| 34 | + |
| 35 | +const PARENT = { |
| 36 | + name: 'pin_bulk_parent', |
| 37 | + label: 'Pin bulk parent', |
| 38 | + fields: { |
| 39 | + title: { name: 'title', label: 'Title', type: 'text' }, |
| 40 | + grp: { name: 'grp', label: 'Group', type: 'text' }, |
| 41 | + }, |
| 42 | +}; |
| 43 | +/** A detail: `master_detail` cascades by default. */ |
| 44 | +const DETAIL = { |
| 45 | + name: 'pin_bulk_detail', |
| 46 | + label: 'Pin bulk detail', |
| 47 | + fields: { |
| 48 | + title: { name: 'title', label: 'Title', type: 'text' }, |
| 49 | + parent: { name: 'parent', label: 'Parent', type: 'master_detail', reference: 'pin_bulk_parent' }, |
| 50 | + }, |
| 51 | +}; |
| 52 | +/** An authored `restrict` lookup. */ |
| 53 | +const GUARD = { |
| 54 | + name: 'pin_bulk_guard', |
| 55 | + label: 'Pin bulk guard', |
| 56 | + fields: { |
| 57 | + title: { name: 'title', label: 'Title', type: 'text' }, |
| 58 | + parent: { name: 'parent', label: 'Parent', type: 'lookup', reference: 'pin_bulk_parent', deleteBehavior: 'restrict' }, |
| 59 | + }, |
| 60 | +}; |
| 61 | +/** An optional `lookup`: `set_null` by default. */ |
| 62 | +const LINK = { |
| 63 | + name: 'pin_bulk_link', |
| 64 | + label: 'Pin bulk link', |
| 65 | + fields: { |
| 66 | + title: { name: 'title', label: 'Title', type: 'text' }, |
| 67 | + parent: { name: 'parent', label: 'Parent', type: 'lookup', reference: 'pin_bulk_parent' }, |
| 68 | + }, |
| 69 | +}; |
| 70 | + |
| 71 | +function makeLogger(): any { |
| 72 | + const l: any = { info() {}, warn() {}, error() {}, debug() {}, trace() {}, fatal() {} }; |
| 73 | + l.child = () => l; |
| 74 | + return l; |
| 75 | +} |
| 76 | + |
| 77 | +type Executor = { execute: (node: any, variables: Map<string, unknown>, context: any) => Promise<any> }; |
| 78 | + |
| 79 | +describe('[#26871] a multi delete_record runs the referential walk for every row its filter matches', () => { |
| 80 | + let kernel: ObjectKernel; |
| 81 | + let ql: ObjectQL; |
| 82 | + let driver: SqlDriver; |
| 83 | + let automation: AutomationEngine; |
| 84 | + let deleteRecord: Executor; |
| 85 | + |
| 86 | + beforeAll(async () => { |
| 87 | + kernel = new ObjectKernel({ logger: { level: 'fatal' } }); |
| 88 | + await kernel.use(new ObjectQLPlugin()); |
| 89 | + await kernel.use(new AutomationServicePlugin({ suspendedRunStore: 'memory' })); |
| 90 | + await kernel.bootstrap(); |
| 91 | + ql = kernel.getService<ObjectQL>('objectql'); |
| 92 | + automation = kernel.getService<AutomationEngine>('automation'); |
| 93 | + |
| 94 | + driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true }); |
| 95 | + await driver.connect(); |
| 96 | + ql.registerDriver(driver, true); |
| 97 | + for (const o of [PARENT, DETAIL, GUARD, LINK]) ql.registry.registerObject(o as any, 'pin-bulk-delete', 'pin-bulk-delete'); |
| 98 | + await ql.syncSchemas(); |
| 99 | + await installDefaultOrganization(ql); |
| 100 | + |
| 101 | + // The registered executor itself, over the same engine, so the refusal's |
| 102 | + // structured half can be read off the node's own answer. |
| 103 | + const executors = new Map<string, Executor>(); |
| 104 | + const capture = new Proxy(new AutomationEngine(makeLogger()), { |
| 105 | + get(target, key, receiver) { |
| 106 | + if (key === 'registerNodeExecutor') return (e: any) => { executors.set(e.type, e); }; |
| 107 | + return Reflect.get(target, key, receiver); |
| 108 | + }, |
| 109 | + }); |
| 110 | + registerCrudNodes(capture as AutomationEngine, { logger: makeLogger(), getService: (n: string) => (n === 'data' ? ql : undefined) } as any); |
| 111 | + deleteRecord = executors.get('delete_record')!; |
| 112 | + expect(deleteRecord, 'control: the delete_record executor was registered').toBeDefined(); |
| 113 | + }, 60_000); |
| 114 | + |
| 115 | + afterAll(async () => { |
| 116 | + try { await kernel?.shutdown(); } catch { /* best-effort teardown */ } |
| 117 | + }); |
| 118 | + |
| 119 | + const insert = async (object: string, data: Record<string, unknown>): Promise<string> => |
| 120 | + ((await ql.insert(object, data, { context: SYS })) as any).id; |
| 121 | + /** Rows of `object`, read from the driver with no engine in between. */ |
| 122 | + const rows = async (object: string, where: Record<string, unknown> = {}): Promise<any[]> => |
| 123 | + (await driver.find(object, { object, where } as any)) as any[]; |
| 124 | + |
| 125 | + function bulkDeleteFlow(name: string, grp: string) { |
| 126 | + return { |
| 127 | + name, label: name, type: 'autolaunched', runAs: 'system', |
| 128 | + nodes: [ |
| 129 | + { id: 'start', type: 'start', label: 'Start' }, |
| 130 | + { id: 'sweep', type: 'delete_record', label: 'Sweep', config: { objectName: PARENT.name, filter: { grp }, multi: true } }, |
| 131 | + { id: 'end', type: 'end', label: 'End' }, |
| 132 | + ], |
| 133 | + edges: [ |
| 134 | + { id: 'e1', source: 'start', target: 'sweep' }, |
| 135 | + { id: 'e2', source: 'sweep', target: 'end' }, |
| 136 | + ], |
| 137 | + }; |
| 138 | + } |
| 139 | + |
| 140 | + it('a restrict held on one matched parent refuses the whole delete, and nothing is deleted', async () => { |
| 141 | + const p1 = await insert(PARENT.name, { title: 'h1', grp: 'held' }); |
| 142 | + const p2 = await insert(PARENT.name, { title: 'h2', grp: 'held' }); |
| 143 | + await insert(DETAIL.name, { title: 'd-h1', parent: p1 }); |
| 144 | + await insert(LINK.name, { title: 'l-h1', parent: p1 }); |
| 145 | + await insert(GUARD.name, { title: 'g-h2', parent: p2 }); |
| 146 | + |
| 147 | + const flow = bulkDeleteFlow('pin_bulk_delete_held', 'held'); |
| 148 | + automation.registerFlow(flow.name, flow as any); |
| 149 | + const res: any = await automation.execute(flow.name, {}); |
| 150 | + expect(res.success, 'the run fails on the restrict').toBe(false); |
| 151 | + |
| 152 | + // The node's own answer carries the engine's refusal. |
| 153 | + const node: any = await deleteRecord.execute( |
| 154 | + { id: 'sweep', type: 'delete_record', config: { objectName: PARENT.name, filter: { grp: 'held' }, multi: true } }, |
| 155 | + new Map(), |
| 156 | + { runAs: 'system', flowName: flow.name }, |
| 157 | + ); |
| 158 | + expect(node.success).toBe(false); |
| 159 | + expect(node.innerRefusal).toMatchObject({ code: 'DELETE_RESTRICTED' }); |
| 160 | + |
| 161 | + // Nothing changed: both parents, the detail the walk reached first, and |
| 162 | + // the lookup it cleared are all as they were. |
| 163 | + expect((await rows(PARENT.name, { grp: 'held' })).map((r) => r.id).sort()).toEqual([p1, p2].sort()); |
| 164 | + expect(await rows(DETAIL.name, { parent: p1 })).toHaveLength(1); |
| 165 | + expect(await rows(LINK.name, { parent: p1 })).toHaveLength(1); |
| 166 | + expect(await rows(GUARD.name, { parent: p2 })).toHaveLength(1); |
| 167 | + }); |
| 168 | + |
| 169 | + it('cascades master_detail children and clears set_null lookups of every matched parent', async () => { |
| 170 | + const p1 = await insert(PARENT.name, { title: 'x1', grp: 'doomed' }); |
| 171 | + const p2 = await insert(PARENT.name, { title: 'x2', grp: 'doomed' }); |
| 172 | + const kept = await insert(PARENT.name, { title: 'k', grp: 'kept' }); |
| 173 | + await insert(DETAIL.name, { title: 'd-x1', parent: p1 }); |
| 174 | + await insert(DETAIL.name, { title: 'd-x2', parent: p2 }); |
| 175 | + const keptDetail = await insert(DETAIL.name, { title: 'd-k', parent: kept }); |
| 176 | + const l1 = await insert(LINK.name, { title: 'l-x1', parent: p1 }); |
| 177 | + const l2 = await insert(LINK.name, { title: 'l-x2', parent: p2 }); |
| 178 | + const keptLink = await insert(LINK.name, { title: 'l-k', parent: kept }); |
| 179 | + |
| 180 | + const flow = bulkDeleteFlow('pin_bulk_delete_doomed', 'doomed'); |
| 181 | + automation.registerFlow(flow.name, flow as any); |
| 182 | + const res: any = await automation.execute(flow.name, {}); |
| 183 | + expect(res.success, `run failed: ${JSON.stringify(res.error ?? res)}`).toBe(true); |
| 184 | + |
| 185 | + expect(await rows(PARENT.name, { grp: 'doomed' })).toEqual([]); |
| 186 | + expect((await rows(PARENT.name, { grp: 'kept' })).map((r) => r.id)).toEqual([kept]); |
| 187 | + // master_detail: the matched parents' details are gone, the kept one stays. |
| 188 | + expect(await rows(DETAIL.name, { parent: p1 })).toEqual([]); |
| 189 | + expect(await rows(DETAIL.name, { parent: p2 })).toEqual([]); |
| 190 | + expect((await rows(DETAIL.name, { parent: kept })).map((r) => r.id)).toEqual([keptDetail]); |
| 191 | + // set_null: the matched parents' lookups are cleared, not left dangling. |
| 192 | + const links = new Map((await rows(LINK.name)).map((r) => [r.id, r.parent ?? null])); |
| 193 | + expect(links.get(l1)).toBeNull(); |
| 194 | + expect(links.get(l2)).toBeNull(); |
| 195 | + expect(links.get(keptLink)).toBe(kept); |
| 196 | + }); |
| 197 | +}); |
0 commit comments