Add committing of the db operations into the migration lifecycle
This commit is contained in:
parent
85e3838396
commit
b0a3d972f0
4 changed files with 84 additions and 30 deletions
|
|
@ -3,9 +3,24 @@ import { migrateCollection, shouldMigrateCompendium } from "./utils.mjs";
|
|||
|
||||
const flag = `convertAttributesIntoItems`;
|
||||
const operations = [];
|
||||
let compendiumOperations = [];
|
||||
|
||||
export async function migrateTo3_0_0() {
|
||||
Logger.debug(`Starting v3.0.0 data migration`);
|
||||
const packsToMigrate = game.packs.filter(
|
||||
(pack) => shouldMigrateCompendium(pack, [`Actor`]),
|
||||
);
|
||||
const intervalSize = 1 / (packsToMigrate.length + 1);
|
||||
|
||||
const warning = ui.notifications.warn(
|
||||
"taf.notifs.warn.migration-in-progress",
|
||||
{
|
||||
format: { version: `v3.0.0` },
|
||||
progress: true,
|
||||
permanent: true,
|
||||
console: false,
|
||||
},
|
||||
);
|
||||
|
||||
operations.push(
|
||||
...await migrateCollection(
|
||||
|
|
@ -15,30 +30,41 @@ export async function migrateTo3_0_0() {
|
|||
{ update: false, },
|
||||
),
|
||||
);
|
||||
warning.update({ pct: warning.pct + intervalSize });
|
||||
|
||||
// for (const pack of game.packs) {
|
||||
// if (
|
||||
// pack.metadata.type !== "Actor"
|
||||
// || !shouldMigrateCompendium(pack)
|
||||
// ) {
|
||||
// continue;
|
||||
// };
|
||||
for (const pack of packsToMigrate) {
|
||||
await pack.getDocuments();
|
||||
|
||||
// await pack.getDocuments();
|
||||
const wasLocked = pack.config.locked;
|
||||
if (wasLocked) pack.configure({ locked: false });
|
||||
|
||||
// // TODO: unlock compendium if required then re-lock after finishing
|
||||
// await migrateCollection(
|
||||
// pack,
|
||||
// flag,
|
||||
// handleMigratingActor,
|
||||
// { pack },
|
||||
// );
|
||||
// };
|
||||
compendiumOperations.push(
|
||||
...await migrateCollection(
|
||||
pack,
|
||||
flag,
|
||||
handleMigratingActor,
|
||||
{ pack, update: false, },
|
||||
),
|
||||
);
|
||||
|
||||
// foundry.documents.modifyBatch(compendiumOperations);
|
||||
console.log(`compendiumOperations`, compendiumOperations);
|
||||
|
||||
if (wasLocked) await pack.configure({ locked: true });
|
||||
|
||||
compendiumOperations = [];
|
||||
warning.update({ pct: warning.pct + intervalSize });
|
||||
};
|
||||
|
||||
// TODO: re-lock packs here?
|
||||
|
||||
warning.update({ pct: 1 });
|
||||
|
||||
// TODO: create the item documents (batch them if possible)
|
||||
Logger.debug(`Finished v3.0.0 migration, resulting operations:`);
|
||||
console.log(operations);
|
||||
// Use: foundry.documents.modifyBatch
|
||||
// await foundry.documents.modifyBatch(operations);
|
||||
};
|
||||
|
||||
function handleMigratingActor(actor) {
|
||||
|
|
@ -48,16 +74,27 @@ function handleMigratingActor(actor) {
|
|||
action: `create`,
|
||||
documentName: `Item`,
|
||||
parent: actor,
|
||||
noHook: true,
|
||||
data: [],
|
||||
};
|
||||
|
||||
const attrs = actor.system.attr;
|
||||
const attrs = actor.system?.attr ?? {};
|
||||
for (const [ key, attr ] of Object.entries(attrs)) {
|
||||
operation.data.push(convertToItem(key, attr));
|
||||
};
|
||||
operations.push(operation);
|
||||
|
||||
return null;
|
||||
// No items to create, don't queue the operation
|
||||
if (operation.data.length > 0) {
|
||||
if (actor.inCompendium) {
|
||||
compendiumOperations.push(operation);
|
||||
} else {
|
||||
operations.push(operation);
|
||||
};
|
||||
};
|
||||
|
||||
return {
|
||||
"system.attr": _del,
|
||||
};
|
||||
};
|
||||
|
||||
function convertToItem(key, attr) {
|
||||
|
|
@ -68,6 +105,7 @@ function convertToItem(key, attr) {
|
|||
key,
|
||||
value: attr.value,
|
||||
max: attr.isRange ? attr.max : null,
|
||||
aboveTheFold: true,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue