From 7a47ee2e2272fdb2ef6bfc1c7ff8366de387af6e Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Tue, 30 Nov 2021 17:39:40 +0530 Subject: [PATCH] refactor: update patch run pre and post migration patches --- .../makePaymentRefIdNullable.js | 0 patches/patches.json | 7 ++ patches/patches.txt | 1 - src/migrate.js | 64 ++++++++++--------- 4 files changed, 42 insertions(+), 30 deletions(-) rename patches/{v0_0_3 => 0.0.3}/makePaymentRefIdNullable.js (100%) create mode 100644 patches/patches.json delete mode 100644 patches/patches.txt diff --git a/patches/v0_0_3/makePaymentRefIdNullable.js b/patches/0.0.3/makePaymentRefIdNullable.js similarity index 100% rename from patches/v0_0_3/makePaymentRefIdNullable.js rename to patches/0.0.3/makePaymentRefIdNullable.js diff --git a/patches/patches.json b/patches/patches.json new file mode 100644 index 00000000..31ad50ee --- /dev/null +++ b/patches/patches.json @@ -0,0 +1,7 @@ +[ + { + "version": "0.0.3", + "fileName": "makePaymentRefIdNullable", + "beforeMigrate": true + } +] diff --git a/patches/patches.txt b/patches/patches.txt deleted file mode 100644 index bddd2fe4..00000000 --- a/patches/patches.txt +++ /dev/null @@ -1 +0,0 @@ -v0_0_3/makePaymentRefIdNullable \ No newline at end of file diff --git a/src/migrate.js b/src/migrate.js index 19c24a11..e2c7054b 100644 --- a/src/migrate.js +++ b/src/migrate.js @@ -1,41 +1,47 @@ import frappe from 'frappejs'; import runPatches from 'frappejs/model/runPatches'; -import patchesTxt from '../patches/patches.txt'; -const requirePatch = require.context('../patches', true, /\w+\.(js)$/); +import patches from '../patches/patches.json'; export default async function runMigrate() { - if (await canRunPatches()) { - const patchOrder = patchesTxt.split('\n'); - const allPatches = getAllPatches(); - await runPatches(allPatches, patchOrder); + const canRunPatches = await getCanRunPatches(); + if (!canRunPatches) { + return await frappe.db.migrate(); } + + const patchList = await fetchPatchList(); + await runPatches(patchList.filter(({ beforeMigrate }) => beforeMigrate)); await frappe.db.migrate(); + await runPatches(patchList.filter(({ beforeMigrate }) => !beforeMigrate)); } -async function canRunPatches() { - return ( - (await frappe.db - .knex('sqlite_master') - .where({ type: 'table', name: 'PatchRun' }) - .select('name').length) > 0 +async function fetchPatchList() { + return await Promise.all( + patches.map(async ({ version, fileName, beforeMigrate }) => { + if (typeof beforeMigrate === 'undefined') { + beforeMigrate = true; + } + + const patchName = `${version}/${fileName}`; + // This import is pseudo dynamic + // webpack runs static analysis on the static portion of the import + // i.e. '../patches/' this may break on windows due to the path + // delimiter used. + // + // Only way to fix this is probably upgrading the build from + // webpack to something else. + const patchFunction = (await import('../patches/' + patchName)).default; + return { patchName, patchFunction, beforeMigrate }; + }) ); } -async function getAllPatches() { - const allPatches = {}; - requirePatch.keys().forEach((fileName) => { - if (fileName === './index.js') return; - let method; - try { - method = requirePatch(fileName).default; - } catch (error) { - console.error(error); - method = null; - } - fileName = fileName.slice(2, -3); - if (fileName && method) { - allPatches[fileName] = method; - } - }); - return allPatches; +async function getCanRunPatches() { + return ( + ( + await frappe.db + .knex('sqlite_master') + .where({ type: 'table', name: 'PatchRun' }) + .select('name') + ).length > 0 + ); }