diff --git a/web/apps/photos/src/utils/storage/mlIDbStorage.ts b/web/apps/photos/src/utils/storage/mlIDbStorage.ts index eec86e3a2..6dccbb89d 100644 --- a/web/apps/photos/src/utils/storage/mlIDbStorage.ts +++ b/web/apps/photos/src/utils/storage/mlIDbStorage.ts @@ -85,30 +85,25 @@ class MLIDbStorage { async upgrade(db, oldVersion, newVersion, tx) { let wasMLSearchEnabled = false; try { - const s: unknown = await tx + const searchConfig: unknown = await tx .objectStore("configs") - .getKey(ML_SEARCH_CONFIG_NAME); - if (typeof s == "string") { - const json = JSON.parse(s); - if ( - json && - typeof json == "object" && - "enabled" in json - ) { - const enabled = json.enabled; - if (typeof enabled == "boolean") { - wasMLSearchEnabled = enabled; - } - } + .get(ML_SEARCH_CONFIG_NAME); + if ( + searchConfig && + typeof searchConfig == "object" && + "enabled" in searchConfig && + typeof searchConfig.enabled == "boolean" + ) { + wasMLSearchEnabled = searchConfig.enabled; } } catch (e) { - log.error( - "Ignoring error while trying to determine ML search status during migration", + log.info( + "Ignoring likely harmless error while trying to determine ML search status during migration", e, ); } log.info( - `Old database had wasMLSearchEnabled = ${wasMLSearchEnabled}`, + `Previous ML database v${oldVersion} had ML search ${wasMLSearchEnabled ? "enabled" : "disabled"}`, ); if (oldVersion < 1) { @@ -152,15 +147,28 @@ class MLIDbStorage { .add(DEFAULT_ML_SEARCH_CONFIG, ML_SEARCH_CONFIG_NAME); } if (oldVersion < 4) { - // TODO(MR): This loses the user's settings. - db.deleteObjectStore("configs"); - db.createObjectStore("configs"); + try { + await tx + .objectStore("configs") + .delete(ML_SEARCH_CONFIG_NAME); - db.deleteObjectStore("things"); + await tx + .objectStore("configs") + .add( + { enabled: wasMLSearchEnabled }, + ML_SEARCH_CONFIG_NAME, + ); + + db.deleteObjectStore("things"); + } catch { + // TODO: ignore for now as we finalize the new version + // the shipped implementation should have a more + // deterministic migration. + } } log.info( - `Ml DB upgraded to version: ${newVersion} from version: ${oldVersion}`, + `ML DB upgraded from version ${oldVersion} to version ${newVersion}`, ); }, });