Repository navigation
Expand file tree
/
Copy pathseed-loader-deferred-key-reference.test.ts
More file actions
375 lines (322 loc) · 15.4 KB
/
Copy pathseed-loader-deferred-key-reference.test.ts
File metadata and controls
375 lines (322 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
import { describe, it, expect, vi } from 'vitest';
import { SeedLoaderService } from './seed-loader.js';
import type { IDataEngine, IMetadataService } from '@objectstack/spec/contracts';
import { assertEngineDeleteDispatch, assertEngineUpdateDispatch, assertEngineFindOnePredicate } from '@objectstack/metadata-core';
/**
* [#34800] A dataset whose `externalId` includes a reference the first load
* must leave empty — the person it names has not signed up yet — holds ONE row
* across loads.
*
* The row lands with that key column NULL (the reference is deferred to pass 2
* and misses there too). Matching existing rows by natural key cannot find it
* again: while the reference stays unresolvable the record keys empty, and
* once it resolves the record carries an id the stored row does not. Measured
* before the fix, three loads over one store: 1 → 2 → 3 rows, and 1 → 2 → 2
* when the person appears between loads (the copy that finally carried the
* person sat beside the first one, still NULL). The loader now lands such a
* row with an id derived from the key as authored, and a replay whose natural
* key misses finds the row by that id — and fills the reference onto it once
* it resolves.
*
* Each load runs a fresh loader over the same store, as a restart does: the
* held person references a loader keeps in memory do not survive it.
*
* The engine double filters `where` by equality (an absent column matches
* `null`), keeps an id the write supplies and refuses a second row under one
* id, as a primary key does, and answers the compare-and-set update form.
*/
const ORG = 'org_dkr34800test';
function createLogger() {
return { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() };
}
function rowMatches(row: Record<string, unknown>, where: Record<string, unknown> = {}): boolean {
return Object.entries(where).every(([k, v]) => {
if (k.startsWith('$')) throw new Error(`fake driver: unsupported combinator ${k}`);
if (v !== null && typeof v === 'object') throw new Error(`fake driver: unsupported operator on ${k}`);
return (row[k] ?? null) === (v ?? null);
});
}
function createEngine(): { engine: IDataEngine; store: Record<string, any[]> } {
const store: Record<string, any[]> = {};
let idCounter = 0;
const engine = {
find: vi.fn(async (objectName: string, query?: any) => {
let rows = (store[objectName] ?? []).filter((r) => rowMatches(r, query?.where));
if (typeof query?.limit === 'number') rows = rows.slice(0, query.limit);
return rows.map((r) => ({ ...r }));
}),
findOne: vi.fn(async (objectName: string, query?: any) => {
assertEngineFindOnePredicate(objectName, query);
const rows = await (engine.find as any)(objectName, { ...query, limit: 1 });
return rows[0] ?? null;
}),
insert: vi.fn(async (objectName: string, data: any) => {
const rows = (store[objectName] ??= []);
const land = (d: any) => {
const id = d.id ?? `gen-${++idCounter}`;
if (rows.some((r) => r.id === id)) throw new Error(`fake driver: duplicate primary key '${id}'`);
const row = { ...d, id };
rows.push(row);
return { ...row };
};
return Array.isArray(data) ? data.map(land) : land(data);
}),
update: vi.fn(async (objectName: string, data: any, options?: any) => {
assertEngineUpdateDispatch(data, options);
const rows = store[objectName] ?? [];
if (options?.where) {
let n = 0;
for (const row of rows) {
if (!rowMatches(row, options.where)) continue;
Object.assign(row, data);
n += 1;
}
return n;
}
const row = rows.find((r) => r.id === data.id);
if (!row) return null;
Object.assign(row, data);
return { ...row };
}),
delete: vi.fn(async (_objectName: string, options?: any) => {
assertEngineDeleteDispatch(options);
return { deleted: 1 };
}),
count: vi.fn(async (objectName: string) => (store[objectName] ?? []).length),
aggregate: vi.fn(async () => []),
} as unknown as IDataEngine;
return { engine, store };
}
const OBJECTS: Record<string, any> = {
dkr_person: { name: 'dkr_person', fields: { name: { type: 'text' } } },
dkr_task: {
name: 'dkr_task',
fields: {
name: { type: 'text' },
assignee: { type: 'lookup', reference: 'dkr_person' },
notes: { type: 'text' },
},
},
dkr_tag: { name: 'dkr_tag', fields: { name: { type: 'text' }, kind: { type: 'text' }, notes: { type: 'text' } } },
sys_seed_landed_key: {
name: 'sys_seed_landed_key',
fields: { object_name: { type: 'text' }, seed_key: { type: 'textarea' } },
},
};
function createMetadata(): IMetadataService {
return {
getObject: vi.fn(async (name: string) => OBJECTS[name]),
listObjects: vi.fn(async () => Object.values(OBJECTS)),
register: vi.fn(async () => {}),
get: vi.fn(async (_t: string, name: string) => OBJECTS[name]),
list: vi.fn(async () => []),
unregister: vi.fn(async () => {}),
exists: vi.fn(async () => false),
listNames: vi.fn(async () => []),
} as unknown as IMetadataService;
}
const CONFIG = {
dryRun: false,
haltOnError: false,
multiPass: true,
defaultMode: 'ignore',
batchSize: 1000,
transaction: false,
organizationId: ORG,
} as any;
function taskSeed(mode: 'ignore' | 'upsert', records: Array<Record<string, unknown>>) {
return { object: 'dkr_task', externalId: ['name', 'assignee'], mode, env: ['prod', 'dev', 'test'], records };
}
/** One load by a fresh loader — a restart. */
async function load(engine: IDataEngine, seeds: any[], landedKeys?: 'honour') {
return new SeedLoaderService(engine, createMetadata(), createLogger() as never, landedKeys ? { landedKeys } : {})
.load({ seeds, config: CONFIG });
}
function signUp(store: Record<string, any[]>, name: string): string {
const id = `person-${name.toLowerCase()}`;
(store.dkr_person ??= []).push({ id, name, organization_id: ORG });
return id;
}
describe('seed: a key reference empty on the first load holds one row across loads (#34800)', () => {
for (const mode of ['ignore', 'upsert'] as const) {
it(`${mode}: three loads while the person never appears leave one row`, async () => {
const { engine, store } = createEngine();
const seeds = [taskSeed(mode, [{ name: 'Onboard', assignee: 'Alice', notes: 'first week' }])];
const counts: number[] = [];
for (let boot = 0; boot < 3; boot++) {
await load(engine, seeds);
counts.push(store.dkr_task.length);
}
expect(counts).toEqual([1, 1, 1]);
expect(store.dkr_task[0].assignee ?? null).toBeNull();
});
it(`${mode}: the person appearing between loads fills the reference onto the row first landed`, async () => {
const { engine, store } = createEngine();
const seeds = [taskSeed(mode, [{ name: 'Onboard', assignee: 'Alice', notes: 'first week' }])];
await load(engine, seeds);
const firstLanded = store.dkr_task[0].id;
const alice = signUp(store, 'Alice');
const second = await load(engine, seeds);
await load(engine, seeds);
expect(second.success).toBe(true);
expect(store.dkr_task).toHaveLength(1);
expect(store.dkr_task[0]).toMatchObject({ id: firstLanded, name: 'Onboard', assignee: alice });
});
}
it('with the landed-key record kept, the row is found too and is not reported as removed', async () => {
const { engine, store } = createEngine();
const seeds = [taskSeed('ignore', [{ name: 'Onboard', assignee: 'Alice' }])];
await load(engine, seeds, 'honour');
const alice = signUp(store, 'Alice');
const second = await load(engine, seeds, 'honour');
await load(engine, seeds, 'honour');
expect(second.summary.totalSkipped).toBe(1);
expect(store.dkr_task).toHaveLength(1);
expect(store.dkr_task[0].assignee).toBe(alice);
});
it('two records that differ only in the reference stay two rows, each filled with its own person', async () => {
const { engine, store } = createEngine();
const seeds = [taskSeed('ignore', [
{ name: 'Onboard', assignee: 'Alice' },
{ name: 'Onboard', assignee: 'Bob' },
])];
await load(engine, seeds);
await load(engine, seeds);
expect(store.dkr_task).toHaveLength(2);
const [alice, bob] = [signUp(store, 'Alice'), signUp(store, 'Bob')];
await load(engine, seeds);
await load(engine, seeds);
expect(store.dkr_task).toHaveLength(2);
expect(store.dkr_task.map((r) => r.assignee).sort()).toEqual([alice, bob].sort());
});
it('a row a person created with the reference empty is never taken for the seed row', async () => {
const { engine, store } = createEngine();
store.dkr_task = [{ id: 'customer-row', name: 'Onboard', assignee: null, notes: 'mine', organization_id: ORG }];
const seeds = [taskSeed('upsert', [{ name: 'Onboard', assignee: 'Alice', notes: 'seeded' }])];
await load(engine, seeds);
const alice = signUp(store, 'Alice');
await load(engine, seeds);
expect(store.dkr_task).toHaveLength(2);
expect(store.dkr_task.find((r) => r.id === 'customer-row')).toEqual(
{ id: 'customer-row', name: 'Onboard', assignee: null, notes: 'mine', organization_id: ORG },
);
expect(store.dkr_task.find((r) => r.id !== 'customer-row')).toMatchObject({ assignee: alice, notes: 'seeded' });
});
it('CONTROL: a composite key whose reference resolves on the first load is matched as before, engine-minted id', async () => {
const { engine, store } = createEngine();
const alice = signUp(store, 'Alice');
const seeds = [taskSeed('ignore', [{ name: 'Onboard', assignee: 'Alice' }])];
for (let boot = 0; boot < 3; boot++) await load(engine, seeds);
expect(store.dkr_task).toHaveLength(1);
expect(store.dkr_task[0]).toMatchObject({ assignee: alice });
expect(store.dkr_task[0].id).toMatch(/^gen-/);
});
it('CONTROL: a composite key over plain fields is matched as before, engine-minted id', async () => {
const { engine, store } = createEngine();
const seeds = [{
object: 'dkr_tag', externalId: ['name', 'kind'], mode: 'upsert', env: ['prod', 'dev', 'test'],
records: [{ name: 'Urgent', kind: 'priority' }, { name: 'Urgent', kind: 'label' }],
}];
for (let boot = 0; boot < 3; boot++) await load(engine, seeds);
expect(store.dkr_tag).toHaveLength(2);
expect(store.dkr_tag.every((r) => /^gen-/.test(r.id))).toBe(true);
});
});
/**
* [#34856] A row that landed while a reference in its key was still empty, and
* that a person then deleted, stays deleted — as a row whose key was complete
* at landing does. The record of landed rows (`landedKeys: 'honour'`) recorded
* nothing for a key with an empty component, so such a row was never recorded
* and the next load inserted it again (measured before the fix with exactly
* these loads: back on the next load, whether the person was still missing or
* had signed up since). It is now recorded under the id it lands with, derived
* from its key as AUTHORED: the one identity a load can compute both while the
* reference is still empty and after it resolves. A person's delete removes the
* row from the store directly, as the app's own delete would.
*/
/** One load by a fresh loader, keeping the landed-key record in `landedKeys` mode. */
async function loadWith(engine: IDataEngine, seeds: any[], landedKeys: 'honour' | 'reseed' = 'honour') {
return new SeedLoaderService(engine, createMetadata(), createLogger() as never, { landedKeys })
.load({ seeds, config: CONFIG });
}
/** A person deletes the seeded task named `name` in the app. */
function deleteTask(store: Record<string, any[]>, name: string): void {
const before = store.dkr_task.length;
store.dkr_task = store.dkr_task.filter((r) => r.name !== name);
expect(store.dkr_task.length).toBe(before - 1);
}
describe('seed: a row whose key reference was empty at landing stays deleted (#34856)', () => {
for (const mode of ['ignore', 'upsert'] as const) {
it(`${mode}: deleted while the person is still missing, it is not inserted again`, async () => {
const { engine, store } = createEngine();
const seeds = [taskSeed(mode, [{ name: 'Onboard', assignee: 'Alice', notes: 'first week' }])];
await loadWith(engine, seeds);
expect(store.dkr_task).toHaveLength(1);
deleteTask(store, 'Onboard');
const second = await loadWith(engine, seeds);
await loadWith(engine, seeds);
expect(store.dkr_task).toHaveLength(0);
expect(second.success).toBe(true);
expect(second.summary.totalSkipped).toBe(1);
expect(second.summary.totalInserted).toBe(0);
});
it(`${mode}: deleted, then the person signs up, it is still not inserted again`, async () => {
const { engine, store } = createEngine();
const seeds = [taskSeed(mode, [{ name: 'Onboard', assignee: 'Alice', notes: 'first week' }])];
await loadWith(engine, seeds);
deleteTask(store, 'Onboard');
signUp(store, 'Alice');
const second = await loadWith(engine, seeds);
await loadWith(engine, seeds);
expect(store.dkr_task).toHaveLength(0);
expect(second.summary.totalSkipped).toBe(1);
expect(second.summary.totalInserted).toBe(0);
});
}
it('the row is recorded under the id it landed with, derived from its key as authored', async () => {
const { engine, store } = createEngine();
await loadWith(engine, [taskSeed('ignore', [{ name: 'Onboard', assignee: 'Alice' }])]);
expect(store.dkr_task).toHaveLength(1);
expect(store.dkr_task[0].id).toMatch(/^seed_/);
expect(store.sys_seed_landed_key).toEqual([
expect.objectContaining({ object_name: 'dkr_task', seed_key: JSON.stringify([['id', store.dkr_task[0].id]]) }),
]);
});
it('CONTROL: a row whose key was complete at landing, deleted, stays deleted as before', async () => {
const { engine, store } = createEngine();
signUp(store, 'Alice');
const seeds = [taskSeed('ignore', [{ name: 'Onboard', assignee: 'Alice' }])];
await loadWith(engine, seeds);
expect(store.dkr_task[0].id).toMatch(/^gen-/);
deleteTask(store, 'Onboard');
const second = await loadWith(engine, seeds);
expect(store.dkr_task).toHaveLength(0);
expect(second.summary.totalSkipped).toBe(1);
});
it('CONTROL: a deferred-key row never landed here still lands, beside one a person deleted', async () => {
const { engine, store } = createEngine();
await loadWith(engine, [taskSeed('ignore', [{ name: 'Onboard', assignee: 'Alice' }])]);
deleteTask(store, 'Onboard');
// An upgrade adds a second record to the seed, its key reference empty too.
const upgraded = [taskSeed('ignore', [
{ name: 'Onboard', assignee: 'Alice' },
{ name: 'Review', assignee: 'Alice' },
])];
const second = await loadWith(engine, upgraded);
await loadWith(engine, upgraded);
expect(store.dkr_task.map((r) => r.name)).toEqual(['Review']);
expect(second.summary.totalInserted).toBe(1);
expect(second.summary.totalSkipped).toBe(1);
});
it('CONTROL: a deliberate re-seed brings the deleted deferred-key row back', async () => {
const { engine, store } = createEngine();
const seeds = [taskSeed('ignore', [{ name: 'Onboard', assignee: 'Alice' }])];
await loadWith(engine, seeds);
deleteTask(store, 'Onboard');
await loadWith(engine, seeds, 'reseed');
expect(store.dkr_task).toHaveLength(1);
expect(store.dkr_task[0].name).toBe('Onboard');
expect(store.dkr_task[0].assignee ?? null).toBeNull();
});
});