refactor: avoid unnecessary migrations
All checks were successful
ci/woodpecker/pr/checks Pipeline was successful
ci/woodpecker/push/checks Pipeline was successful
ci/woodpecker/manual/checks Pipeline was successful
ci/woodpecker/manual/release Pipeline was successful

This commit is contained in:
Johannes Loher 2023-07-09 22:35:37 +02:00
parent 4ed292f6c0
commit 1025ed68a8
Signed by: saluu
GPG key ID: 7CB0A9FB553DA045

View file

@ -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,8 +136,15 @@ 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 migrateDocument = {
Item: migrateItem,
Actor: migrateActor,
Scene: migrateScene,
}[type];
if (migrateDocument) {
const wasLocked = pack.locked;
await pack.configure({ locked: false });
if (migrateToTemplateEarly) {
@ -151,13 +156,7 @@ export function getCompendiumMigrator(
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);
}
await migrateDocument(doc);
} catch (err) {
logger.error(
`Error during migration of document ${doc.name} (${doc.id}) in compendium ${pack.collection}, continuing anyways.`,
@ -171,6 +170,8 @@ export function getCompendiumMigrator(
await pack.migrate();
}
await pack.configure({ locked: wasLocked });
}
return result;
};
}