2
0
mirror of https://github.com/frappe/books.git synced 2024-11-15 01:44:04 +00:00
books/backend/database/runPatch.ts

61 lines
1.5 KiB
TypeScript
Raw Normal View History

import { emitMainProcessError, getDefaultMetaFieldValueMap } from '../helpers';
import { DatabaseManager } from './manager';
import { FieldValueMap, Patch } from './types';
2023-07-12 09:28:14 +00:00
export async function runPatches(
patches: Patch[],
dm: DatabaseManager,
version: string
) {
const list: { name: string; success: boolean }[] = [];
for (const patch of patches) {
2023-07-12 09:28:14 +00:00
const success = await runPatch(patch, dm, version);
list.push({ name: patch.name, success });
}
return list;
}
2023-07-12 09:28:14 +00:00
async function runPatch(
patch: Patch,
dm: DatabaseManager,
version: string
): Promise<boolean> {
let failed = false;
try {
await patch.patch.execute(dm);
} catch (error) {
2023-07-12 09:28:14 +00:00
failed = true;
if (error instanceof Error) {
error.message = `Patch Failed: ${patch.name}\n${error.message}`;
emitMainProcessError(error, { patchName: patch.name, notifyUser: false });
}
}
2023-07-12 09:28:14 +00:00
await makeEntry(patch.name, version, failed, dm);
return true;
}
2023-07-12 09:28:14 +00:00
async function makeEntry(
patchName: string,
version: string,
failed: boolean,
dm: DatabaseManager
) {
const defaultFieldValueMap = getDefaultMetaFieldValueMap() as FieldValueMap;
2023-07-12 09:28:14 +00:00
defaultFieldValueMap.name = patchName;
2023-07-12 09:28:14 +00:00
defaultFieldValueMap.failed = failed;
defaultFieldValueMap.version = version;
try {
await dm.db!.insert('PatchRun', defaultFieldValueMap);
} catch {
/**
* Error is thrown if PatchRun table hasn't been migrated.
* In this case, PatchRun will migrated post pre-migration-patches
* are run and rerun the patch.
*/
return;
}
}