import * as path from 'path' ; import Conversion from './Conversion' ; import DBAccess from './DBAccess' ; import DBAccessQueryResult from './DBAccessQueryResult' ; import DBVendors from './DBVendors' ; import IDBAccessQueryParams from './IDBAccessQueryParams' ; import IConfAndLogsPaths from './IConfAndLogsPaths' ; import { getStateLogsTableName } from './MigrationStateManager' ; import { generateError , log } from './FsOps' ; /**
 * Checks correctness of connection details of both MySQL and PostgreSQL.
 */ export const checkConnection = async ( conversion ) : Promise < string > => { let resultMessage = '' ; const params = { conversion : conversion , caller : 'BootProcessor::checkConnection' , sql : 'SELECT 1;' , vendor : DBVendors . MYSQL , processExitOnError : false , shouldReturnClient : false } ; const mySqlResult = await DBAccess . query ( params ) ; resultMessage += mySqlResult . error ? `\tMySQL connection error: ${ JSON . stringify ( mySqlResult . error ) }\n` : '' ; params . vendor = DBVendors . PG ; const pgResult = await DBAccess . query ( params ) ; resultMessage += pgResult . error ? `\tPostgreSQL connection error: ${ JSON . stringify ( pgResult . error ) }` : '' ; return resultMessage ; } ; /**
 * Returns Nmig's logo.
 */ export const getLogo = ( ) : string => { return '\n\t/\\_  |\\  /\\/\\ /\\___' + '\n\t|  \\ | |\\ | | | __' + '\n\t| |\\\\| || | | | \\_ \\' + '\n\t| | \\| || | | |__/ |' + '\n\t\\|   \\/ /_|/______/' + '\n\n\tNMIG - the database migration tool' + '\n\tCopyright (C) 2016 - present, Anatoly Khaytovich <anatolyuss@gmail.com>\n\n' + '\t--[boot] Configuration has been just loaded.' ; } ; /**
 * Boots the migration.
 */ export const boot = async ( conversion ) : Promise < Conversion > => { const connectionErrorMessage = await checkConnection ( conversion ) ; const logo = getLogo ( ) ; const logTitle = 'BootProcessor::boot' ; if ( connectionErrorMessage ) { await generateError ( conversion , `\t--[${ logTitle }]\n ${ logo } \n ${ connectionErrorMessage }` ) ; process . exit ( 1 ) ; } const sql = `SELECT EXISTS(SELECT 1 FROM information_schema.tables` + ` WHERE table_schema = '${ conversion . _schema }' AND table_name = '${ getStateLogsTableName ( conversion , true ) }');` ; const params = { conversion : conversion , caller : 'BootProcessor::boot' , sql : sql , vendor : DBVendors . PG , processExitOnError : true , shouldReturnClient : false } ; const result = await DBAccess . query ( params ) ; const isExists = ! ! result . data . rows [ 0 ] . exists ; const message = `${ ( isExists ? '\n\t--[boot] NMIG is restarting after some failure.\n\t--[boot] Consider checking log files at the end of migration.\n' : '\n\t--[boot] NMIG is starting.' ) } \n` ; log ( conversion , `\t--[${ logTitle }] ${ logo }${ message }` ) ; return conversion ; } ; /**
 * Parses CLI input arguments, if given.
 * Returns an object containing paths to configuration files and to logs directory.
 *
 * Sample:
 * npm start -- --conf-dir='C:\Users\anatolyuss\Documents\projects\nmig_config' --logs-dir='C:\Users\anatolyuss\Documents\projects\nmig_logs'
 * npm test -- --conf-dir='C:\Users\anatolyuss\Documents\projects\nmig_config' --logs-dir='C:\Users\anatolyuss\Documents\projects\nmig_logs'
 */ export const getConfAndLogsPaths = ( ) : IConfAndLogsPaths => { const baseDir = path . join ( __dirname , '..' , '..' ) ; const _parseInputArguments = ( paramName ) : string | undefined => { const _path = process . argv . find ( ( arg ) => arg . startsWith ( paramName ) ) ; return _path ? _path . split ( '=' ) [ 1 ] : undefined ; } ; return { confPath : _parseInputArguments ( '--conf-dir' ) || path . join ( baseDir , 'config' ) , logsPath : _parseInputArguments ( '--logs-dir' ) || baseDir } ; } ;