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

@@ -13,11 +13,21 @@ describe("measurement validation", () => {
expect(getUnitForType("BLOOD_PRESSURE_DIA")).toBe("mmHg");
expect(getUnitForType("PULSE")).toBe("bpm");
expect(getUnitForType("BODY_FAT")).toBe("%");
expect(getUnitForType("SLEEP_DURATION")).toBe("hours");
// v1.4.23 — sleep duration shifted from hours to minutes so per-stage
// HealthKit category samples can be stored without precision loss.
expect(getUnitForType("SLEEP_DURATION")).toBe("minutes");
expect(getUnitForType("ACTIVITY_STEPS")).toBe("steps");
expect(getUnitForType("BLOOD_GLUCOSE")).toBe("mg/dL");
expect(getUnitForType("TOTAL_BODY_WATER")).toBe("kg");
expect(getUnitForType("BONE_MASS")).toBe("kg");
// ── v1.4.23 Apple Health canonical units ──
expect(getUnitForType("HEART_RATE_VARIABILITY")).toBe("ms");
expect(getUnitForType("RESTING_HEART_RATE")).toBe("bpm");
expect(getUnitForType("ACTIVE_ENERGY_BURNED")).toBe("kcal");
expect(getUnitForType("FLIGHTS_CLIMBED")).toBe("flights");
expect(getUnitForType("WALKING_RUNNING_DISTANCE")).toBe("m");
expect(getUnitForType("VO2_MAX")).toBe("mL/(kg·min)");
expect(getUnitForType("BODY_TEMPERATURE")).toBe("celsius");
});
it("returns 'unknown' for unrecognised types", () => {