migrating to typescript
This commit is contained in:
parent
2ad0921b33
commit
93e4452642
|
@ -23,6 +23,7 @@ import Conversion from './Conversion';
|
|||
import DBAccess from './DBAccess';
|
||||
import DBAccessQueryResult from './DBAccessQueryResult';
|
||||
import DBVendors from './DBVendors';
|
||||
import { PoolClient } from 'pg';
|
||||
|
||||
/**
|
||||
* Decodes binary data from from textual representation in string.
|
||||
|
@ -41,6 +42,7 @@ export default async function (conversion: Conversion): Promise<Conversion> {
|
|||
|
||||
if (result.error) {
|
||||
// No need to continue if no 'bytea' or 'geometry' columns found.
|
||||
dbAccess.releasePgClient(<PoolClient>result.client);
|
||||
return conversion;
|
||||
}
|
||||
|
||||
|
|
|
@ -24,7 +24,6 @@ import DBAccess from './DBAccess';
|
|||
import DBVendors from './DBVendors';
|
||||
import DBAccessQueryResult from './DBAccessQueryResult';
|
||||
import * as extraConfigProcessor from './ExtraConfigProcessor';
|
||||
import generateError from './ErrorGenerator';
|
||||
|
||||
/**
|
||||
* Escapes quotes inside given string.
|
||||
|
@ -47,7 +46,6 @@ async function processTableComments(conversion: Conversion, tableName: string):
|
|||
const resultSelectComment: DBAccessQueryResult = await dbAccess.query(logTitle, sqlSelectComment, DBVendors.MYSQL, false, false);
|
||||
|
||||
if (resultSelectComment.error) {
|
||||
generateError(conversion, `\t--[${ logTitle }] ${ resultSelectComment.error }`, sqlSelectComment);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -56,10 +54,6 @@ async function processTableComments(conversion: Conversion, tableName: string):
|
|||
const createCommentResult: DBAccessQueryResult = await dbAccess.query(logTitle, sqlCreateComment, DBVendors.PG, false, false);
|
||||
|
||||
if (createCommentResult.error) {
|
||||
const msg: string = `\t--[${ logTitle }] Error while processing comment for
|
||||
"${ conversion._schema }"."${ tableName }"...\n${ createCommentResult.error }`;
|
||||
|
||||
generateError(conversion, msg, sqlCreateComment);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -85,10 +79,6 @@ async function processColumnsComments(conversion: Conversion, tableName: string)
|
|||
const createCommentResult: DBAccessQueryResult = await dbAccess.query(logTitle, sqlCreateComment, DBVendors.PG, false, false);
|
||||
|
||||
if (createCommentResult.error) {
|
||||
const msg: string = `\t--[${ logTitle }] Error while processing comment for
|
||||
"${ conversion._schema }"."${ tableName }"...\n${ createCommentResult.error }`;
|
||||
|
||||
generateError(conversion, msg, sqlCreateComment);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,82 +0,0 @@
|
|||
/*
|
||||
* This file is a part of "NMIG" - the database migration tool.
|
||||
*
|
||||
* Copyright (C) 2016 - present, Anatoly Khaytovich <anatolyuss@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program (please see the "LICENSE.md" file).
|
||||
* If not, see <http://www.gnu.org/licenses/gpl.txt>.
|
||||
*
|
||||
* @author Anatoly Khaytovich <anatolyuss@gmail.com>
|
||||
*/
|
||||
import { Pool as MySQLPool } from 'mysql';
|
||||
import * as mysql from 'mysql';
|
||||
import { Pool as PgPool } from 'pg';
|
||||
import log from './Logger';
|
||||
import generateError from './ErrorGenerator';
|
||||
import generateReport from './ReportGenerator';
|
||||
import Conversion from './Conversion';
|
||||
|
||||
/**
|
||||
* Check if both servers are connected.
|
||||
* If not, than create connections.
|
||||
* Kill current process if can not connect.
|
||||
*/
|
||||
export default (conversion: Conversion): Promise<Conversion> => {
|
||||
return new Promise(resolve => {
|
||||
const mysqlConnectionPromise: Promise<void> = new Promise((mysqlResolve, mysqlReject) => {
|
||||
if (!conversion._mysql) {
|
||||
conversion._sourceConString.connectionLimit = conversion._maxDbConnectionPoolSize;
|
||||
conversion._sourceConString.multipleStatements = true;
|
||||
const pool: MySQLPool = mysql.createPool(conversion._sourceConString);
|
||||
|
||||
if (pool) {
|
||||
conversion._mysql = pool;
|
||||
mysqlResolve();
|
||||
} else {
|
||||
log(conversion, '\t--[connect] Cannot connect to MySQL server...');
|
||||
mysqlReject();
|
||||
}
|
||||
} else {
|
||||
mysqlResolve();
|
||||
}
|
||||
});
|
||||
|
||||
const pgConnectionPromise: Promise<void> = new Promise((pgResolve, pgReject) => {
|
||||
if (!conversion._pg) {
|
||||
conversion._targetConString.max = conversion._maxDbConnectionPoolSize;
|
||||
const pool: PgPool = new PgPool(conversion._targetConString);
|
||||
|
||||
if (pool) {
|
||||
conversion._pg = pool;
|
||||
|
||||
conversion._pg.on('error', (error: Error) => {
|
||||
const message: string = `Cannot connect to PostgreSQL server...\n' ${ error.message }\n${ error.stack }`;
|
||||
generateError(conversion, message);
|
||||
generateReport(conversion, message);
|
||||
});
|
||||
|
||||
pgResolve();
|
||||
} else {
|
||||
log(conversion, '\t--[connect] Cannot connect to PostgreSQL server...');
|
||||
pgReject();
|
||||
}
|
||||
} else {
|
||||
pgResolve();
|
||||
}
|
||||
});
|
||||
|
||||
Promise.all([mysqlConnectionPromise, pgConnectionPromise])
|
||||
.then(() => resolve(conversion))
|
||||
.catch(() => process.exit());
|
||||
});
|
||||
}
|
|
@ -24,6 +24,7 @@ import DBAccess from './DBAccess';
|
|||
import DBVendors from './DBVendors';
|
||||
import * as extraConfigProcessor from './ExtraConfigProcessor';
|
||||
import { mapDataTypes } from './TableProcessor';
|
||||
import DBAccessQueryResult from './DBAccessQueryResult';
|
||||
|
||||
/**
|
||||
* Defines which columns of the given table have default value.
|
||||
|
@ -63,9 +64,12 @@ export default async function(conversion: Conversion, tableName: string): Promis
|
|||
sql += `${ column.Default };`;
|
||||
}
|
||||
|
||||
await dbAccess.query('DefaultValuesProcessor', sql, DBVendors.PG, false, false);
|
||||
const successMsg: string = `\t--[DefaultValuesProcessor] Set default value for "${ conversion._schema }"."${ tableName }"."${ columnName }"...`;
|
||||
log(conversion, successMsg, conversion._dicTables[tableName].tableLogPath);
|
||||
const result: DBAccessQueryResult = await dbAccess.query('DefaultValuesProcessor', sql, DBVendors.PG, false, false);
|
||||
|
||||
if (!result.error) {
|
||||
const successMsg: string = `\t--[DefaultValuesProcessor] Set default value for "${ conversion._schema }"."${ tableName }"."${ columnName }"...`;
|
||||
log(conversion, successMsg, conversion._dicTables[tableName].tableLogPath);
|
||||
}
|
||||
});
|
||||
|
||||
await Promise.all(promises);
|
||||
|
|
|
@ -23,6 +23,7 @@ import Conversion from './Conversion';
|
|||
import DBAccess from './DBAccess';
|
||||
import DBVendors from './DBVendors';
|
||||
import * as extraConfigProcessor from './ExtraConfigProcessor';
|
||||
import DBAccessQueryResult from './DBAccessQueryResult';
|
||||
|
||||
/**
|
||||
* Defines which columns of the given table are of type "enum".
|
||||
|
@ -47,9 +48,12 @@ export default async function(conversion: Conversion, tableName: string): Promis
|
|||
);
|
||||
|
||||
const sql: string = `ALTER TABLE "${ conversion._schema }"."${ tableName }" ADD CHECK ("${ columnName }" IN (${ arrType[1] });`;
|
||||
await dbAccess.query('EnumProcessor', sql, DBVendors.PG, false, false);
|
||||
const successMsg: string = `\t--[EnumProcessor] Set "ENUM" for "${ conversion._schema }"."${ tableName }"."${ columnName }"...`;
|
||||
log(conversion, successMsg, conversion._dicTables[tableName].tableLogPath);
|
||||
const result: DBAccessQueryResult = await dbAccess.query('EnumProcessor', sql, DBVendors.PG, false, false);
|
||||
|
||||
if (!result.error) {
|
||||
const successMsg: string = `\t--[EnumProcessor] Set "ENUM" for "${ conversion._schema }"."${ tableName }"."${ columnName }"...`;
|
||||
log(conversion, successMsg, conversion._dicTables[tableName].tableLogPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
@ -110,6 +110,11 @@ export default async function(conversion: Conversion): Promise<void> {
|
|||
|
||||
const dbAccess: DBAccess = new DBAccess(conversion);
|
||||
const result: DBAccessQueryResult = await dbAccess.query(logTitle, sql, DBVendors.MYSQL, false, false);
|
||||
|
||||
if (result.error) {
|
||||
return;
|
||||
}
|
||||
|
||||
const extraRows: any[] = extraConfigProcessor.parseForeignKeys(conversion, tableName);
|
||||
const fullRows: any[] = (result.data || []).concat(extraRows); // Prevent failure if "result.data" is undefined.
|
||||
await processForeignKeyWorker(conversion, dbAccess, tableName, fullRows);
|
||||
|
|
|
@ -35,6 +35,10 @@ export default async function(conversion: Conversion, tableName: string): Promis
|
|||
const sqlShowIndex: string = `SHOW INDEX FROM \`${ originalTableName }\`;`;
|
||||
const showIndexResult: DBAccessQueryResult = await dbAccess.query(logTitle, sqlShowIndex, DBVendors.MYSQL, false, false);
|
||||
|
||||
if (showIndexResult.error) {
|
||||
return;
|
||||
}
|
||||
|
||||
const objPgIndices: any = Object.create(null);
|
||||
let cnt: number = 0;
|
||||
let indexType: string = '';
|
||||
|
|
|
@ -30,7 +30,7 @@ import Conversion from './Conversion';
|
|||
export async function get(conversion: Conversion, param: string): Promise<boolean> {
|
||||
const dbAccess: DBAccess = new DBAccess(conversion);
|
||||
const sql: string = `SELECT ${ param } FROM "${ conversion._schema }"."state_logs_${ conversion._schema }${ conversion._mySqlDbName }";`;
|
||||
const result: DBAccessQueryResult = await dbAccess.query('MigrationStateManager::get', sql, DBVendors.PG, false, false);
|
||||
const result: DBAccessQueryResult = await dbAccess.query('MigrationStateManager::get', sql, DBVendors.PG, true, false);
|
||||
return result.data.rows[0][param];
|
||||
}
|
||||
|
||||
|
@ -40,7 +40,7 @@ export async function get(conversion: Conversion, param: string): Promise<boolea
|
|||
export async function set(conversion: Conversion, param: string): Promise<void> {
|
||||
const dbAccess: DBAccess = new DBAccess(conversion);
|
||||
const sql: string = `UPDATE "${ conversion._schema }"."state_logs_${ conversion._schema }${ conversion._mySqlDbName }" SET ${ param } = TRUE;`;
|
||||
await dbAccess.query('MigrationStateManager::set', sql, DBVendors.PG, false, false);
|
||||
await dbAccess.query('MigrationStateManager::set', sql, DBVendors.PG, true, false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -22,6 +22,7 @@ import log from './Logger';
|
|||
import Conversion from './Conversion';
|
||||
import DBAccess from './DBAccess';
|
||||
import DBVendors from './DBVendors';
|
||||
import DBAccessQueryResult from './DBAccessQueryResult';
|
||||
import * as extraConfigProcessor from './ExtraConfigProcessor';
|
||||
|
||||
/**
|
||||
|
@ -38,9 +39,12 @@ export default async function(conversion: Conversion, tableName: string): Promis
|
|||
if (column.Null.toLowerCase() === 'no') {
|
||||
const columnName: string = extraConfigProcessor.getColumnName(conversion, originalTableName, column.Field, false);
|
||||
const sql: string = `ALTER TABLE "${ conversion._schema }"."${ tableName }" ALTER COLUMN "${ columnName }" SET NOT NULL;`;
|
||||
await dbAccess.query('NullConstraintsProcessor', sql, DBVendors.PG, false, false);
|
||||
const successMsg: string = `\t--[NullConstraintsProcessor] Set NOT NULL for "${ conversion._schema }"."${ tableName }"."${ columnName }"...`;
|
||||
log(conversion, successMsg, conversion._dicTables[tableName].tableLogPath);
|
||||
const result: DBAccessQueryResult = await dbAccess.query('NullConstraintsProcessor', sql, DBVendors.PG, false, false);
|
||||
|
||||
if (!result.error) {
|
||||
const successMsg: string = `\t--[NullConstraintsProcessor] Set NOT NULL for "${ conversion._schema }"."${ tableName }"."${ columnName }"...`;
|
||||
log(conversion, successMsg, conversion._dicTables[tableName].tableLogPath);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -37,9 +37,12 @@ export async function setSequenceValue(conversion: Conversion, tableName: string
|
|||
const sql: string = `SELECT SETVAL(\'"${ conversion._schema }"."${ seqName }"\',
|
||||
(SELECT MAX("' + columnName + '") FROM "${ conversion._schema }"."${ tableName }"));`;
|
||||
|
||||
await dbAccess.query('SequencesProcessor::setSequenceValue', sql, DBVendors.PG, false, false);
|
||||
const successMsg: string = `\t--[setSequenceValue] Sequence "${ conversion._schema }"."${ seqName }" is created...`;
|
||||
log(conversion, successMsg, conversion._dicTables[tableName].tableLogPath);
|
||||
const result: DBAccessQueryResult = await dbAccess.query('SequencesProcessor::setSequenceValue', sql, DBVendors.PG, false, false);
|
||||
|
||||
if (!result.error) {
|
||||
const successMsg: string = `\t--[setSequenceValue] Sequence "${ conversion._schema }"."${ seqName }" is created...`;
|
||||
log(conversion, successMsg, conversion._dicTables[tableName].tableLogPath);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue