perf(MySQL): made some common errors related to corrupted tables non-blocking, closes #877

This commit is contained in:
Fabio Di Stasio 2024-10-08 18:34:29 +02:00
parent 2f3f5de8d6
commit 9a0ad80bb5
6 changed files with 47 additions and 16 deletions

View File

@ -3,14 +3,25 @@ import mysql from 'mysql2/promise';
import * as pg from 'pg';
import SSH2Promise = require('@fabio286/ssh2-promise');
const queryLogger = ({ sql, cUid }: {sql: string; cUid: string}) => {
// Remove comments, newlines and multiple spaces
const escapedSql = sql.replace(/(\/\*(.|[\r\n])*?\*\/)|(--(.*|[\r\n]))/gm, '').replace(/\s\s+/g, ' ');
if (process.type !== undefined) {
const mainWindow = require('electron').webContents.fromId(1);
mainWindow.send('query-log', { cUid, sql: escapedSql, date: new Date() });
export type LoggerLevel = 'query' | 'error'
const ipcLogger = ({ content, cUid, level }: {content: string; cUid: string; level: LoggerLevel}) => {
if (level === 'error') {
if (process.type !== undefined) {
const mainWindow = require('electron').webContents.fromId(1);
mainWindow.send('non-blocking-exception', { cUid, message: content, date: new Date() });
}
if (process.env.NODE_ENV === 'development' && process.type === 'browser') console.log(content);
}
else if (level === 'query') {
// Remove comments, newlines and multiple spaces
const escapedSql = content.replace(/(\/\*(.|[\r\n])*?\*\/)|(--(.*|[\r\n]))/gm, '').replace(/\s\s+/g, ' ');
if (process.type !== undefined) {
const mainWindow = require('electron').webContents.fromId(1);
mainWindow.send('query-log', { cUid, sql: escapedSql, date: new Date() });
}
if (process.env.NODE_ENV === 'development' && process.type === 'browser') console.log(escapedSql);
}
if (process.env.NODE_ENV === 'development' && process.type === 'browser') console.log(escapedSql);
};
/**
@ -22,7 +33,7 @@ export abstract class BaseClient {
protected _params: mysql.ConnectionOptions | pg.ClientConfig | { databasePath: string; readonly: boolean};
protected _poolSize: number;
protected _ssh?: SSH2Promise;
protected _logger: (args: {sql: string; cUid: string}) => void;
protected _logger: (args: {content: string; cUid: string; level: LoggerLevel}) => void;
protected _queryDefaults: antares.QueryBuilderObject;
protected _query: antares.QueryBuilderObject;
@ -31,7 +42,7 @@ export abstract class BaseClient {
this._cUid = args.uid;
this._params = args.params;
this._poolSize = args.poolSize || undefined;
this._logger = args.logger || queryLogger;
this._logger = args.logger || ipcLogger;
this._queryDefaults = {
schema: '',

View File

@ -1024,7 +1024,7 @@ export class FirebirdSQLClient extends BaseClient {
alias: string;
}
this._logger({ cUid: this._cUid, sql });
this._logger({ cUid: this._cUid, content: sql, level: 'query' });
args = {
nest: false,

View File

@ -354,10 +354,21 @@ export class MySQLClient extends BaseClient {
if (this._params.schema)
filteredDatabases = filteredDatabases.filter(db => db.Database === this._params.schema);
const { rows: functions } = await this.raw('SHOW FUNCTION STATUS');
const { rows: procedures } = await this.raw('SHOW PROCEDURE STATUS');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
/* eslint-disable @typescript-eslint/no-explicit-any */
let functions: any[] = [];
let procedures: any[] = [];
let schedulers: any[] = [];
/* eslint-enable @typescript-eslint/no-explicit-any */
try {
const { rows: functionRows } = await this.raw('SHOW FUNCTION STATUS');
const { rows: procedureRows } = await this.raw('SHOW PROCEDURE STATUS');
functions = functionRows;
procedures = procedureRows;
}
catch (err) {
this._logger({ content: err.sqlMessage, cUid: this._cUid, level: 'error' });
}
try { // Avoid exception with event_scheduler DISABLED with MariaDB 10
const { rows } = await this.raw('SELECT *, EVENT_SCHEMA AS `Db`, EVENT_NAME AS `Name` FROM information_schema.`EVENTS`');
@ -1667,7 +1678,7 @@ export class MySQLClient extends BaseClient {
}
async raw<T = antares.QueryResult> (sql: string, args?: antares.QueryParams) {
this._logger({ cUid: this._cUid, sql });
this._logger({ cUid: this._cUid, content: sql, level: 'query' });
args = {
nest: false,

View File

@ -1645,7 +1645,7 @@ export class PostgreSQLClient extends BaseClient {
}
async raw<T = antares.QueryResult> (sql: string, args?: antares.QueryParams) {
this._logger({ cUid: this._cUid, sql });
this._logger({ cUid: this._cUid, content: sql, level: 'query' });
args = {
nest: false,

View File

@ -612,7 +612,7 @@ export class SQLiteClient extends BaseClient {
}
async raw<T = antares.QueryResult> (sql: string, args?: antares.QueryParams) {
this._logger({ cUid: this._cUid, sql });// TODO: replace BLOB content with a placeholder
this._logger({ cUid: this._cUid, content: sql, level: 'query' });// TODO: replace BLOB content with a placeholder
args = {
nest: false,

View File

@ -44,6 +44,15 @@ ipcRenderer.on('unhandled-exception', (event, error) => {
date: new Date()
});
});
ipcRenderer.on('non-blocking-exception', (event, error) => {
useNotificationsStore().addNotification({ status: 'error', message: error.message });
useConsoleStore().putLog('debug', {
level: 'error',
process: 'main',
message: error.message,
date: new Date()
});
});
// IPC query logs
ipcRenderer.on('query-log', (event, logRecord: QueryLog) => {