2022-04-17 12:01:07 +02:00
|
|
|
import * as mysql from 'mysql2';
|
|
|
|
import * as importer from 'common/interfaces/importer';
|
|
|
|
import * as fs from 'fs/promises';
|
2022-04-07 12:49:34 +02:00
|
|
|
import MySQLParser from '../../parsers/MySQLParser';
|
2021-12-28 15:30:07 +01:00
|
|
|
import { BaseImporter } from '../BaseImporter';
|
|
|
|
|
2022-04-07 12:49:34 +02:00
|
|
|
export default class MySQLImporter extends BaseImporter {
|
2022-04-17 12:01:07 +02:00
|
|
|
protected _client: mysql.Pool
|
|
|
|
|
|
|
|
constructor (client: mysql.Pool, options: importer.ImportOptions) {
|
2021-12-28 15:30:07 +01:00
|
|
|
super(options);
|
2022-02-24 13:14:57 +01:00
|
|
|
this._client = client;
|
2021-12-28 15:30:07 +01:00
|
|
|
}
|
|
|
|
|
2022-04-17 12:01:07 +02:00
|
|
|
async import (): Promise<void> {
|
2022-02-24 13:14:57 +01:00
|
|
|
try {
|
|
|
|
const { size: totalFileSize } = await fs.stat(this._options.file);
|
2022-04-07 12:49:34 +02:00
|
|
|
const parser = new MySQLParser();
|
2022-02-24 13:14:57 +01:00
|
|
|
let readPosition = 0;
|
|
|
|
let queryCount = 0;
|
2021-12-28 15:30:07 +01:00
|
|
|
|
2022-02-24 13:14:57 +01:00
|
|
|
this.emitUpdate({
|
|
|
|
fileSize: totalFileSize,
|
|
|
|
readPosition: 0,
|
2022-03-05 18:54:08 +01:00
|
|
|
percentage: 0,
|
|
|
|
queryCount: 0
|
2021-12-28 15:30:07 +01:00
|
|
|
});
|
|
|
|
|
2022-02-24 13:14:57 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._fileHandler.pipe(parser);
|
|
|
|
|
2022-03-05 17:00:05 +01:00
|
|
|
parser.on('error', reject);
|
2022-02-24 13:14:57 +01:00
|
|
|
|
2022-03-10 10:23:22 +01:00
|
|
|
parser.on('close', async () => {
|
2022-03-05 18:54:08 +01:00
|
|
|
console.log('TOTAL QUERIES', queryCount);
|
|
|
|
console.log('import end');
|
|
|
|
resolve();
|
2022-02-24 13:14:57 +01:00
|
|
|
});
|
|
|
|
|
2022-03-05 18:54:08 +01:00
|
|
|
parser.on('data', async (query) => {
|
2022-02-24 13:14:57 +01:00
|
|
|
queryCount++;
|
2022-03-10 10:23:22 +01:00
|
|
|
parser.pause();
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this._client.query(query);
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
this.emit('query-error', {
|
|
|
|
sql: query,
|
|
|
|
message: error.sqlMessage,
|
|
|
|
sqlSnippet: error.sql,
|
|
|
|
time: new Date().getTime()
|
|
|
|
});
|
|
|
|
}
|
2021-12-28 15:30:07 +01:00
|
|
|
|
2022-02-24 13:14:57 +01:00
|
|
|
this.emitUpdate({
|
2022-03-05 18:54:08 +01:00
|
|
|
queryCount,
|
2022-02-24 13:14:57 +01:00
|
|
|
readPosition,
|
|
|
|
percentage: readPosition / totalFileSize * 100
|
|
|
|
});
|
2022-03-10 10:23:22 +01:00
|
|
|
this._fileHandler.pipe(parser);
|
|
|
|
parser.resume();
|
|
|
|
});
|
|
|
|
|
|
|
|
parser.on('pause', () => {
|
|
|
|
this._fileHandler.unpipe(parser);
|
2022-04-17 12:01:07 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
(this._fileHandler as any).readableFlowing = false;
|
2022-03-10 10:23:22 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
this._fileHandler.on('data', (chunk) => {
|
|
|
|
readPosition += chunk.length;
|
2022-02-24 13:14:57 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
this._fileHandler.on('error', (err) => {
|
|
|
|
console.log(err);
|
|
|
|
reject(err);
|
|
|
|
});
|
2021-12-28 15:30:07 +01:00
|
|
|
});
|
2022-02-24 13:14:57 +01:00
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
2021-12-28 15:30:07 +01:00
|
|
|
}
|
|
|
|
}
|