Skip to content

Commit

Permalink
perf(MySQL): made some common errors related to corrupted tables non-…
Browse files Browse the repository at this point in the history
…blocking, closes #877
  • Loading branch information
Fabio286 committed Oct 8, 2024
1 parent 2f3f5de commit 9a0ad80
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 17 deletions.
31 changes: 21 additions & 10 deletions src/main/libs/clients/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() });
}
if (process.env.NODE_ENV === 'development' && process.type === 'browser') console.log(escapedSql);
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);
}
};

/**
Expand All @@ -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;

Expand All @@ -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: '',
Expand Down
2 changes: 1 addition & 1 deletion src/main/libs/clients/FirebirdSQLClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 15 additions & 4 deletions src/main/libs/clients/MySQLClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`');
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/main/libs/clients/PostgreSQLClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/main/libs/clients/SQLiteClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions src/renderer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 9a0ad80

Please sign in to comment.