2
0
mirror of https://github.com/frappe/books.git synced 2024-12-25 12:10:06 +00:00

feat: PatchRun

- Run ad-hoc patch code
- Run patches based on patchOrder
- Skip already run patches
This commit is contained in:
Faris Ansari 2020-01-28 16:30:23 +05:30
parent b00de586cf
commit bc39fa4feb
4 changed files with 53 additions and 2 deletions

View File

@ -208,7 +208,7 @@ module.exports = {
return newDoc;
},
async getNewDoc(doctype) {
getNewDoc(doctype) {
let doc = this.newDoc({ doctype: doctype });
doc._notInserted = true;
doc.name = frappe.getRandomString();

40
model/migrate.js Normal file
View File

@ -0,0 +1,40 @@
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);
}
}

View File

@ -0,0 +1,10 @@
module.exports = {
name: 'PatchRun',
fields: [
{
fieldname: 'name',
fieldtype: 'Data',
label: 'Name'
}
]
};

View File

@ -11,5 +11,6 @@ module.exports = {
ToDo: require('./doctype/ToDo/ToDo.js'),
User: require('./doctype/User/User.js'),
UserRole: require('./doctype/UserRole/UserRole.js'),
File: require('./doctype/File/File.js')
File: require('./doctype/File/File.js'),
PatchRun: require('./doctype/PatchRun/PatchRun.js')
};