2
0
mirror of https://github.com/frappe/books.git synced 2024-11-08 14:50:56 +00:00
books/backend/database/runPatch.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

52 lines
1.3 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;
2022-03-25 10:12:39 +00:00
await dm.db!.insert('PatchRun', defaultFieldValueMap);
}