1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Fix errors in HotbarStore migrations (#4105)

This commit is contained in:
Sebastian Malton 2021-10-22 09:03:35 -04:00 committed by GitHub
parent 1872a8da87
commit cd14097668
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 10 deletions

View File

@ -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(),

View File

@ -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;

View File

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