From 1025ed68a8fd8a242ceb17fe9637aebfc15d60af Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Sun, 9 Jul 2023 22:35:37 +0200 Subject: [PATCH] refactor: avoid unnecessary migrations --- src/migration/migrationHelpers.js | 61 ++++++++++++++++--------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/src/migration/migrationHelpers.js b/src/migration/migrationHelpers.js index 0b169e77..314a75f8 100644 --- a/src/migration/migrationHelpers.js +++ b/src/migration/migrationHelpers.js @@ -2,8 +2,6 @@ // // SPDX-License-Identifier: MIT -import { DS4Actor } from "../documents/actor/actor.js"; -import { DS4Item } from "../documents/item/item.js"; import { logger } from "../utils/logger.js"; import { getGame } from "../utils/utils.js"; @@ -138,39 +136,42 @@ export function getCompendiumMigrator( return async (pack) => { /** @type {import("./migration.js").Result} */ let result = "success"; + const type = pack.metadata.type; - if (!["Actor", "Item", "Scene"].includes(type)) return; - const wasLocked = pack.locked; - await pack.configure({ locked: false }); - if (migrateToTemplateEarly) { - await pack.migrate(); - } + const migrateDocument = { + Item: migrateItem, + Actor: migrateActor, + Scene: migrateScene, + }[type]; - const documents = await pack.getDocuments(); - - for (const doc of documents) { - try { - logger.info(`Migrating document ${doc.name} (${doc.id}) in compendium ${pack.collection}`); - if (doc instanceof DS4Item && migrateItem) { - await migrateItem(doc); - } else if (doc instanceof DS4Actor && migrateActor) { - await migrateActor(doc); - } else if (doc instanceof Scene && migrateScene) { - await migrateScene(doc); - } - } catch (err) { - logger.error( - `Error during migration of document ${doc.name} (${doc.id}) in compendium ${pack.collection}, continuing anyways.`, - err, - ); - result = "error"; + if (migrateDocument) { + const wasLocked = pack.locked; + await pack.configure({ locked: false }); + if (migrateToTemplateEarly) { + await pack.migrate(); } + + const documents = await pack.getDocuments(); + + for (const doc of documents) { + try { + logger.info(`Migrating document ${doc.name} (${doc.id}) in compendium ${pack.collection}`); + await migrateDocument(doc); + } catch (err) { + logger.error( + `Error during migration of document ${doc.name} (${doc.id}) in compendium ${pack.collection}, continuing anyways.`, + err, + ); + result = "error"; + } + } + + if (!migrateToTemplateEarly) { + await pack.migrate(); + } + await pack.configure({ locked: wasLocked }); } - if (!migrateToTemplateEarly) { - await pack.migrate(); - } - await pack.configure({ locked: wasLocked }); return result; }; }