mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-08 20:42:43 +00:00
The dense intra-day retention fold minted its daily-mean stats: row with an externalId-keyed upsert. On real data a row already occupies the canonical local-noon instant — a sibling daily stats: row or a per-sample HRV/PULSE reading that fell on noon — so the upsert lookup missed (the existing row carries a different externalId) and the INSERT then violated the (user_id, type, measured_at, source, sleep_stage) unique index with P2002. The mocked unit tests could not surface it. Resolve the canonical row by the measured_at composite the collision is on (sleep_stage IS NULL for these continuous types, matched via the NULLS NOT DISTINCT index) and adopt it in place — refresh the mean, stamp the canonical stats: externalId, un-tombstone — or create it when absent. Exclude the adopted row id from the per-sample soft-delete so a noon-instant sample becomes the daily row instead of being tombstoned. Add a testcontainer integration test that seeds the colliding shape (a pre-existing canonical-instant row plus out-of-window per-sample rows) and pins no-P2002, coexistence, the noon-sample case, idempotency, and the retention bound. Update the unit coverage for the adopt-in-place path.