mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-08 20:42:43 +00:00
The Apple Health ZIP import upserted each spot reading on the (userId, type, source, externalId) index only. When two readings shared the natural key (userId, type, measuredAt, source, sleepStage) with different externalIds — a re-import of overlapping data, or two same-instant Apple samples from different source apps — the externalId upsert missed and the create collided on the second unique index (migration 0055), raising P2002 and aborting the entire import (issue #486, after the v1.28.36 dedup fix let the upload run). flushSpotBatch now wraps the per-row upsert: on P2002 it probes the occupying row by the natural key (tombstone-inclusive) and adopts it in place — re-keys to the incoming externalId, takes the new value/unit, resurrects if soft-deleted — mirroring the v1.28.39 provider natural-key rescues (closest to withings/sync-sleep). If no twin resolves it, the one reading is skipped and the run continues; the catch sits in the per-row loop outside any transaction, so a collision can never abort the batch or the import. Adopted rows count as updated, skipped ones surface honestly. Workout + stats: paths verified unable to hit this (no second unique index / deterministic externalId). Repro + real-Postgres integration tests added.