2
0
mirror of https://github.com/frappe/books.git synced 2024-12-23 11:29:03 +00:00
books/src/migrate.js
2022-01-21 02:27:29 +05:30

48 lines
1.4 KiB
JavaScript

import frappe from 'frappe';
import runPatches from 'frappe/model/runPatches';
import patches from '../patches/patches.json';
export default async function runMigrate() {
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 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 getCanRunPatches() {
return (
(
await frappe.db
.knex('sqlite_master')
.where({ type: 'table', name: 'PatchRun' })
.select('name')
).length > 0
);
}