Drop db if failed migrating

This commit is contained in:
Ming Ming 2022-01-15 18:40:34 +08:00
parent ea07b5d2e8
commit 781d846806
3 changed files with 48 additions and 28 deletions

View file

@ -45,6 +45,14 @@ class AppDb {
});
}
Future<void> delete() async {
_log.warning("[delete] Deleting database");
return await _lock.synchronized(() async {
final dbFactory = platform.getDbFactory();
await dbFactory.deleteDatabase(dbName);
});
}
/// Open the database
Future<Database> _open() {
if (platform_k.isWeb) {

View file

@ -27,36 +27,43 @@ class DbCompatV5 {
static Future<void> migrate(AppDb appDb) async {
_log.info("[migrate] Migrate AppDb");
await appDb.use((db) async {
final transaction = db.transaction(
[AppDb.file2StoreName, AppDb.metaStoreName], idbModeReadWrite);
try {
final fileStore = transaction.objectStore(AppDb.file2StoreName);
await for (final c in fileStore.openCursor()) {
final item = c.value as Map;
// migrate file entry: add bestDateTime
final fileEntry = item.cast<String, dynamic>().run((json) {
final f = File.fromJson(json["file"].cast<String, dynamic>());
return AppDbFile2Entry(
json["server"],
(json["userId"] as String).toCi(),
json["strippedPath"],
f.bestDateTime.millisecondsSinceEpoch,
File.fromJson(json["file"].cast<String, dynamic>()),
);
});
await c.update(fileEntry.toJson());
try {
await appDb.use((db) async {
final transaction = db.transaction(
[AppDb.file2StoreName, AppDb.metaStoreName], idbModeReadWrite);
try {
final fileStore = transaction.objectStore(AppDb.file2StoreName);
await for (final c in fileStore.openCursor()) {
final item = c.value as Map;
// migrate file entry: add bestDateTime
final fileEntry = item.cast<String, dynamic>().run((json) {
final f = File.fromJson(json["file"].cast<String, dynamic>());
return AppDbFile2Entry(
json["server"],
(json["userId"] as String).toCi(),
json["strippedPath"],
f.bestDateTime.millisecondsSinceEpoch,
File.fromJson(json["file"].cast<String, dynamic>()),
);
});
await c.update(fileEntry.toJson());
c.next();
c.next();
}
final metaStore = transaction.objectStore(AppDb.metaStoreName);
await metaStore
.put(const AppDbMetaEntryDbCompatV5(true).toEntry().toJson());
} catch (_) {
transaction.abort();
rethrow;
}
final metaStore = transaction.objectStore(AppDb.metaStoreName);
await metaStore
.put(const AppDbMetaEntryDbCompatV5(true).toEntry().toJson());
} catch (_) {
transaction.abort();
rethrow;
}
});
});
} catch (e, stackTrace) {
_log.shout(
"[migrate] Failed while migrating, drop db instead", e, stackTrace);
await appDb.delete();
rethrow;
}
}
static final _log = Logger("use_case.db_compat.v5.DbCompatV5");

View file

@ -117,6 +117,11 @@ class MockAppDb implements AppDb {
}
}
@override
Future<void> delete() async {
throw UnimplementedError();
}
static void _createDb(
Database db, {
bool hasAlbumStore = true,