@@ -25,8 +25,12 @@ class CacheObjectProvider extends CacheInfoRepository
25
25
if (! shouldOpenOnNewConnection ()) {
26
26
return openCompleter! .future;
27
27
}
28
+
28
29
final path = await _getPath ();
30
+
29
31
await File (path).parent.create (recursive: true );
32
+ await _dropCorruptedDb (path);
33
+
30
34
db = await openDatabase (path, version: 3 ,
31
35
onCreate: (Database db, int version) async {
32
36
await db.execute ('''
@@ -214,4 +218,41 @@ class CacheObjectProvider extends CacheInfoRepository
214
218
}
215
219
}
216
220
}
221
+
222
+ static Future <void > _dropCorruptedDb (String path) async {
223
+ bool exists = false ;
224
+
225
+ try {
226
+ exists = await File (path).exists ();
227
+ } on FileSystemException {
228
+ // We can not read the file, so we assume it does not exist.
229
+ } catch (e) {
230
+ // We can not read the file, so we assume it does not exist.
231
+ }
232
+
233
+ if (! exists) {
234
+ return ;
235
+ }
236
+
237
+ Database ? pragmaDb;
238
+ bool isIntegral = true ;
239
+
240
+ try {
241
+ pragmaDb = await openDatabase (path);
242
+ final check = await pragmaDb.rawQuery ("pragma integrity_check" );
243
+ isIntegral = check.length == 1 && check[0 ].values.first == "ok" ;
244
+ } on DatabaseException {
245
+ isIntegral = false ;
246
+ } catch (e) {
247
+ isIntegral = false ;
248
+ } finally {
249
+ if (pragmaDb != null ) {
250
+ await pragmaDb.close ();
251
+ }
252
+ }
253
+
254
+ if (! isIntegral) {
255
+ await deleteDatabase (path);
256
+ }
257
+ }
217
258
}
0 commit comments