From bc39fa4feb5402f7cffa8eed9fc972e1c9a7acb1 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Tue, 28 Jan 2020 16:30:23 +0530 Subject: [PATCH] feat: PatchRun - Run ad-hoc patch code - Run patches based on patchOrder - Skip already run patches --- index.js | 2 +- model/migrate.js | 40 +++++++++++++++++++++++++++++ models/doctype/PatchRun/PatchRun.js | 10 ++++++++ models/index.js | 3 ++- 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 model/migrate.js create mode 100644 models/doctype/PatchRun/PatchRun.js diff --git a/index.js b/index.js index e9485380..0b2642fd 100644 --- a/index.js +++ b/index.js @@ -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(); diff --git a/model/migrate.js b/model/migrate.js new file mode 100644 index 00000000..1e74425b --- /dev/null +++ b/model/migrate.js @@ -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); + } +} diff --git a/models/doctype/PatchRun/PatchRun.js b/models/doctype/PatchRun/PatchRun.js new file mode 100644 index 00000000..3ecad237 --- /dev/null +++ b/models/doctype/PatchRun/PatchRun.js @@ -0,0 +1,10 @@ +module.exports = { + name: 'PatchRun', + fields: [ + { + fieldname: 'name', + fieldtype: 'Data', + label: 'Name' + } + ] +}; diff --git a/models/index.js b/models/index.js index 5b11a021..95a91d6c 100644 --- a/models/index.js +++ b/models/index.js @@ -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') };