Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion internal/store/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,19 @@ func (s *PostgresStore) InsertW(w models.BodyWeight) error {
if w.RecordedAt != "" {
recordedAt = w.RecordedAt
}
// Dedup identical same-day readings. The bt-scale gateway burst-posts the
// same value several times as the reading settles/re-sends, which would
// otherwise pile up duplicate rows (the reason DeleteW deletes by date).
// A genuinely different value on the same day still inserts, so multiple
// distinct weigh-ins per day remain supported (latest recorded_at wins for
// display). NUMERIC comparison ignores decimal-string formatting, so
// "272.2" and "272.20" are treated as equal.
_, err := s.pool.Exec(context.Background(),
"INSERT INTO weight (date, recorded_at, weight) VALUES ($1::date, COALESCE($2::timestamptz, NOW()), $3)",
`INSERT INTO weight (date, recorded_at, weight)
SELECT $1::date, COALESCE($2::timestamptz, NOW()), $3::numeric
WHERE NOT EXISTS (
SELECT 1 FROM weight WHERE date = $1::date AND weight = $3::numeric
)`,
w.Date, recordedAt, w.Weight.String())
return err
}
Expand Down
Loading