mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 16:12:11 +00:00
Postgres returns rows that tie on an ORDER BY in no particular order, and not the same one twice. The medication reads ordered by columns two rows share all the time: every read that hands schedules on included them with no order at all, medications were listed by createdAt alone (one import writes many in a millisecond), intakes by scheduledFor (a twice-daily medication puts every morning dose on one instant), and "the latest intake" lookups by takenAt. A client read the same account in a different order from one request to the next, and a latest-intake lookup picked either of two rows. Every findMany / findFirst on a medication table, nested includes included, now ends its order on the id in the direction of its last key, and schedules are read by window, then id (MEDICATION_SCHEDULE_ORDER, the order the backup already wrote). medication-read-order-guard.test.ts holds the tree to it. medication-read-order.test.ts writes tying rows in the opposite order to the one each read promises, through the medication list and today's intakes, and fails without the tie-breakers. The parity suite's own state reader had the same gap: it sorts rows by content with the ids blanked, and rows that tie then kept whatever order the unordered read returned, so their <id:N> labels swapped under a loaded run. Its reads are ordered by id, which for a cuid is the order the rows were written in. The JSON import's schema left the route, so its entry in the inline schema strictness register goes; the schema module says why its objects are not strict.
23 lines
1.0 KiB
TypeScript
23 lines
1.0 KiB
TypeScript
/**
|
|
* The order a medication's rows are read in, ties included.
|
|
*
|
|
* Postgres returns rows that tie on an `ORDER BY` in no particular order, and
|
|
* not the same order twice: the heap layout, a vacuum or a parallel plan all
|
|
* change it. A read ordered by a column two rows can share (two schedules at
|
|
* 08:00 on different medications, two intakes in the same slot, two
|
|
* medications created in one import) therefore answered its client a
|
|
* different order from one request to the next, and a "latest" lookup picked
|
|
* either of two rows. Every medication and intake read ends its order on the
|
|
* row id, and `medication-read-order-guard.test.ts` holds the tree to it.
|
|
*/
|
|
import type { Prisma } from "@/generated/prisma/client";
|
|
|
|
/**
|
|
* A medication's schedules, as every reader that hands them on includes
|
|
* them: by window, then by id, the order the backup already writes.
|
|
*/
|
|
export const MEDICATION_SCHEDULE_ORDER = [
|
|
{ windowStart: "asc" },
|
|
{ id: "asc" },
|
|
] as const satisfies Prisma.MedicationScheduleOrderByWithRelationInput[];
|