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') };