From 22a68df94fb267724505781ba83893d7ce5a75fd Mon Sep 17 00:00:00 2001 From: Pipiche Date: Sun, 4 Oct 2026 19:44:29 +0200 Subject: [PATCH 1/2] db: reunite rows stranded under a dead device_id (schema 59) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pairing minted a fresh `-xxxxxxxx` every run, so re-pairing ONE physical device forked it into N identities. The id is the storage key for `decoded_onehz`, `decoded_rr`, `raw_archive` and every other per-device table, so each earlier pairing's rows were left under an id no `device` row points at — unreachable by every reader, because all of them start from a `device` row. `pairOuraRing` stopped minting a second id (#530), but that is forward-only: it reuses the id the surviving row carries, which is not the id the stranded rows are under. Nothing reunites them without this. Seen on a device: a `device` row `oura-a4487268` beside 96,521 `raw_archive` frames, 1,765 `decoded_onehz` rows and all four `sync_cursor` bookmarks under `oura-7e117c66`. `miband234-` and `o2ring-` still mint one id per pairing, so the heal is written per device FAMILY rather than for Oura alone. A stranded id moves onto the surviving row of its own family (text up to the last `-`, so `kPrimaryDeviceId` has none and the primary band is never re-keyed). A family with no surviving row is left alone — there is nothing to reunite it with, and minting a `device` row would claim a pairing that does not exist. A family with MORE THAN ONE surviving row is skipped: two rows means there is no single answer to "which device is this", and picking one would merge two devices' measurements into one history. The `sync_cursor` bookmarks are DELETED, not carried. A stranded id cannot be shown to be the same physical device — `pairOuraRing` only carries its cursor when `remote_id` still matches — and a foreign decisecond origin would stamp this device's seconds against the wrong wall clock. Dropping costs one full re-read; keeping risks timestamps the data does not support. Tables are discovered via `PRAGMA table_info`, not listed: a hand-written list is how one table gets missed and goes on serving rows under a dead id (AGENTS.md §4.7). `UPDATE OR REPLACE` because a constraint failure inside `onUpgrade`'s one exclusive transaction would roll the whole ladder back and quarantine the database (invariant 11). Verification: `flutter analyze` clean; 6 new cases in `test/reunite_stranded_device_rows_test.dart` (reunite, idempotence, the primary band, cross-family refusal, the ambiguous family, the colliding key). Rehearsed against the real 16.8 MB device database: all 98,286 rows moved, none lost, four dead cursors dropped, 0.35 s. NOT verified on hardware, and no `kAlgoVersion` bump — see the PR description for why that is a question for the maintainer rather than a decision taken here. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01WghYW3iriwtRErXYnj7d7D --- lib/data/db.dart | 115 ++++++++++++++++- test/reunite_stranded_device_rows_test.dart | 136 ++++++++++++++++++++ 2 files changed, 250 insertions(+), 1 deletion(-) create mode 100644 test/reunite_stranded_device_rows_test.dart diff --git a/lib/data/db.dart b/lib/data/db.dart index ed24a5b2c..d2f29d425 100644 --- a/lib/data/db.dart +++ b/lib/data/db.dart @@ -383,7 +383,7 @@ class LocalDb { /// pass it: sqflite throws `ArgumentError('onCreate must be null if no /// version is specified')` BEFORE opening anything when `onCreate` is given /// without `version` (sqflite_common database_mixin.dart). - static const int schemaVersion = 58; + static const int schemaVersion = 59; /// SQLite caps host parameters per statement (`SQLITE_MAX_VARIABLE_NUMBER` — /// only 999 on the builds shipped with older Android/iOS). Any `IN (?, ?, …)` @@ -1138,6 +1138,13 @@ class LocalDb { // A band's own hypnogram (`vendor_staged`). New table only. await _createVendorSleepEpoch(db); } + if (oldV < 59) { + // One-time heal of the rows every re-pairing stranded under a dead + // `device_id`. Here and not in `_repairOpenSchema`: pairing no longer + // mints a second id for a ring that already has a row, so once is + // enough — same reasoning as `_scrubImportedSkinTempZ`. + await reuniteStrandedDeviceRows(db); + } }, onOpen: (db) async { await _repairOpenSchema(db); @@ -1240,6 +1247,112 @@ class LocalDb { await _dropRawStore(db); } + /// v59: move rows stranded under a dead `device_id` onto the row that lived. + /// + /// Pairing minted a fresh `-xxxxxxxx` every run (fixed for Oura in + /// `pairOuraRing`; `miband234-` and `o2ring-` still mint one per pairing), so + /// re-pairing ONE physical device forked it into N identities. The id is the + /// storage key for `decoded_onehz`, `decoded_rr`, `raw_archive` and every + /// other per-device table, so each earlier pairing's rows are stranded under + /// an id no `device` row points at — unreachable by every reader, because all + /// of them start from a `device` row. + /// + /// WHICH ROWS MOVE. A stranded id moves onto the surviving row of its own + /// FAMILY — the text up to and including the last `-`, so `oura-7e117c66` + /// joins `oura-a4487268` and never an `o2ring-` row. A family with no + /// surviving row keeps its rows where they are: there is nothing to reunite + /// them with, and minting a `device` row would claim a pairing that does not + /// exist. A family with MORE THAN ONE surviving row is skipped too — two rows + /// means there is no single answer to "which device is this", and picking one + /// would merge two devices' measurements into one history, which is a + /// fabricated number by a slower route. + /// + /// WHICH ROWS DO NOT. The `sync_cursor` bookmarks (`:`) are + /// DELETED rather than carried over. A stranded id cannot be shown to be the + /// same physical device — `pairOuraRing` only carries its cursor across when + /// `remote_id` still matches — and another device's decisecond origin would + /// stamp this one's seconds against the wrong wall clock. Dropping them costs + /// one full re-read; keeping them risks timestamps the data does not support. + /// + /// `UPDATE OR REPLACE` because these tables are keyed `(device_id,