mirror of
https://github.com/frappe/books.git
synced 2025-01-26 00:28:25 +00:00
c20cc5a5db
- minor refactors and formatting
42 lines
1.0 KiB
JavaScript
42 lines
1.0 KiB
JavaScript
import frappe from 'frappejs';
|
|
import runPatches from 'frappejs/model/runPatches';
|
|
import patchesTxt from '../patches/patches.txt';
|
|
const requirePatch = require.context('../patches', true, /\w+\.(js)$/);
|
|
|
|
export default async function runMigrate() {
|
|
if (await canRunPatches()) {
|
|
const patchOrder = patchesTxt.split('\n');
|
|
const allPatches = getAllPatches();
|
|
await runPatches(allPatches, patchOrder);
|
|
}
|
|
await frappe.db.migrate();
|
|
}
|
|
|
|
async function canRunPatches() {
|
|
return (
|
|
(await frappe.db
|
|
.knex('sqlite_master')
|
|
.where({ type: 'table', name: 'PatchRun' })
|
|
.select('name').length) > 0
|
|
);
|
|
}
|
|
|
|
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;
|
|
}
|