2
0
mirror of https://github.com/frappe/books.git synced 2024-11-14 09:24:04 +00:00

Merge pull request #464 from 18alantom/fix-dbfile-issues

fix: db file related issues
This commit is contained in:
Alan 2022-08-31 02:26:11 -07:00 committed by GitHub
commit 2351b90f59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 164 additions and 42 deletions

View File

@ -1,9 +1,12 @@
import { constants } from 'fs';
import fs from 'fs/promises'; import fs from 'fs/promises';
import path from 'path'; import path from 'path';
import { DatabaseDemuxBase, DatabaseMethod } from 'utils/db/types'; import { DatabaseDemuxBase, DatabaseMethod } from 'utils/db/types';
import { getSchemas } from '../../schemas'; import { getSchemas } from '../../schemas';
import { databaseMethodSet } from '../helpers'; import {
databaseMethodSet,
emitMainProcessError,
unlinkIfExists,
} from '../helpers';
import patches from '../patches'; import patches from '../patches';
import { BespokeQueries } from './bespoke'; import { BespokeQueries } from './bespoke';
import DatabaseCore from './core'; import DatabaseCore from './core';
@ -22,7 +25,7 @@ export class DatabaseManager extends DatabaseDemuxBase {
} }
async createNewDatabase(dbPath: string, countryCode: string) { async createNewDatabase(dbPath: string, countryCode: string) {
await this.#unlinkIfExists(dbPath); await unlinkIfExists(dbPath);
return await this.connectToDatabase(dbPath, countryCode); return await this.connectToDatabase(dbPath, countryCode);
} }
@ -61,12 +64,35 @@ export class DatabaseManager extends DatabaseDemuxBase {
try { try {
await this.#runPatchesAndMigrate(); await this.#runPatchesAndMigrate();
} catch (err) { } catch (err) {
console.error(err); this.#handleFailedMigration(err, dbPath, copyPath);
await this.db!.close();
copyPath && (await fs.copyFile(copyPath, dbPath));
throw err;
} finally { } finally {
copyPath && (await fs.unlink(copyPath)); await unlinkIfExists(copyPath);
}
}
async #handleFailedMigration(
error: unknown,
dbPath: string,
copyPath: string | null
) {
await this.db!.close();
if (copyPath) {
await this.#restoreDbCopy(dbPath, copyPath);
}
if (error instanceof Error) {
error.message = `failed migration\n${error.message}`;
}
throw error;
}
async #restoreDbCopy(dbPath: string, copyPath: string) {
try {
await fs.copyFile(copyPath!, dbPath);
} catch (err) {
emitMainProcessError(err);
} }
} }
@ -130,17 +156,6 @@ export class DatabaseManager extends DatabaseDemuxBase {
return await queryFunction(this.db!, ...args); return await queryFunction(this.db!, ...args);
} }
async #unlinkIfExists(dbPath: string) {
const exists = await fs
.access(dbPath, constants.W_OK)
.then(() => true)
.catch(() => false);
if (exists) {
fs.unlink(dbPath);
}
}
async #getIsFirstRun(): Promise<boolean> { async #getIsFirstRun(): Promise<boolean> {
if (!this.#isInitialized) { if (!this.#isInitialized) {
return true; return true;
@ -160,7 +175,14 @@ export class DatabaseManager extends DatabaseDemuxBase {
const dir = path.parse(src).dir; const dir = path.parse(src).dir;
const dest = path.join(dir, '__premigratory_temp.db'); const dest = path.join(dir, '__premigratory_temp.db');
await fs.copyFile(src, dest);
try {
await fs.copyFile(src, dest);
} catch (err) {
emitMainProcessError(err);
return null;
}
return dest; return dest;
} }
} }

View File

@ -1,4 +1,7 @@
import { constants } from 'fs';
import fs from 'fs/promises';
import { DatabaseMethod } from 'utils/db/types'; import { DatabaseMethod } from 'utils/db/types';
import { CUSTOM_EVENTS } from 'utils/messages';
import { KnexColumnType } from './database/types'; import { KnexColumnType } from './database/types';
export const sqliteTypeMap: Record<string, KnexColumnType> = { export const sqliteTypeMap: Record<string, KnexColumnType> = {
@ -47,3 +50,32 @@ export const databaseMethodSet: Set<DatabaseMethod> = new Set([
'close', 'close',
'exists', 'exists',
]); ]);
export function emitMainProcessError(
error: unknown,
more?: Record<string, unknown>
) {
(process.emit as Function)(CUSTOM_EVENTS.MAIN_PROCESS_ERROR, error, more);
}
export async function checkFileAccess(filePath: string, mode?: number) {
mode ??= constants.W_OK;
return await fs
.access(filePath, mode)
.then(() => true)
.catch(() => false);
}
export async function unlinkIfExists(filePath: unknown) {
if (!filePath || typeof filePath !== 'string') {
return false;
}
const exists = await checkFileAccess(filePath);
if (exists) {
await fs.unlink(filePath);
return true;
}
return false;
}

View File

@ -73,3 +73,17 @@ export async function getErrorHandledReponse(func: () => Promise<unknown>) {
export function rendererLog(main: Main, ...args: unknown[]) { export function rendererLog(main: Main, ...args: unknown[]) {
main.mainWindow?.webContents.send(IPC_CHANNELS.CONSOLE_LOG, ...args); main.mainWindow?.webContents.send(IPC_CHANNELS.CONSOLE_LOG, ...args);
} }
export function isNetworkError(error: Error) {
switch (error?.message) {
case 'net::ERR_INTERNET_DISCONNECTED':
case 'net::ERR_PROXY_CONNECTION_FAILED':
case 'net::ERR_CONNECTION_RESET':
case 'net::ERR_CONNECTION_CLOSE':
case 'net::ERR_NAME_NOT_RESOLVED':
case 'net::ERR_CONNECTION_TIMED_OUT':
return true;
default:
return false;
}
}

View File

@ -1,23 +1,24 @@
import { emitMainProcessError } from 'backend/helpers';
import { app, dialog } from 'electron'; import { app, dialog } from 'electron';
import { autoUpdater, UpdateInfo } from 'electron-updater'; import { autoUpdater, UpdateInfo } from 'electron-updater';
import { Main } from '../main'; import { Main } from '../main';
import { IPC_CHANNELS } from '../utils/messages'; import { isNetworkError } from './helpers';
export default function registerAutoUpdaterListeners(main: Main) { export default function registerAutoUpdaterListeners(main: Main) {
autoUpdater.autoDownload = false; autoUpdater.autoDownload = false;
autoUpdater.allowPrerelease = true;
autoUpdater.autoInstallOnAppQuit = true; autoUpdater.autoInstallOnAppQuit = true;
autoUpdater.on('error', (error) => { autoUpdater.on('error', (error) => {
if (!main.checkedForUpdate) { if (!main.checkedForUpdate) {
main.checkedForUpdate = true; main.checkedForUpdate = true;
}
if (isNetworkError(error)) {
return; return;
} }
main.mainWindow!.webContents.send(IPC_CHANNELS.MAIN_PROCESS_ERROR, error); emitMainProcessError(error);
dialog.showErrorBox(
'Update Error: ',
error == null ? 'unknown' : (error.stack || error).toString()
);
}); });
autoUpdater.on('update-available', async (info: UpdateInfo) => { autoUpdater.on('update-available', async (info: UpdateInfo) => {
@ -30,7 +31,7 @@ export default function registerAutoUpdaterListeners(main: Main) {
if (!isCurrentBeta && isNextBeta) { if (!isCurrentBeta && isNextBeta) {
const option = await dialog.showMessageBox({ const option = await dialog.showMessageBox({
type: 'info', type: 'info',
title: `Update Frappe Books?`, title: 'Update Available',
message: `Download version ${nextVersion}?`, message: `Download version ${nextVersion}?`,
buttons: ['Yes', 'No'], buttons: ['Yes', 'No'],
}); });
@ -44,4 +45,19 @@ export default function registerAutoUpdaterListeners(main: Main) {
await autoUpdater.downloadUpdate(); await autoUpdater.downloadUpdate();
}); });
autoUpdater.on('update-downloaded', async () => {
const option = await dialog.showMessageBox({
type: 'info',
title: 'Update Downloaded',
message: 'Restart Frappe Books to install update?',
buttons: ['Yes', 'No'],
});
if (option.response === 1) {
return;
}
autoUpdater.quitAndInstall();
});
} }

View File

@ -1,3 +1,4 @@
import { emitMainProcessError } from 'backend/helpers';
import { app, dialog, ipcMain } from 'electron'; import { app, dialog, ipcMain } from 'electron';
import { autoUpdater } from 'electron-updater'; import { autoUpdater } from 'electron-updater';
import fs from 'fs/promises'; import fs from 'fs/promises';
@ -11,7 +12,8 @@ import { getLanguageMap } from './getLanguageMap';
import { import {
getConfigFilesWithModified, getConfigFilesWithModified,
getErrorHandledReponse, getErrorHandledReponse,
setAndGetCleanedConfigFiles, isNetworkError,
setAndGetCleanedConfigFiles
} from './helpers'; } from './helpers';
import { saveHtmlAsPdf } from './saveHtmlAsPdf'; import { saveHtmlAsPdf } from './saveHtmlAsPdf';
@ -52,10 +54,20 @@ export default function registerIpcMainActionListeners(main: Main) {
}); });
ipcMain.handle(IPC_ACTIONS.CHECK_FOR_UPDATES, async () => { ipcMain.handle(IPC_ACTIONS.CHECK_FOR_UPDATES, async () => {
if (!main.isDevelopment && !main.checkedForUpdate) { if (main.isDevelopment || main.checkedForUpdate) {
await autoUpdater.checkForUpdates(); return;
main.checkedForUpdate = true;
} }
try {
await autoUpdater.checkForUpdates();
} catch (error) {
if (isNetworkError(error as Error)) {
return;
}
emitMainProcessError(error);
}
main.checkedForUpdate = true;
}); });
ipcMain.handle(IPC_ACTIONS.GET_LANGUAGE_MAP, async (event, code) => { ipcMain.handle(IPC_ACTIONS.GET_LANGUAGE_MAP, async (event, code) => {

View File

@ -1,5 +1,5 @@
import { app } from 'electron'; import { app } from 'electron';
import { IPC_CHANNELS } from 'utils/messages'; import { CUSTOM_EVENTS, IPC_CHANNELS } from 'utils/messages';
import { Main } from '../main'; import { Main } from '../main';
export default function registerProcessListeners(main: Main) { export default function registerProcessListeners(main: Main) {
@ -17,12 +17,26 @@ export default function registerProcessListeners(main: Main) {
} }
} }
process.on(CUSTOM_EVENTS.MAIN_PROCESS_ERROR, (error, more) => {
main.mainWindow!.webContents.send(
IPC_CHANNELS.LOG_MAIN_PROCESS_ERROR,
error,
more
);
});
process.on('unhandledRejection', (error) => { process.on('unhandledRejection', (error) => {
main.mainWindow!.webContents.send(IPC_CHANNELS.MAIN_PROCESS_ERROR, error); main.mainWindow!.webContents.send(
IPC_CHANNELS.LOG_MAIN_PROCESS_ERROR,
error
);
}); });
process.on('uncaughtException', (error) => { process.on('uncaughtException', (error) => {
main.mainWindow!.webContents.send(IPC_CHANNELS.MAIN_PROCESS_ERROR, error); main.mainWindow!.webContents.send(
IPC_CHANNELS.LOG_MAIN_PROCESS_ERROR,
error
);
setTimeout(() => process.exit(1), 10000); setTimeout(() => process.exit(1), 10000);
}); });
} }

View File

@ -57,7 +57,11 @@ export function getErrorLogObject(
error: Error, error: Error,
more: Record<string, unknown> more: Record<string, unknown>
): ErrorLog { ): ErrorLog {
const { name, stack, message } = error; const { name, stack, message, cause } = error;
if (cause) {
more.cause = cause;
}
const errorLogObj = { name, stack, message, more }; const errorLogObj = { name, stack, message, more };
fyo.errorLog.push(errorLogObj); fyo.errorLog.push(errorLogObj);

View File

@ -4,13 +4,16 @@ import { fyo } from 'src/initFyo';
import { IPC_CHANNELS } from 'utils/messages'; import { IPC_CHANNELS } from 'utils/messages';
export default function registerIpcRendererListeners() { export default function registerIpcRendererListeners() {
ipcRenderer.on(IPC_CHANNELS.MAIN_PROCESS_ERROR, async (_, error) => { ipcRenderer.on(
if (fyo.store.isDevelopment) { IPC_CHANNELS.LOG_MAIN_PROCESS_ERROR,
console.error(error); async (_, error, more) => {
} if (fyo.store.isDevelopment) {
console.error(error);
}
await handleError(true, error as Error); await handleError(true, error as Error, more);
}); }
);
ipcRenderer.on(IPC_CHANNELS.CONSOLE_LOG, (_, ...stuff: unknown[]) => { ipcRenderer.on(IPC_CHANNELS.CONSOLE_LOG, (_, ...stuff: unknown[]) => {
if (!fyo.store.isDevelopment) { if (!fyo.store.isDevelopment) {

View File

@ -33,7 +33,7 @@ export enum IPC_ACTIONS {
// ipcMain.send(...) // ipcMain.send(...)
export enum IPC_CHANNELS { export enum IPC_CHANNELS {
MAIN_PROCESS_ERROR = 'main-process-error', LOG_MAIN_PROCESS_ERROR = 'main-process-error',
CONSOLE_LOG = 'console-log', CONSOLE_LOG = 'console-log',
} }
@ -42,3 +42,8 @@ export enum DB_CONN_FAILURE {
CANT_OPEN = 'cant-open', CANT_OPEN = 'cant-open',
CANT_CONNECT = 'cant-connect', CANT_CONNECT = 'cant-connect',
} }
// events
export enum CUSTOM_EVENTS {
MAIN_PROCESS_ERROR = 'main-process-error',
}