From d3878543290112d350828b632af0247725d7b11b Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Tue, 1 Dec 2026 19:04:50 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Merge=20entry=20edits=20after=20?= =?UTF-8?q?pending=20sync=20data=20has=20landed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit update() read the record before save() waited for the sync to apply server state, and the record finally written was that stale read plus the patch with a fresh client_updated_at: an edit silently reverted what another device had changed in between, and the server took it as the newer version. update() and remove() now patch the record after the wait, so every field they do not touch keeps the value the pull just applied. The test holds an update while a round applies a remote change to the same record and fails on the old code with the reverted field. --- web/src/lib/db.svelte.ts | 26 +++++++++++++++++++------- web/src/lib/db.test.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/web/src/lib/db.svelte.ts b/web/src/lib/db.svelte.ts index 562e613..85aa363 100644 --- a/web/src/lib/db.svelte.ts +++ b/web/src/lib/db.svelte.ts @@ -104,7 +104,7 @@ function toServerData(name: CollectionName, item: AnyDoc): Record(name: K, data: PayloadMap[K]): Record { +function pick(name: K, data: Partial): Record { const out: Record = {}; for (const [f, v] of Object.entries(data)) { if (DATA_FIELDS[name].includes(f)) out[f] = v; @@ -538,14 +538,26 @@ export async function save(name: K, id: string | null, return rid; } +/** Applies a patch once the sync applied the server state it was waiting for, so the fields the patch leaves out keep a newer remote value. */ +async function merge(name: K, id: string, patch: Partial, deleted?: boolean): Promise { + if (!cols) throw new Error('database is not open'); + await waitForApply(name); + const c = cols?.[name]; + if (!c) throw new Error('database is not open'); + const prev = readOnce(c, { id }, (cursor) => cursor.fetch()[0]); + if (!prev) return; + await c.replaceOne({ id }, { + ...prev, + ...pick(name, patch), + client_updated_at: Math.max(Date.now(), prev.client_updated_at + 1), + deleted: deleted ?? prev.deleted + }); +} + export async function update(name: K, id: string, patch: Partial): Promise { - const cur = get(name, id); - if (!cur) return; - await save(name, id, { ...cur, ...patch } as PayloadMap[K], cur.deleted); + await merge(name, id, patch); } export async function remove(name: K, id: string): Promise { - const cur = get(name, id); - if (!cur) return; - await save(name, id, cur, true); + await merge(name, id, {}, true); } diff --git a/web/src/lib/db.test.ts b/web/src/lib/db.test.ts index e925197..0c57701 100644 --- a/web/src/lib/db.test.ts +++ b/web/src/lib/db.test.ts @@ -364,3 +364,31 @@ describe('session expiry', () => { expect(mocks.pushes).toHaveLength(0); }); }); + +describe('merge after apply', () => { + it('an update waiting for the sync keeps the remote fields the patch does not touch', async () => { + status.online = true; + const id = await save('entries', null, { start_time: 1000, end_time: 2000, auto_stopped: false, note: 'mine', day_key: '2026-01-01' }); + await syncNow(); + const server = mocks.server.get('entries'); + if (!server?.get(id)) throw new Error('server record missing'); + server.set(id, { ...server.get(id)!, start_time: 3000, updated: mocks.stamp() }); + const snapshots = Collection.getCollections().find((c) => c.name === 'wotra-snapshots'); + if (!snapshots) throw new Error('snapshots collection missing'); + let waiting: Promise | undefined; + const onFind = (selector: unknown) => { + if ((selector as { collectionName?: unknown } | null)?.collectionName !== 'entries' || waiting) return; + waiting = update('entries', id, { note: 'edited here' }); + }; + snapshots.on('find', onFind); + try { + await syncNow(); + } finally { + snapshots.off('find', onFind); + } + expect(waiting).toBeDefined(); + await waiting; + expect(get('entries', id)).toMatchObject({ start_time: 3000, end_time: 2000, note: 'edited here', auto_stopped: false }); + expect(await update('entries', 'missing', { note: 'ignored' })).toBeUndefined(); + }); +});