feat(schema): Apple Health measurement types + sleep stages + dedup index

Extend MeasurementType with the seven HealthKit-shaped values the iOS
batch ingest needs (HRV, resting HR, active energy, flights climbed,
distance, VO2 max, body temperature) and add APPLE_HEALTH to the
MeasurementSource enum. Sleep duration unit shifts from hours to
minutes so per-stage HKCategorySample rows persist without precision
loss; the new SleepStage enum mirrors the existing GlucoseContext
column shape and a CHECK constraint scopes it to SLEEP_DURATION rows.

The composite unique index `(user_id, type, source, external_id)`
gives the new batch endpoint an idempotent dedup key while the legacy
`(user_id, type, measured_at, source)` index continues to back the
manual UI's no-duplicate-at-the-same-wall-clock guard. NULL externalId
rows stay distinct under Postgres semantics so manual entries never
collide.

Migration is strictly additive: new enum values append, new columns
are nullable, no rows mutate.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Marc-André BombeckandClaude Opus 4.7 committed 2026-05-11 00:15:56 +02:00
1 parent 8569a1879f
commit 95209ebf79
9 files changed
+256 -8

No files matched your search

@@ -26,10 +26,18 @@ const EXPECTED_TYPES = [
"TOTAL_BODY_WATER",
"BONE_MASS",
"OXYGEN_SATURATION",
// ── v1.4.23 Apple Health additions ──
"HEART_RATE_VARIABILITY",
"RESTING_HEART_RATE",
"ACTIVE_ENERGY_BURNED",
"FLIGHTS_CLIMBED",
"WALKING_RUNNING_DISTANCE",
"VO2_MAX",
"BODY_TEMPERATURE",
] as const;
describe("measurementTypeEnum coverage", () => {
it("exposes the 11 canonical measurement types", () => {
it("exposes the 18 canonical measurement types", () => {
expect([...measurementTypeEnum.options].sort()).toEqual(
[...EXPECTED_TYPES].sort(),
);
@@ -39,12 +47,24 @@ describe("measurementTypeEnum coverage", () => {
// - BLOOD_GLUCOSE renders through the per-context `glucoseStats` section
// - SLEEP_DURATION + ACTIVITY_STEPS are intentionally omitted from the
// clinical PDF (lifestyle, not a vital sign — see source comment).
// - v1.4.23 Apple Health metrics (HRV, resting HR, active energy,
// flights, distance, VO2 max, body temperature) are excluded from
// the v1.4.23 release of the doctor PDF — they ship into the
// clinical surface alongside the iOS app's first paying-customer
// sync in v1.5 once layout + reference ranges are agreed.
// Updates to this set MUST be paired with a comment in
// doctor-report-pdf-core.ts so the rationale stays discoverable.
const PDF_VITAL_EXCLUSIONS = new Set([
"BLOOD_GLUCOSE",
"SLEEP_DURATION",
"ACTIVITY_STEPS",
"HEART_RATE_VARIABILITY",
"RESTING_HEART_RATE",
"ACTIVE_ENERGY_BURNED",
"FLIGHTS_CLIMBED",
"WALKING_RUNNING_DISTANCE",
"VO2_MAX",
"BODY_TEMPERATURE",
]);
it("doctor-report PDF vital types cover the canonical enum minus documented exclusions", () => {