feat(db): events are identified by unit + time + type (#51)

The events table was UNIQUE(serial, timestamp): one event per unit per
second.  A unit records a waveform and a histogram in the same second
legitimately -- 209 times in prod's 2026-09-30 store once histograms
carry Blastware's start -- so the key is now (serial, timestamp,
record_type).  Agreed with Brian 2026-11-03.

- Migration 3 rebuilds the table from its own current definition with
  only the key swapped, in one transaction: every column, row and id is
  kept (Terra-View reads rows by id) and the indexes are recreated.
  Migration 1's check no longer mistakes the new key for a missing one.
- The upsert, the estimate guard and the restamp match on the type too.
  The restamp also re-types a file's own row in place: before v0.33.0 a
  row's type came from the filename, and a re-import under the file's
  own type must not add a second row.
- An untyped event still refreshes the untyped row at its second (a
  UNIQUE key treats NULLs as distinct).
- scripts/repair_unknown_serials.py checks the new key.

No time changes yet: that is the next commit, and they ship together.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ALX2R8pghDaZuti9U7mEE9
This commit is contained in:
serversdownandClaude Opus 5.5 committed 2026-11-03 21:29:46 -05:00
1 parent e8242aaadc
commit ea2dfc21b7
3 files changed
+193 -23

No files matched your search

+5 -4
View File
@@ -73,7 +73,7 @@ def main(argv: list[str] | None = None) -> int:
conn.row_factory = sqlite3.Row
rows = list(conn.execute(
"SELECT id, serial, timestamp, blastware_filename "
"SELECT id, serial, timestamp, record_type, blastware_filename "
" FROM events "
" WHERE serial = 'UNKNOWN' "
" ORDER BY timestamp",
@@ -98,10 +98,11 @@ def main(argv: list[str] | None = None) -> int:
unresolved += 1
continue
# Check for an existing row at the target (serial, timestamp).
# Check for an existing row at the target key (serial, timestamp,
# record_type -- #51).
existing = conn.execute(
"SELECT id FROM events WHERE serial = ? AND timestamp = ?",
(new_serial, ts),
"SELECT id FROM events WHERE serial = ? AND timestamp = ? AND record_type IS ?",
(new_serial, ts, row["record_type"]),
).fetchone()
action: str
if existing is None: