From cd140976689674e78d838eb605488e20b3f18adb Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Fri, 22 Oct 2021 09:03:35 -0400 Subject: [PATCH] Fix errors in HotbarStore migrations (#4105) --- src/migrations/hotbar-store/5.0.0-alpha.2.ts | 3 ++- src/migrations/hotbar-store/5.0.0-beta.10.ts | 16 ++++++++++++++-- src/migrations/hotbar-store/5.0.0-beta.5.ts | 16 +++++++++------- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/migrations/hotbar-store/5.0.0-alpha.2.ts b/src/migrations/hotbar-store/5.0.0-alpha.2.ts index 0a662b466a..6eeb759779 100644 --- a/src/migrations/hotbar-store/5.0.0-alpha.2.ts +++ b/src/migrations/hotbar-store/5.0.0-alpha.2.ts @@ -27,7 +27,8 @@ import type { MigrationDeclaration } from "../helpers"; export default { version: "5.0.0-alpha.2", run(store) { - const hotbars = (store.get("hotbars") || []) as Hotbar[]; + const rawHotbars = store.get("hotbars"); + const hotbars: Hotbar[] = Array.isArray(rawHotbars) ? rawHotbars : []; store.set("hotbars", hotbars.map((hotbar) => ({ id: uuid.v4(), diff --git a/src/migrations/hotbar-store/5.0.0-beta.10.ts b/src/migrations/hotbar-store/5.0.0-beta.10.ts index 7d7abc01b7..a9a0de11b2 100644 --- a/src/migrations/hotbar-store/5.0.0-beta.10.ts +++ b/src/migrations/hotbar-store/5.0.0-beta.10.ts @@ -46,9 +46,20 @@ interface PartialHotbar { export default { version: "5.0.0-beta.10", run(store) { - const hotbars = (store.get("hotbars") as Hotbar[] ?? []).filter(Boolean); + const rawHotbars = store.get("hotbars"); + const hotbars: Hotbar[] = Array.isArray(rawHotbars) ? rawHotbars.filter(h => h && typeof h === "object") : []; const userDataPath = app.getPath("userData"); + // Hotbars might be empty, if some of the previous migrations weren't run + if (hotbars.length === 0) { + const hotbar = getEmptyHotbar("default"); + const { metadata: { uid, name, source } } = catalogEntity; + + hotbar.items[0] = { entity: { uid, name, source } }; + + hotbars.push(hotbar); + } + try { const workspaceStoreData: Pre500WorkspaceStoreModel = fse.readJsonSync(path.join(userDataPath, "lens-workspace-store.json")); const { clusters }: ClusterStoreModel = fse.readJSONSync(path.join(userDataPath, "lens-cluster-store.json")); @@ -144,12 +155,13 @@ export default { } } - store.set("hotbars", hotbars); } catch (error) { // ignore files being missing if (error.code !== "ENOENT") { throw error; } } + + store.set("hotbars", hotbars); } } as MigrationDeclaration; diff --git a/src/migrations/hotbar-store/5.0.0-beta.5.ts b/src/migrations/hotbar-store/5.0.0-beta.5.ts index f9cfd77262..40f36e13fe 100644 --- a/src/migrations/hotbar-store/5.0.0-beta.5.ts +++ b/src/migrations/hotbar-store/5.0.0-beta.5.ts @@ -26,25 +26,27 @@ import type { MigrationDeclaration } from "../helpers"; export default { version: "5.0.0-beta.5", run(store) { - const hotbars: Hotbar[] = store.get("hotbars"); + const rawHotbars = store.get("hotbars"); + const hotbars: Hotbar[] = Array.isArray(rawHotbars) ? rawHotbars : []; - hotbars.forEach((hotbar, hotbarIndex) => { - hotbar.items.forEach((item, itemIndex) => { + for (const hotbar of hotbars) { + for (let i = 0; i < hotbar.items.length; i += 1) { + const item = hotbar.items[i]; const entity = catalogEntityRegistry.items.find((entity) => entity.metadata.uid === item?.entity.uid); if (!entity) { // Clear disabled item - hotbars[hotbarIndex].items[itemIndex] = null; + hotbar.items[i] = null; } else { // Save additional data - hotbars[hotbarIndex].items[itemIndex].entity = { + hotbar.items[i].entity = { ...item.entity, name: entity.metadata.name, source: entity.metadata.source }; } - }); - }); + } + } store.set("hotbars", hotbars); }