#1471 DB sanitizing to mitigate v1.12.4 upgrade issue

This commit is contained in:
Thibault Deckers 2025-03-10 23:08:23 +01:00
parent 3f6ef0cdaf
commit a32c0cf0f0
2 changed files with 22 additions and 1 deletions

View file

@ -117,7 +117,7 @@ class SqfliteLocalMediaDb implements LocalMediaDb {
')');
},
onUpgrade: LocalMediaDbUpgrader.upgradeDb,
version: 14,
version: 15,
);
final maxIdRows = await _db.rawQuery('SELECT MAX(id) AS maxId FROM $entryTable');

View file

@ -50,6 +50,8 @@ class LocalMediaDbUpgrader {
await _upgradeFrom12(db);
case 13:
await _upgradeFrom13(db);
case 14:
await _upgradeFrom14(db);
}
oldVersion++;
}
@ -479,4 +481,23 @@ class LocalMediaDbUpgrader {
await db.execute('ALTER TABLE $newEntryTable RENAME TO $entryTable;');
});
}
static Future<void> _upgradeFrom14(Database db) async {
debugPrint('upgrading DB from v14');
// no schema changes, but v1.12.4 may have corrupted the DB, so we sanitize it
// clear rebuildable tables
await db.delete(dateTakenTable, where: '1');
await db.delete(metadataTable, where: '1');
await db.delete(addressTable, where: '1');
await db.delete(trashTable, where: '1');
await db.delete(videoPlaybackTable, where: '1');
// remove rows referencing future entry IDs
final maxIdRows = await db.rawQuery('SELECT MAX(id) AS maxId FROM $entryTable');
final lastId = (maxIdRows.firstOrNull?['maxId'] as int?) ?? 0;
await db.delete(favouriteTable, where: 'id > ?', whereArgs: [lastId]);
await db.delete(coverTable, where: 'entryId > ?', whereArgs: [lastId]);
}
}