diff --git a/lib/data/db.dart b/lib/data/db.dart index ed24a5b2c..d517d0846 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,133 @@ 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 IGNORE` then a delete of what stayed behind, because these + /// tables are keyed `(device_id, )`: when the same + /// key exists under both ids one row must win rather than a constraint + /// failure, which inside `onUpgrade`'s one exclusive transaction would roll + /// the whole ladder back and quarantine the database (invariant 11). The + /// SURVIVING id's row wins. It is the one every reader already serves, so a + /// collision never changes a value on screen, and a per-device setting such + /// as a `signal_priority` rank the user set on the current pairing is never + /// overwritten by the stale one (`OR REPLACE` would let the stranded row win). + /// + /// Idempotent: once a stranded id is gone nothing matches it, so a re-run + /// does no work. Returns how many ids were reunited. + @visibleForTesting + static Future reuniteStrandedDeviceRows(Database db) async { + // The text up to and including the last `-`, or null when there is none. + // `kPrimaryDeviceId` ('') lands on null, which is the point: the primary + // band is never re-keyed. + String? familyOf(String id) { + final cut = id.lastIndexOf('-'); + return cut <= 0 ? null : id.substring(0, cut + 1); + } + + // A ladder that starts below the rung creating `device` reaches here + // without it (`_repairOpenSchema` only adds it in onOpen, AFTER this). No + // `device` row means nothing to reunite with, and querying the missing + // table would throw inside `onUpgrade` and quarantine the database. + final hasDevice = (await db.rawQuery( + "SELECT 1 FROM sqlite_master WHERE type='table' AND name='device'", + )) + .isNotEmpty; + if (!hasDevice) return 0; + + final live = { + for (final r in await db.query('device', columns: ['id'])) + (r['id'] as String?) ?? '', + }; + final target = {}; + final ambiguous = {}; + for (final id in live) { + final fam = familyOf(id); + if (fam == null) continue; + if (target.containsKey(fam)) { + ambiguous.add(fam); + } else { + target[fam] = id; + } + } + for (final fam in ambiguous) { + target.remove(fam); + } + if (target.isEmpty) return 0; + + // Every device-keyed table, DISCOVERED rather than listed: a hand-written + // list is how one table gets missed and goes on serving rows under a dead + // id long after the rest were healed (AGENTS.md §4.7). + final tables = []; + for (final t in await db.rawQuery( + "SELECT name FROM sqlite_master WHERE type='table' " + "AND name NOT LIKE 'sqlite_%'", + )) { + final name = t['name'] as String; + final info = await db.rawQuery('PRAGMA table_info("$name")'); + if (info.any((c) => c['name'] == 'device_id')) tables.add(name); + } + if (tables.isEmpty) return 0; + + final stranded = {}; + for (final t in tables) { + for (final r + in await db.rawQuery('SELECT DISTINCT device_id FROM "$t"')) { + final id = r['device_id'] as String?; + if (id == null || id.isEmpty || live.contains(id)) continue; + if (target.containsKey(familyOf(id))) stranded.add(id); + } + } + + for (final old in stranded) { + final to = target[familyOf(old)]!; + for (final t in tables) { + await db.rawUpdate( + 'UPDATE OR IGNORE "$t" SET device_id = ? WHERE device_id = ?', + [to, old], + ); + // Only rows whose key the surviving id already holds are left here. + await db.delete(t, where: 'device_id = ?', whereArgs: [old]); + } + // The bookmarks, dropped and not moved — see this method's header. An + // exact suffix match, not LIKE: LIKE treats `_` as a wildcard and ignores + // ASCII case, so it could drop a bookmark that belongs to another id. + await db.delete( + 'sync_cursor', + where: 'substr(name, ?) = ?', + whereArgs: [-(old.length + 1), ':$old'], + ); + } + // The COUNT is the report. This file deliberately carries no logger of its + // own (see `runMaintenance`'s note) — a caller that wants a line prints it. + return stranded.length; + } + /// Older WHOOP CSV imports filed the export's absolute skin temp (°C) under /// `scalars.skin_temp_z`, which every reader treats as SDs off baseline, so /// 33 °C read as +33 SD in the cross-day temp inputs and the skin temp card. diff --git a/test/reunite_stranded_device_rows_test.dart b/test/reunite_stranded_device_rows_test.dart new file mode 100644 index 000000000..77fdc60e1 --- /dev/null +++ b/test/reunite_stranded_device_rows_test.dart @@ -0,0 +1,167 @@ +// Rows stranded under a dead device_id are moved onto the row that lived. +// +// Pairing minted a fresh `-xxxxxxxx` every run, so re-pairing one +// physical ring forked it into N identities and every earlier pairing's rows +// were left under an id no `device` row points at — unreachable, because every +// reader starts from a `device` row. Seen on a real phone: a `device` row +// `oura-a4487268` beside 96,521 `raw_archive` frames and 1,765 `decoded_onehz` +// rows under `oura-7e117c66`. +import 'package:flutter_test/flutter_test.dart'; +import 'package:path/path.dart' as p; +import 'package:sqflite_common_ffi/sqflite_ffi.dart'; +import 'package:openstrap_edge/data/db.dart'; + +Future _open() async { + final dir = await databaseFactory.getDatabasesPath(); + final path = p.join(dir, 'reunite_test_${DateTime.now().microsecondsSinceEpoch}.db'); + await databaseFactory.deleteDatabase(path); + final db = await databaseFactory.openDatabase(path); + // The shapes this heal touches, reduced to the columns it keys on. + await db.execute('CREATE TABLE device (id TEXT PRIMARY KEY, adapter_id TEXT)'); + await db.execute('CREATE TABLE decoded_onehz (' + "device_id TEXT NOT NULL DEFAULT '', ts_ms INTEGER NOT NULL DEFAULT 0, " + 'hr INTEGER, PRIMARY KEY (device_id, ts_ms))'); + await db.execute('CREATE TABLE raw_archive (' + "device_id TEXT NOT NULL DEFAULT '', hex TEXT NOT NULL, " + 'PRIMARY KEY (device_id, hex))'); + await db.execute('CREATE TABLE sync_cursor (' + 'name TEXT PRIMARY KEY, value TEXT, updated_at INTEGER)'); + return db; +} + +void main() { + setUpAll(() { + sqfliteFfiInit(); + databaseFactory = databaseFactoryFfi; + }); + + test('a stranded id is reunited with the surviving row of its family', + () async { + final db = await _open(); + await db.insert('device', {'id': 'oura-a4487268', 'adapter_id': 'oura'}); + await db.insert('decoded_onehz', + {'device_id': 'oura-7e117c66', 'ts_ms': 1000, 'hr': 55}); + await db.insert('raw_archive', {'device_id': 'oura-7e117c66', 'hex': 'ab'}); + await db.insert('sync_cursor', + {'name': 'oura_cursor_ds:oura-7e117c66', 'value': '42'}); + + expect(await LocalDb.reuniteStrandedDeviceRows(db), 1); + + expect( + (await db.query('decoded_onehz')).single['device_id'], + 'oura-a4487268', + ); + expect( + (await db.query('raw_archive')).single['device_id'], + 'oura-a4487268', + ); + // The bookmark is DROPPED, never carried: a stranded id cannot be shown to + // be the same physical ring, and a foreign origin mis-stamps every second. + expect(await db.query('sync_cursor'), isEmpty); + await db.close(); + }); + + test('re-running is a no-op — nothing is left to match', () async { + final db = await _open(); + await db.insert('device', {'id': 'oura-a4487268', 'adapter_id': 'oura'}); + await db.insert('decoded_onehz', + {'device_id': 'oura-7e117c66', 'ts_ms': 1000, 'hr': 55}); + + expect(await LocalDb.reuniteStrandedDeviceRows(db), 1); + expect(await LocalDb.reuniteStrandedDeviceRows(db), 0); + await db.close(); + }); + + test('the primary band is never re-keyed', () async { + final db = await _open(); + await db.insert('device', {'id': 'oura-a4487268', 'adapter_id': 'oura'}); + // kPrimaryDeviceId: no family separator, so it can never be a candidate. + await db.insert( + 'decoded_onehz', {'device_id': '', 'ts_ms': 1000, 'hr': 55}); + + expect(await LocalDb.reuniteStrandedDeviceRows(db), 0); + expect((await db.query('decoded_onehz')).single['device_id'], ''); + await db.close(); + }); + + test('a stranded id is never moved across families', () async { + final db = await _open(); + await db.insert('device', {'id': 'oura-a4487268', 'adapter_id': 'oura'}); + await db.insert('decoded_onehz', + {'device_id': 'o2ring-deadbeef', 'ts_ms': 1000, 'hr': 55}); + + expect(await LocalDb.reuniteStrandedDeviceRows(db), 0); + expect( + (await db.query('decoded_onehz')).single['device_id'], + 'o2ring-deadbeef', + ); + await db.close(); + }); + + // Two rows in one family means there is no single answer to "which device is + // this", and picking one would merge two devices' measurements into one + // history — a fabricated number by a slower route. + test('an ambiguous family is left completely alone', () async { + final db = await _open(); + await db.insert('device', {'id': 'oura-aaaaaaaa', 'adapter_id': 'oura'}); + await db.insert('device', {'id': 'oura-bbbbbbbb', 'adapter_id': 'oura'}); + await db.insert('decoded_onehz', + {'device_id': 'oura-7e117c66', 'ts_ms': 1000, 'hr': 55}); + + expect(await LocalDb.reuniteStrandedDeviceRows(db), 0); + expect( + (await db.query('decoded_onehz')).single['device_id'], + 'oura-7e117c66', + ); + await db.close(); + }); + + // The collision the UPDATE OR IGNORE + delete exists for: a constraint + // failure here would roll back onUpgrade's one exclusive transaction and + // quarantine the database (invariant 11). The surviving id's row wins, so a + // value already on screen never changes. + test('a colliding key keeps the surviving row instead of aborting', + () async { + final db = await _open(); + await db.insert('device', {'id': 'oura-a4487268', 'adapter_id': 'oura'}); + await db.insert('decoded_onehz', + {'device_id': 'oura-a4487268', 'ts_ms': 1000, 'hr': 55}); + await db.insert('decoded_onehz', + {'device_id': 'oura-7e117c66', 'ts_ms': 1000, 'hr': 61}); + + expect(await LocalDb.reuniteStrandedDeviceRows(db), 1); + final rows = await db.query('decoded_onehz'); + expect(rows.length, 1); + expect(rows.single['device_id'], 'oura-a4487268'); + expect(rows.single['hr'], 55); + await db.close(); + }); + + test('only the stranded id\'s own bookmarks are dropped', () async { + final db = await _open(); + await db.insert('device', {'id': 'oura-a4487268', 'adapter_id': 'oura'}); + await db.insert('decoded_onehz', + {'device_id': 'oura-7e117c66', 'ts_ms': 1000, 'hr': 55}); + for (final name in [ + 'oura_cursor_ds:oura-7e117c66', + 'oura_cursor_ds:oura-a4487268', + 'oura_cursor_ds:xoura-7e117c66', + 'oura_cursor_ds:OURA-7E117C66', + ]) { + await db.insert('sync_cursor', {'name': name, 'value': '1'}); + } + + expect(await LocalDb.reuniteStrandedDeviceRows(db), 1); + expect( + (await db.query('sync_cursor', orderBy: 'name')) + .map((r) => r['name']) + .toList(), + [ + 'oura_cursor_ds:OURA-7E117C66', + 'oura_cursor_ds:oura-a4487268', + 'oura_cursor_ds:xoura-7e117c66', + ], + ); + await db.close(); + }); +}