2
0
mirror of https://github.com/frappe/books.git synced 2025-02-04 13:08:29 +00:00
books/src/migrate.js

42 lines
1.0 KiB
JavaScript
Raw Normal View History

import frappe from 'frappejs';
import runPatches from 'frappejs/model/runPatches';
import patchesTxt from '../patches/patches.txt';
2020-01-28 16:40:01 +05:30
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) => {
2020-01-28 16:40:01 +05:30
if (fileName === './index.js') return;
let method;
try {
method = requirePatch(fileName).default;
2020-01-28 16:40:01 +05:30
} catch (error) {
console.error(error);
method = null;
}
fileName = fileName.slice(2, -3);
if (fileName && method) {
allPatches[fileName] = method;
}
});
return allPatches;
}