refactor: avoid unnecessary migrations
This commit is contained in:
parent
4ed292f6c0
commit
1025ed68a8
1 changed files with 31 additions and 30 deletions
|
@ -2,8 +2,6 @@
|
||||||
//
|
//
|
||||||
// SPDX-License-Identifier: MIT
|
// 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 { logger } from "../utils/logger.js";
|
||||||
import { getGame } from "../utils/utils.js";
|
import { getGame } from "../utils/utils.js";
|
||||||
|
|
||||||
|
@ -138,39 +136,42 @@ export function getCompendiumMigrator(
|
||||||
return async (pack) => {
|
return async (pack) => {
|
||||||
/** @type {import("./migration.js").Result} */
|
/** @type {import("./migration.js").Result} */
|
||||||
let result = "success";
|
let result = "success";
|
||||||
|
|
||||||
const type = pack.metadata.type;
|
const type = pack.metadata.type;
|
||||||
if (!["Actor", "Item", "Scene"].includes(type)) return;
|
const migrateDocument = {
|
||||||
const wasLocked = pack.locked;
|
Item: migrateItem,
|
||||||
await pack.configure({ locked: false });
|
Actor: migrateActor,
|
||||||
if (migrateToTemplateEarly) {
|
Scene: migrateScene,
|
||||||
await pack.migrate();
|
}[type];
|
||||||
}
|
|
||||||
|
|
||||||
const documents = await pack.getDocuments();
|
if (migrateDocument) {
|
||||||
|
const wasLocked = pack.locked;
|
||||||
for (const doc of documents) {
|
await pack.configure({ locked: false });
|
||||||
try {
|
if (migrateToTemplateEarly) {
|
||||||
logger.info(`Migrating document ${doc.name} (${doc.id}) in compendium ${pack.collection}`);
|
await pack.migrate();
|
||||||
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";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
return result;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue