Granular migration

This commit is contained in:
Manav Rathi 2024-04-13 12:58:21 +05:30
parent 0f3b28a425
commit f2d3ec74a1
No known key found for this signature in database

View file

@ -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}`,
);
},
});