2
0
mirror of https://github.com/frappe/books.git synced 2024-12-26 12:28:12 +00:00
books/model/migrate.js

41 lines
944 B
JavaScript
Raw Normal View History

const frappe = require('frappejs');
module.exports = async function migrate(allPatches, patchOrder) {
let executedPatchRuns = [];
try {
executedPatchRuns = (
await frappe.db.getAll({ doctype: 'PatchRun', fields: ['name'] })
).map(d => d.name);
} catch (error) {}
let patchRunOrder = patchOrder
.map(text => {
let [patch] = text.split(' ');
if (text && patch) {
return {
fileName: text,
method: allPatches[patch]
};
}
})
.filter(Boolean);
for (let patch of patchRunOrder) {
if (!executedPatchRuns.includes(patch.fileName)) {
await runPatch(patch);
}
}
};
async function runPatch(patch) {
try {
await patch.method();
let patchRun = frappe.getNewDoc('PatchRun');
patchRun.name = patch.fileName;
await patchRun.insert();
} catch (error) {
console.error(error);
console.log('Could not run patch', patch);
}
}