perf(db): the calendar export, the journey reads and the stop reorders read and write in batches

The ICS export reads the stops and notes of all dated days in two statements instead of two per day, the journey's full read and entry list take the source trip titles in one join instead of one read per entry, and reordering a day's stops or sorting it by time writes every position in one CASE update. Results are unchanged: each day keeps its order (the id settles a tie), a missing trip still names null, and an id listed twice keeps its last position.
This commit is contained in:
Maurice
2026-10-10 12:37:51 +02:00
parent 4a3c97e500
commit e316ccf3bb
11 changed files with 163 additions and 61 deletions
@@ -407,6 +407,32 @@ describe('DayAssignmentsRepository — AS10/AS11/AS13/AS14/AS19 (delete / order
expect(await orderOf(a.id)).toBe(3);
});
it('ASSIGNREPO-009b (setOrderIndexes): one statement leaves what the per-row writes left, day scope included', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id);
const otherDay = createDay(testDb, trip.id);
const place = createPlace(testDb, trip.id);
const a = createDayAssignment(testDb, day.id, place.id, { order_index: 0 });
const b = createDayAssignment(testDb, day.id, place.id, { order_index: 1 });
const c = createDayAssignment(testDb, day.id, place.id, { order_index: 2 });
const foreign = createDayAssignment(testDb, otherDay.id, place.id, { order_index: 5 });
// b twice: the later index wins, as two writes in a row would leave it; foreign is on another day.
await withRequestContext(t.orm, () =>
assignments.setOrderIndexes([{ id: c.id, order_index: 0 }, { id: b.id, order_index: 9 }, { id: a.id, order_index: 2 }, { id: b.id, order_index: 1 }, { id: foreign.id, order_index: 0 }], day.id),
);
expect([await orderOf(a.id), await orderOf(b.id), await orderOf(c.id), await orderOf(foreign.id)]).toEqual([2, 1, 0, 5]);
// Unscoped: by id alone, and only the listed rows.
await withRequestContext(t.orm, () => assignments.setOrderIndexes([{ id: foreign.id, order_index: 1 }]));
expect([await orderOf(foreign.id), await orderOf(a.id)]).toEqual([1, 2]);
// Nothing to write is no statement at all.
await withRequestContext(t.orm, () => assignments.setOrderIndexes([], day.id));
expect(await orderOf(c.id)).toBe(0);
});
it('ASSIGNREPO-010 (AS13, getDayId): the assignment\'s day_id, undefined for a missing id', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
@@ -185,7 +185,7 @@ describe('ReservationsRepository — CL2 (listForCalendar)', () => {
});
});
describe('ReservationsRepository — CL4 (listCalendarStops)', () => {
describe('ReservationsRepository — CL4 (listCalendarStopsForDays)', () => {
it('CAL-CL4-001: matches the legacy per-day statement, excluding a booked-night stop', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id, { start_date: '2026-06-01', end_date: '2026-06-03' });
@@ -197,7 +197,7 @@ describe('ReservationsRepository — CL4 (listCalendarStops)', () => {
const accId = await insertRow(await orm(), DayAccommodations, { trip: trip.id, place: hotelPlace.id, startDay: day.id, endDay: day.id });
await insertRow(await orm(), DayAssignments, { day: day.id, place: hotelPlace.id, order_index: 1, accommodation_id: accId });
const typed = await reservationsRepo.listCalendarStops(day.id);
const typed = await reservationsRepo.listCalendarStopsForDays([day.id]);
// test-sql-allow: the legacy statement is the oracle the repository read is held to.
const legacy = testDb.prepare(`
SELECT da.*, p.name as place_name, p.address as place_address,
@@ -488,6 +488,24 @@ describe('exportICS', () => {
expect(ics).toContain('LOCATION:Rue de Rivoli');
});
it('CAL-016b: read in one go for all days, each day keeps its own stops and notes in their order', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id, { title: 'Two Days' });
const first = createDay(testDb, trip.id, { date: '2025-06-02', title: 'First' });
const second = createDay(testDb, trip.id, { date: '2025-06-03', title: 'Second' });
const place = (name: string) => createPlace(testDb, trip.id, { name });
createDayAssignment(testDb, first.id, place('Later').id, { order_index: 1 });
createDayAssignment(testDb, first.id, place('Sooner').id, { order_index: 0 });
createDayAssignment(testDb, second.id, place('Elsewhere').id, { order_index: 0 });
createDayNote(testDb, first.id, trip.id, { text: 'second note', sort_order: 1 });
createDayNote(testDb, first.id, trip.id, { text: 'first note', sort_order: 0 });
const ics = (await svc.exportICS(trip.id)).ics.replace(/\r\n /g, '');
expect(ics).toContain('SUMMARY:First\r\nDESCRIPTION:• Sooner\\n• Later\\n\\nNotes:\\n• first note\\n• second note\r\n');
expect(ics).toContain('SUMMARY:Second\r\nDESCRIPTION:• Elsewhere\r\n');
});
it('CAL-016: a day without a date is skipped and a titled day uses its own title', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id, { title: 'Mixed Days' });
@@ -2729,6 +2729,23 @@ describe('entry enrichment', () => {
expect((entry as any).source_trip_name).toBe('Japan 2026');
});
it('JOURNEY-SVC-ENRICH-001b: entries from two trips each carry their own trip name, in the full read and the list alike', async () => {
const { user } = createUser(testDb);
const a = tripWithPlace(user.id).trip;
const b = tripWithPlace(user.id).trip;
await updateRows(await orm(), Trips, { id: a.id }, { title: 'Alps' });
await updateRows(await orm(), Trips, { id: b.id }, { title: 'Baltic' });
const journey = await svc.createJourney(user.id, { title: 'J', trip_ids: [a.id, b.id] });
await svc.createEntry(journey.id, user.id, { entry_date: '2026-05-01', title: 'Solo' });
const names = (rows: Array<{ source_trip_id?: number | null; source_trip_name?: string | null }>) =>
rows.map((e) => [e.source_trip_id ?? null, e.source_trip_name]).sort((x, y) => Number(x[0]) - Number(y[0]));
const listed = names((await svc.listEntries(journey.id, user.id))!);
expect(listed).toEqual([[null, null], [a.id, 'Alps'], [b.id, 'Baltic']].sort((x, y) => Number(x[0]) - Number(y[0])));
const full = (await svc.getJourneyFull(journey.id, user.id))! as unknown as { entries: Array<{ source_trip_id?: number | null; source_trip_name?: string | null }> };
expect(names(full.entries)).toEqual(listed);
});
it('JOURNEY-SVC-ENRICH-002: an entry with no trip and no tags gets [] and null, not undefined', async () => {
const { user } = createUser(testDb);
const journey = await svc.createJourney(user.id, { title: 'J' });