From 96f90149cfb7dc5c80624b6e49662a3e527b6057 Mon Sep 17 00:00:00 2001 From: eleith Date: Mon, 23 Nov 2026 06:25:46 -0800 Subject: [PATCH] refactor(snapshots): centralize selected publication reads --- .../centralize-saved-publication-reads.md | 5 ++ packages/snapshots/README.md | 5 +- .../snapshots/src/reports/publication.test.ts | 41 +++++++++---- packages/snapshots/src/reports/publication.ts | 57 ++++++++----------- 4 files changed, 62 insertions(+), 46 deletions(-) create mode 100644 .changeset/centralize-saved-publication-reads.md diff --git a/.changeset/centralize-saved-publication-reads.md b/.changeset/centralize-saved-publication-reads.md new file mode 100644 index 0000000..f833b48 --- /dev/null +++ b/.changeset/centralize-saved-publication-reads.md @@ -0,0 +1,5 @@ +--- +'@hledger-reports/web': patch +--- + +Give saved overview and register reads one place to verify the selected publication and close its database connection. Browsing behavior and the saved format are unchanged. diff --git a/packages/snapshots/README.md b/packages/snapshots/README.md index 4c447f1..6d9f64a 100644 --- a/packages/snapshots/README.md +++ b/packages/snapshots/README.md @@ -2,8 +2,9 @@ Stores one private SQLite report. A complete, closed replacement is published atomically. If a request needs more data after its selected report was replaced, -it can fail cleanly and be refreshed. Recorded journal checks are separate from -financial publication and do not prove freshness. +it can fail cleanly and be refreshed. Follow-up reads in `src/reports/publication.ts` +use the selected report or fail; they never switch to its replacement. Recorded +journal checks are separate from financial publication and do not prove freshness. Update web and worker together and use Settings to prepare the first report in the new slot. Legacy report sets are ignored, not migrated or deleted. diff --git a/packages/snapshots/src/reports/publication.test.ts b/packages/snapshots/src/reports/publication.test.ts index 52648f6..b4fd9f9 100644 --- a/packages/snapshots/src/reports/publication.test.ts +++ b/packages/snapshots/src/reports/publication.test.ts @@ -241,17 +241,18 @@ it('does not mix two publications from different attempts of the same request', ); }); -it('finishes an already-open read from the report it selected', async () => { +it('reads from the connection it verified even if the report is replaced after the key lookup', async () => { await publish(older); const pinned = await selected(); - const readActivity = reports.readAccountActivityPage; + const readKey = reports.readPublicationRequest; const next = { ...fixture(), preparedAt: 'replacement during read' }; const stage = await createReportStage(snapshots, newer, pinned.accounts); await importNativeExports(stage, 1, 'new rows'); await stage.writeRows('assets:cash', rows(1, 'new rows')); - vi.spyOn(reports, 'readAccountActivityPage').mockImplementationOnce(async (...args) => { + vi.spyOn(reports, 'readPublicationRequest').mockImplementationOnce(async (database) => { + const key = await readKey(database); await stage.publish(next, [ready()], journal); - return readActivity(...args); + return key; }); const page = await readAccountPage(pinned, pinned.accounts[0].id, { order: 'asc', @@ -745,6 +746,9 @@ it('releases read connections on successful and failed saved reads', async () => await publish(); const openDatabase = databases.openReadOnlyReportDatabase; const connectionClosures: ReturnType[] = []; + const closeStarted = Promise.withResolvers(); + const releaseClose = Promise.withResolvers(); + let holdNextClose = false; vi.spyOn(databases, 'openReadOnlyReportDatabase').mockImplementation(async (path) => { const database = await openDatabase(path); if (database) { @@ -752,6 +756,11 @@ it('releases read connections on successful and failed saved reads', async () => connectionClosures.push(close); const destroy = database.destroy.bind(database); vi.spyOn(database, 'destroy').mockImplementation(async () => { + if (holdNextClose) { + holdNextClose = false; + closeStarted.resolve(); + await releaseClose.promise; + } await destroy(); close(); }); @@ -761,12 +770,24 @@ it('releases read connections on successful and failed saved reads', async () => const publication = await selected(); expect(connectionClosures.shift()).toHaveBeenCalledOnce(); const account = publication.accounts[0]; - const page = await readAccountPage(publication, account.id, { - order: 'asc', - offset: 0, - limit: 50 - }); - expect(page?.rows).toEqual(rows()); + const options = { order: 'asc', offset: 0, limit: 50 } as const; + holdNextClose = true; + const page = readAccountPage(publication, account.id, options); + try { + await Promise.race([closeStarted.promise, page]); + const pending = Symbol('close still pending'); + expect(await Promise.race([page, timers.setImmediate(pending)])).toBe(pending); + } finally { + releaseClose.resolve(); + await page; + } + expect((await page)?.rows).toEqual(rows()); + expect(connectionClosures.shift()).toHaveBeenCalledOnce(); + + const stalePublication = { ...publication, publicationKey: 'no longer selected' }; + await expect(readAccountPage(stalePublication, account.id, options)).rejects.toSatisfy( + safeError('Unable to read account transactions') + ); expect(connectionClosures.shift()).toHaveBeenCalledOnce(); vi.spyOn(reports, 'readAccountActivityDetail').mockRejectedValueOnce( diff --git a/packages/snapshots/src/reports/publication.ts b/packages/snapshots/src/reports/publication.ts index 7915daa..58b8793 100644 --- a/packages/snapshots/src/reports/publication.ts +++ b/packages/snapshots/src/reports/publication.ts @@ -150,31 +150,32 @@ export async function readInvestmentOverview( publication: ReportPublication ): Promise { try { - const database = await openPublicationDatabase(publication); - try { + return await readFromSelectedPublication(publication, async (database) => { const json = await readPublicationInvestmentJson(database); return json === null ? null : decodeInvestmentOverview(JSON.parse(json), publication.overview.asOf); - } finally { - await database.destroy(); - } + }); } catch { throw new Error('Unable to read investment overview'); } } -async function openPublicationDatabase(publication: ReportPublication): Promise { +async function readFromSelectedPublication( + publication: ReportPublication, + read: (database: ReportConnection) => Promise +): Promise { const database = await openReadOnlyReportDatabase(publication.reportFile); if (!database) throw new Error('Publication database is unavailable.'); try { // Check the same connection that will serve the read; the slot can change between opens. - if ((await readPublicationRequest(database)) !== publication.publicationKey) + const openedPublicationKey = await readPublicationRequest(database); + if (openedPublicationKey !== publication.publicationKey) { throw new Error('Saved report was replaced.'); - return database; - } catch (error) { + } + return await read(database); + } finally { await database.destroy(); - throw error; } } @@ -187,12 +188,9 @@ export async function readAccountPage( if (!isAccountId(id)) throw new Error(); const account = publication.accounts.find((entry) => entry.id === id); if (!account) throw new Error(); - const database = await openPublicationDatabase(publication); - try { - return await readAccountActivityPage(database, account, options); - } finally { - await database.destroy(); - } + return await readFromSelectedPublication(publication, (database) => + readAccountActivityPage(database, account, options) + ); } catch { throw new Error('Unable to read account transactions'); } @@ -206,12 +204,9 @@ export async function countInvestmentActivities( if (!isAccountId(id)) throw new Error(); const account = publication.accounts.find((entry) => entry.id === id); if (!account) throw new Error(); - const database = await openPublicationDatabase(publication); - try { - return await countAccountInvestmentActivities(database, account); - } finally { - await database.destroy(); - } + return await readFromSelectedPublication(publication, (database) => + countAccountInvestmentActivities(database, account) + ); } catch { throw new Error('Unable to read account activity'); } @@ -226,12 +221,9 @@ export async function readInvestmentActivityPage( if (!isAccountId(id)) throw new Error(); const account = publication.accounts.find((entry) => entry.id === id); if (!account) throw new Error(); - const database = await openPublicationDatabase(publication); - try { - return await readAccountInvestmentActivityPage(database, account, options); - } finally { - await database.destroy(); - } + return await readFromSelectedPublication(publication, (database) => + readAccountInvestmentActivityPage(database, account, options) + ); } catch { throw new Error('Unable to read account activity'); } @@ -247,12 +239,9 @@ export async function readAccountTransaction( throw new Error(); const account = publication.accounts.find((entry) => entry.id === id); if (!account) throw new Error(); - const database = await openPublicationDatabase(publication); - try { - return await readAccountActivityDetail(database, account, nativeIndex); - } finally { - await database.destroy(); - } + return await readFromSelectedPublication(publication, (database) => + readAccountActivityDetail(database, account, nativeIndex) + ); } catch { throw new Error('Unable to read account transactions'); } -- 2.54.1