diff --git a/lib/storage/drift/offband_database.dart b/lib/storage/drift/offband_database.dart index 1058f297..d9fd98c5 100644 --- a/lib/storage/drift/offband_database.dart +++ b/lib/storage/drift/offband_database.dart @@ -377,8 +377,23 @@ class OffbandDatabase extends _$OffbandDatabase { MigrationStrategy get migration => MigrationStrategy( onUpgrade: (m, from, to) async { if (from > to) { - // Best-effort: path_provider has no web implementation, and a message - // without the path still beats the silent rewrite it replaces. + // Logged BEFORE the path lookup and before the throw, deliberately. + // This runs inside drift's beforeOpen, so it fires on whichever + // isolate issued the first query, and whoever catches the exception + // decides what the user sees. A downgrade that only stopped + // background work, with nothing in the log, would be the quiet + // failure this branch exists to replace (Gemini review). + appLogger.error( + 'Refusing to open the database: it is at schema $from and this ' + 'build understands $to. The upgrade to a newer schema is one-way ' + '(#943).', + tag: 'Storage', + ); + // Best-effort: path_provider has no web implementation, and on the + // platforms where a downgrade is reachable this reads environment + // variables rather than a platform channel, and has already resolved + // once to open this database. A message without the path still beats + // the silent version rewrite it replaces. String? path; try { path = await pinnedDatabasePath(); diff --git a/test/benchmarks/stuck_key_recovery_bench.dart b/test/benchmarks/stuck_key_recovery_bench.dart index a8b58c0d..f9192d48 100644 --- a/test/benchmarks/stuck_key_recovery_bench.dart +++ b/test/benchmarks/stuck_key_recovery_bench.dart @@ -26,6 +26,19 @@ import 'package:shared_preferences/shared_preferences.dart'; const String _benchDb = String.fromEnvironment('BENCH_DB'); +/// Non-empty `messageId`s in a history blob, read straight from the JSON. +/// +/// Deliberately not via the converter: this is used to check the converter's +/// output, so sharing its code would make the check circular. +List _messageIdsIn(String? blob) { + if (blob == null || blob.isEmpty) return const []; + return (jsonDecode(blob) as List) + .map((e) => (e as Map)['messageId'] as String?) + .whereType() + .where((id) => id.isNotEmpty) + .toList(); +} + void main() { setUp(() async { SharedPreferences.setMockInitialValues({}); @@ -115,14 +128,40 @@ void main() { (ok ? recovered : refused).add(key); if (ok) { - // Every blob record must be readable as a row, and the extra rows - // that caused the old refusal must still be there. expect( rowsAfter, greaterThanOrEqualTo(records), reason: '$key lost records while migrating', ); expect(await blobs.isOnMessageRows(key), isTrue); + + // Counts alone would pass against a converter that produced the + // right NUMBER of rows with the wrong contents (Gemini review). + // Identity and payloads are checked too, derived from the raw blob + // rather than from the converter under test, so this is not + // circular. + // + // Only records carrying a messageId are identity-checked: the `x:` + // fallback merge key would mean reimplementing the converter here, + // which would make the check circular again. The count assertion + // above still covers those. + final ids = _messageIdsIn(blob); + final stored = await blobs.messageMergeKeys(key); + final absent = ids.where((id) => !stored.contains('id:$id')).toList(); + expect( + absent, + isEmpty, + reason: + '$key: ${absent.length} of ${ids.length} identified messages ' + 'in the blob have no row carrying their identity', + ); + + final payloads = await blobs.messagePayloads(key); + expect( + payloads.where((p) => p.trim().length <= 2), + isEmpty, + reason: '$key stored empty payloads', + ); } }