Skip to content

Commit

Permalink
drop corrupted db (PR by @turekj: Baseflow#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
contactjavas committed Sep 18, 2023
1 parent 4199a1c commit b6ec07d
Showing 1 changed file with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ class CacheObjectProvider extends CacheInfoRepository
if (!shouldOpenOnNewConnection()) {
return openCompleter!.future;
}

final path = await _getPath();

await File(path).parent.create(recursive: true);
await _dropCorruptedDb(path);

db = await openDatabase(path, version: 3,
onCreate: (Database db, int version) async {
await db.execute('''
Expand Down Expand Up @@ -214,4 +218,41 @@ class CacheObjectProvider extends CacheInfoRepository
}
}
}

static Future<void> _dropCorruptedDb(String path) async {
bool exists = false;

try {
exists = await File(path).exists();
} on FileSystemException {
// We can not read the file, so we assume it does not exist.
} catch (e) {
// We can not read the file, so we assume it does not exist.
}

if (!exists) {
return;
}

Database? pragmaDb;
bool isIntegral = true;

try {
pragmaDb = await openDatabase(path);
final check = await pragmaDb.rawQuery("pragma integrity_check");
isIntegral = check.length == 1 && check[0].values.first == "ok";
} on DatabaseException {
isIntegral = false;
} catch (e) {
isIntegral = false;
} finally {
if (pragmaDb != null) {
await pragmaDb.close();
}
}

if (!isIntegral) {
await deleteDatabase(path);
}
}
}

0 comments on commit b6ec07d

Please sign in to comment.