2023-04-04 06:21:00 +00:00
|
|
|
import { emitMainProcessError, getDefaultMetaFieldValueMap } from '../helpers';
|
2022-03-24 13:13:59 +00:00
|
|
|
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
|
|
|
|
) {
|
2022-03-24 13:13:59 +00:00
|
|
|
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);
|
2022-03-24 13:13:59 +00:00
|
|
|
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;
|
2022-03-24 13:13:59 +00:00
|
|
|
try {
|
|
|
|
await patch.patch.execute(dm);
|
2023-04-04 06:21:00 +00:00
|
|
|
} 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-04-04 06:21:00 +00:00
|
|
|
}
|
2022-03-24 13:13:59 +00:00
|
|
|
}
|
|
|
|
|
2023-07-12 09:28:14 +00:00
|
|
|
await makeEntry(patch.name, version, failed, dm);
|
2022-03-24 13:13:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-07-12 09:28:14 +00:00
|
|
|
async function makeEntry(
|
|
|
|
patchName: string,
|
|
|
|
version: string,
|
|
|
|
failed: boolean,
|
|
|
|
dm: DatabaseManager
|
|
|
|
) {
|
2022-03-24 13:13:59 +00:00
|
|
|
const defaultFieldValueMap = getDefaultMetaFieldValueMap() as FieldValueMap;
|
2023-07-12 09:28:14 +00:00
|
|
|
|
2022-03-24 13:13:59 +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);
|
2022-03-24 13:13:59 +00:00
|
|
|
}
|