mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-14 14:40:52 +00:00
fix(medications): end every medication and intake read on the row id
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.
This commit is contained in:
1 parent
7936ab9a57
commit
111047491e
57 files changed
+476
-142
No files matched your search
@@ -38,6 +38,7 @@ import {
|
||||
userDayKey,
|
||||
weekdayOfDateKey,
|
||||
} from "@/lib/tz/format";
|
||||
import { MEDICATION_SCHEDULE_ORDER } from "@/lib/medications/read-order";
|
||||
|
||||
/**
|
||||
* Recommended generic name for the canonical GLP-1 drug brand. The Coach
|
||||
@@ -246,18 +247,18 @@ export async function buildGlp1SnapshotBlock(
|
||||
active: true,
|
||||
},
|
||||
include: {
|
||||
schedules: true,
|
||||
doseChanges: { orderBy: { effectiveFrom: "asc" } },
|
||||
schedules: { orderBy: MEDICATION_SCHEDULE_ORDER },
|
||||
doseChanges: { orderBy: [{ effectiveFrom: "asc" }, { id: "asc" }] },
|
||||
// v1.16.10 — pen inventory reads the per-item entities (the rows
|
||||
// the intake consumption hook moves). The legacy event ledger
|
||||
// stays as a READ fallback for ledger-only accounts: when a
|
||||
// medication has zero items, the running-sum numbers surface so
|
||||
// the Coach still knows the pen count.
|
||||
inventoryItems: { orderBy: { createdAt: "asc" } },
|
||||
inventoryEvents: { orderBy: { occurredAt: "asc" } },
|
||||
inventoryItems: { orderBy: [{ createdAt: "asc" }, { id: "asc" }] },
|
||||
inventoryEvents: { orderBy: [{ occurredAt: "asc" }, { id: "asc" }] },
|
||||
intakeEvents: {
|
||||
where: { takenAt: floor ? { not: null, gte: floor } : { not: null } },
|
||||
orderBy: { takenAt: "desc" },
|
||||
orderBy: [{ takenAt: "desc" }, { id: "desc" }],
|
||||
take: 1,
|
||||
select: {
|
||||
takenAt: true,
|
||||
|
||||
@@ -517,7 +517,7 @@ async function buildCoachSnapshotImpl(
|
||||
createdAt: true,
|
||||
schedules: { select: SCHEDULE_COMPLIANCE_SELECT },
|
||||
scheduleRevisions: {
|
||||
orderBy: { validFrom: "asc" },
|
||||
orderBy: [{ validFrom: "asc" }, { id: "asc" }],
|
||||
select: {
|
||||
id: true,
|
||||
validFrom: true,
|
||||
@@ -533,7 +533,7 @@ async function buildCoachSnapshotImpl(
|
||||
intakeEvents: {
|
||||
// Tombstoned intake rows must never reach the Coach snapshot.
|
||||
where: { deletedAt: null, scheduledFor: { gte: cutoff } },
|
||||
orderBy: { scheduledFor: "asc" },
|
||||
orderBy: [{ scheduledFor: "asc" }, { id: "asc" }],
|
||||
select: {
|
||||
scheduledFor: true,
|
||||
takenAt: true,
|
||||
|
||||
Reference in new issue
Block a user