mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-01-22 16:56:19 +01:00
Drop db if failed migrating
This commit is contained in:
parent
ea07b5d2e8
commit
781d846806
3 changed files with 48 additions and 28 deletions
|
@ -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
|
/// Open the database
|
||||||
Future<Database> _open() {
|
Future<Database> _open() {
|
||||||
if (platform_k.isWeb) {
|
if (platform_k.isWeb) {
|
||||||
|
|
|
@ -27,36 +27,43 @@ class DbCompatV5 {
|
||||||
|
|
||||||
static Future<void> migrate(AppDb appDb) async {
|
static Future<void> migrate(AppDb appDb) async {
|
||||||
_log.info("[migrate] Migrate AppDb");
|
_log.info("[migrate] Migrate AppDb");
|
||||||
await appDb.use((db) async {
|
try {
|
||||||
final transaction = db.transaction(
|
await appDb.use((db) async {
|
||||||
[AppDb.file2StoreName, AppDb.metaStoreName], idbModeReadWrite);
|
final transaction = db.transaction(
|
||||||
try {
|
[AppDb.file2StoreName, AppDb.metaStoreName], idbModeReadWrite);
|
||||||
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
try {
|
||||||
await for (final c in fileStore.openCursor()) {
|
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
||||||
final item = c.value as Map;
|
await for (final c in fileStore.openCursor()) {
|
||||||
// migrate file entry: add bestDateTime
|
final item = c.value as Map;
|
||||||
final fileEntry = item.cast<String, dynamic>().run((json) {
|
// migrate file entry: add bestDateTime
|
||||||
final f = File.fromJson(json["file"].cast<String, dynamic>());
|
final fileEntry = item.cast<String, dynamic>().run((json) {
|
||||||
return AppDbFile2Entry(
|
final f = File.fromJson(json["file"].cast<String, dynamic>());
|
||||||
json["server"],
|
return AppDbFile2Entry(
|
||||||
(json["userId"] as String).toCi(),
|
json["server"],
|
||||||
json["strippedPath"],
|
(json["userId"] as String).toCi(),
|
||||||
f.bestDateTime.millisecondsSinceEpoch,
|
json["strippedPath"],
|
||||||
File.fromJson(json["file"].cast<String, dynamic>()),
|
f.bestDateTime.millisecondsSinceEpoch,
|
||||||
);
|
File.fromJson(json["file"].cast<String, dynamic>()),
|
||||||
});
|
);
|
||||||
await c.update(fileEntry.toJson());
|
});
|
||||||
|
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
|
} catch (e, stackTrace) {
|
||||||
.put(const AppDbMetaEntryDbCompatV5(true).toEntry().toJson());
|
_log.shout(
|
||||||
} catch (_) {
|
"[migrate] Failed while migrating, drop db instead", e, stackTrace);
|
||||||
transaction.abort();
|
await appDb.delete();
|
||||||
rethrow;
|
rethrow;
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static final _log = Logger("use_case.db_compat.v5.DbCompatV5");
|
static final _log = Logger("use_case.db_compat.v5.DbCompatV5");
|
||||||
|
|
|
@ -117,6 +117,11 @@ class MockAppDb implements AppDb {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> delete() async {
|
||||||
|
throw UnimplementedError();
|
||||||
|
}
|
||||||
|
|
||||||
static void _createDb(
|
static void _createDb(
|
||||||
Database db, {
|
Database db, {
|
||||||
bool hasAlbumStore = true,
|
bool hasAlbumStore = true,
|
||||||
|
|
Loading…
Reference in a new issue