mirror of
https://github.com/frappe/books.git
synced 2025-02-02 20:18:26 +00:00
Merge pull request #265 from 18alantom/update-patchrun
refactor: update patch run pre and post migration patches
This commit is contained in:
commit
24b84b834b
7
patches/patches.json
Normal file
7
patches/patches.json
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"version": "0.0.3",
|
||||||
|
"fileName": "makePaymentRefIdNullable",
|
||||||
|
"beforeMigrate": true
|
||||||
|
}
|
||||||
|
]
|
@ -1 +0,0 @@
|
|||||||
v0_0_3/makePaymentRefIdNullable
|
|
@ -1,41 +1,47 @@
|
|||||||
import frappe from 'frappejs';
|
import frappe from 'frappejs';
|
||||||
import runPatches from 'frappejs/model/runPatches';
|
import runPatches from 'frappejs/model/runPatches';
|
||||||
import patchesTxt from '../patches/patches.txt';
|
import patches from '../patches/patches.json';
|
||||||
const requirePatch = require.context('../patches', true, /\w+\.(js)$/);
|
|
||||||
|
|
||||||
export default async function runMigrate() {
|
export default async function runMigrate() {
|
||||||
if (await canRunPatches()) {
|
const canRunPatches = await getCanRunPatches();
|
||||||
const patchOrder = patchesTxt.split('\n');
|
if (!canRunPatches) {
|
||||||
const allPatches = getAllPatches();
|
return await frappe.db.migrate();
|
||||||
await runPatches(allPatches, patchOrder);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const patchList = await fetchPatchList();
|
||||||
|
await runPatches(patchList.filter(({ beforeMigrate }) => beforeMigrate));
|
||||||
await frappe.db.migrate();
|
await frappe.db.migrate();
|
||||||
|
await runPatches(patchList.filter(({ beforeMigrate }) => !beforeMigrate));
|
||||||
}
|
}
|
||||||
|
|
||||||
async function canRunPatches() {
|
async function fetchPatchList() {
|
||||||
return (
|
return await Promise.all(
|
||||||
(await frappe.db
|
patches.map(async ({ version, fileName, beforeMigrate }) => {
|
||||||
.knex('sqlite_master')
|
if (typeof beforeMigrate === 'undefined') {
|
||||||
.where({ type: 'table', name: 'PatchRun' })
|
beforeMigrate = true;
|
||||||
.select('name').length) > 0
|
}
|
||||||
|
|
||||||
|
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() {
|
async function getCanRunPatches() {
|
||||||
const allPatches = {};
|
return (
|
||||||
requirePatch.keys().forEach((fileName) => {
|
(
|
||||||
if (fileName === './index.js') return;
|
await frappe.db
|
||||||
let method;
|
.knex('sqlite_master')
|
||||||
try {
|
.where({ type: 'table', name: 'PatchRun' })
|
||||||
method = requirePatch(fileName).default;
|
.select('name')
|
||||||
} catch (error) {
|
).length > 0
|
||||||
console.error(error);
|
);
|
||||||
method = null;
|
|
||||||
}
|
|
||||||
fileName = fileName.slice(2, -3);
|
|
||||||
if (fileName && method) {
|
|
||||||
allPatches[fileName] = method;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return allPatches;
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user