Centralize reads from the selected publication #76
No files matched your search
@@ -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.
|
||||
@@ -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.
|
||||
|
||||
@@ -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<typeof vi.fn>[] = [];
|
||||
const closeStarted = Promise.withResolvers<void>();
|
||||
const releaseClose = Promise.withResolvers<void>();
|
||||
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(
|
||||
|
||||
@@ -150,31 +150,32 @@ export async function readInvestmentOverview(
|
||||
publication: ReportPublication
|
||||
): Promise<PreparedInvestmentOverview | null> {
|
||||
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<ReportConnection> {
|
||||
async function readFromSelectedPublication<Result>(
|
||||
publication: ReportPublication,
|
||||
read: (database: ReportConnection) => Promise<Result>
|
||||
): Promise<Result> {
|
||||
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');
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user