Files
HealthLog/prisma/migrations/0389_measurement_fold_repair/migration.sql
T
Marc-André Bombeck adc7baca14 Fold only complete local days and repair means folded from part of one
The daily-mean consolidation and the dense hourly fold cut at
`now - threshold` instead of a local day boundary. A day was folded in two
runs and the second run overwrote the stored mean with the mean of the
later part alone: every mean-type day, and the boundary hour of every
heart-rate, HRV and SpO2 day, plus the derived resting figure and the DAY
rollup of such a day. The Health Connect importer split its raw window at
the same unaligned instant.

- Fold up to the start of the account's local day the threshold ends in
  (`foldBoundary`), for every consolidation pass and the Health Connect
  importer's raw/hourly split.
- A window that already has a live `stats:` row is refolded from every
  sample of it, live rows plus the fold's own tombstones; with none left
  the stored mean stays.
- `folded_window` refuses a raw sample only when its local day lies before
  the aligned boundary and a live `stats:` row of its source covers it,
  also within reach of the sample after a timezone change.
- A deletion inside a day the fold had not finished is class B.
- Add a one-time, resumable fold repair per account that recomputes each
  hourly and daily mean from its remaining samples and writes only values
  that differ, recorded in `measurement_fold_repairs` (migration 0389).
- The compaction-tombstone purge waits for the repair per account, and the
  boot sends no purge run before one account is repaired.
- The restore keys live `stats:` ids by source, and writes compaction
  tombstones back from a file older than the account's repair, resetting
  the repair.
2026-10-08 18:42:01 +02:00

13 lines
668 B
SQL

-- One row per account once the fold repair has recomputed the daily and
-- hourly means an older release folded in two runs. The compaction-tombstone
-- purge waits for it, because the tombstones it deletes are what the repair
-- reads. Holds no health data; the account's deletion takes it along.
CREATE TABLE "measurement_fold_repairs" (
"user_id" TEXT NOT NULL,
"completed_at" TIMESTAMP(3) NOT NULL,
CONSTRAINT "measurement_fold_repairs_pkey" PRIMARY KEY ("user_id")
);
ALTER TABLE "measurement_fold_repairs" ADD CONSTRAINT "measurement_fold_repairs_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;