Skip to content

Commit

Permalink
fix: unable to import after a failed import, fixes #515
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio286 committed Feb 13, 2023
1 parent fe9817b commit b1fbc43
Showing 1 changed file with 81 additions and 47 deletions.
128 changes: 81 additions & 47 deletions src/main/workers/importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,72 +6,106 @@ import { PostgreSQLClient } from '../libs/clients/PostgreSQLClient';
import { ClientsFactory } from '../libs/ClientsFactory';
import MySQLImporter from '../libs/importers/sql/MySQLlImporter';
import PostgreSQLImporter from '../libs/importers/sql/PostgreSQLImporter';
import SSHConfig from 'ssh2-promise/lib/sshConfig';
import { ImportOptions } from 'common/interfaces/importer';
let importer: antares.Importer;

process.on('message', async ({ type, dbConfig, options }) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
process.on('message', async ({ type, dbConfig, options }: {
type: string;
dbConfig: mysql.ConnectionOptions & { schema: string; ssl?: mysql.SslOptions; ssh?: SSHConfig; readonly: boolean }
| pg.ClientConfig & { schema: string; ssl?: mysql.SslOptions; ssh?: SSHConfig; readonly: boolean }
| { databasePath: string; readonly: boolean };
options: ImportOptions;
}) => {
if (type === 'init') {
const connection = await ClientsFactory.getClient({
client: options.type,
params: {
...dbConfig,
schema: options.schema
},
poolSize: 1
}) as MySQLClient | PostgreSQLClient;
try {
const connection = await ClientsFactory.getClient({
client: options.type,
params: {
...dbConfig,
schema: options.schema
},
poolSize: 1
}) as MySQLClient | PostgreSQLClient;

const pool = await connection.getConnectionPool();
const pool = await connection.getConnectionPool();

switch (options.type) {
case 'mysql':
case 'maria':
importer = new MySQLImporter(pool as unknown as mysql.Pool, options);
break;
case 'pg':
importer = new PostgreSQLImporter(pool as unknown as pg.PoolClient, options);
break;
default:
switch (options.type) {
case 'mysql':
case 'maria':
importer = new MySQLImporter(pool as unknown as mysql.Pool, options);
break;
case 'pg':
importer = new PostgreSQLImporter(pool as unknown as pg.PoolClient, options);
break;
default:
process.send({
type: 'error',
payload: `"${options.type}" importer not aviable`
});
return;
}

importer.once('error', err => {
console.error(err);
process.send({
type: 'error',
payload: `"${options.type}" importer not aviable`
payload: err.toString()
});
return;
}
});

importer.once('error', err => {
console.error(err);
process.send({
type: 'error',
payload: err.toString()
importer.once('end', () => {
process.send({
type: 'end',
payload: { cancelled: importer.isCancelled }
});
});
});

importer.once('end', () => {
process.send({
type: 'end',
payload: { cancelled: importer.isCancelled }
importer.once('cancel', () => {
process.send({ type: 'cancel' });
});
});

importer.once('cancel', () => {
process.send({ type: 'cancel' });
});
importer.on('progress', state => {
process.send({
type: 'import-progress',
payload: state
});
});

importer.on('progress', state => {
process.send({
type: 'import-progress',
payload: state
importer.on('query-error', state => {
process.send({
type: 'query-error',
payload: state
});
});
});

importer.on('query-error', state => {
importer.run();
}
catch (err) {
console.error(err);
process.send({
type: 'query-error',
payload: state
type: 'error',
payload: err.toString()
});
});

importer.run();
}
}
else if (type === 'cancel')
importer.cancel();
});

process.on('uncaughtException', (err) => {
console.error(err);
process.send({
type: 'error',
payload: err.toString()
});
});

process.on('unhandledRejection', (err) => {
console.error(err);
process.send({
type: 'error',
payload: err.toString()
});
});

0 comments on commit b1fbc43

Please sign in to comment.