refactor(medications): drop the compliance rollup table that nothing reads

medication_compliance_rollups lost its last reader when compliance moved
to the schedule-anchored computation, yet every dose write still upserted
it, a boot backfill refolded it, and the wipe and backup plans carried it.

The per-dose hook keeps the one derived row that still hangs off a dose
day, the supplement nutrients, under dose-day-refresh. The pending-row
mints (today projection, reminder) no longer call it, since an untaken
dose adds no nutrients. The import job finishes without its per-day
recompute phase; its progress keeps touchedDays and rollupProcessed on
the wire, inert and documented as deprecated. The backfill queue is
retired on boot and migration 0414 drops the table under a bounded lock
wait.
This commit is contained in:
Marc-André Bombeck committed 2026-10-20 13:01:45 +02:00
1 parent b90c82cba3
commit 41de0ca126
71 files changed
+314 -2027

No files matched your search

@@ -23,14 +23,6 @@ vi.mock("@/lib/db-compat", () => ({
ensureDbCompatibility: vi.fn().mockResolvedValue(undefined),
}));
// v1.37.19 — the zone change enqueues a compliance re-fold.
const refoldMock = vi.hoisted(() =>
vi.fn(async () => ({ enqueued: true, error: null })),
);
vi.mock("@/lib/rollups/medication-compliance-rollups", () => ({
enqueueUserMedicationComplianceBackfill: refoldMock,
}));
// The supplement nutrient rows are keyed by the same local day.
const nutrientMock = vi.hoisted(() => vi.fn(async () => ({ queued: true })));
vi.mock("@/lib/medications/ingredients/recompute-job", () => ({
@@ -110,11 +102,9 @@ describe("PUT /api/auth/me/timezone", () => {
expect(res.status).toBe(422);
});
// Watched red: with the re-fold enqueue removed from the route (the
// pre-v1.37.19 write), the changed-zone case fails — the compliance
// rollup's day keys were minted under the old zone and nothing ever
// re-bucketed them (the coverage probe compares counts, not keys).
it("enqueues a compliance re-fold when the zone actually changes", async () => {
// The supplement nutrient rows' day keys were minted under the old zone;
// without the enqueue nothing re-buckets them.
it("enqueues the nutrient recompute when the zone actually changes", async () => {
vi.mocked(getSession).mockResolvedValue(SESSION_OK as never);
vi.mocked(prisma.user.findUnique).mockResolvedValue({
timezone: "Europe/Berlin",
@@ -123,11 +113,10 @@ describe("PUT /api/auth/me/timezone", () => {
const res = await PUT(mkReq({ timezone: "America/New_York" }));
expect(res.status).toBe(200);
expect(refoldMock).toHaveBeenCalledWith("user-1");
expect(nutrientMock).toHaveBeenCalledWith("user-1");
});
it("does not re-fold when the zone is unchanged", async () => {
it("does not recompute when the zone is unchanged", async () => {
vi.mocked(getSession).mockResolvedValue(SESSION_OK as never);
vi.mocked(prisma.user.findUnique).mockResolvedValue({
timezone: "Europe/Berlin",
@@ -136,7 +125,6 @@ describe("PUT /api/auth/me/timezone", () => {
const res = await PUT(mkReq({ timezone: "Europe/Berlin" }));
expect(res.status).toBe(200);
expect(refoldMock).not.toHaveBeenCalled();
expect(nutrientMock).not.toHaveBeenCalled();
});
+5 -17
View File
@@ -30,7 +30,6 @@ import { annotate } from "@/lib/logging/context";
import { auditLog } from "@/lib/auth/audit";
import { invalidateUserTimezone, isValidTimezone } from "@/lib/tz/resolver";
import { enqueueSupplementNutrientRecompute } from "@/lib/medications/ingredients/recompute-job";
import { enqueueUserMedicationComplianceBackfill } from "@/lib/rollups/medication-compliance-rollups";
export const PUT = apiHandler(async (request: NextRequest) => {
const { user } = await requireAuth();
@@ -62,24 +61,13 @@ export const PUT = apiHandler(async (request: NextRequest) => {
invalidateUserTimezone(user.id);
// v1.37.19 — the medication-compliance rollup keys its rows by
// the LOCAL day the dose fell on, minted under the timezone in force at
// write time. A zone change re-buckets those days, so the stored keys go
// stale (and the coverage probe compares counts, not keys — it cannot
// notice). Re-fold the trailing window through the ordinary backfill
// job; best-effort, the singleton key collapses rapid re-picks.
// The supplement nutrient rows are keyed by the LOCAL day the dose fell
// on, minted under the timezone in force at write time. A zone change
// re-buckets those days, so the account's rows are recomputed through
// the ordinary job; best-effort, the singleton key collapses rapid
// re-picks.
if ((before?.timezone ?? null) !== tz) {
const refold = await enqueueUserMedicationComplianceBackfill(user.id);
// The supplement nutrient rows are keyed by the same local day.
await enqueueSupplementNutrientRecompute(user.id);
annotate({
meta: {
timezone_compliance_refold_enqueued: refold.enqueued,
...(refold.error
? { timezone_compliance_refold_error: refold.error }
: {}),
},
});
}
await auditLog("user.timezone.update", {