import DBAccess from './DBAccess' ; import DBAccessQueryResult from './DBAccessQueryResult' ; import IDBAccessQueryParams from './IDBAccessQueryParams' ; import DBVendors from './DBVendors' ; import { log } from './FsOps' ; import Conversion from './Conversion' ; import Table from './Table' ; import { createTable } from './TableProcessor' ; import prepareDataChunks from './DataChunksProcessor' ; import * as migrationStateManager from './MigrationStateManager' ; import * as extraConfigProcessor from './ExtraConfigProcessor' ; /**
 * Processes current table before data loading.
 */ const processTableBeforeDataLoading = async ( conversion , tableName , stateLog ) : Promise < void > => { await createTable ( conversion , tableName ) ; await prepareDataChunks ( conversion , tableName , stateLog ) ; } ; /**
 * Retrieves the source db (MySQL) version.
 */ const getMySqlVersion = async ( conversion ) : Promise < void > => { const params = { conversion : conversion , caller : 'StructureLoader::getMySqlVersion' , sql : 'SELECT VERSION() AS mysql_version;' , vendor : DBVendors . MYSQL , processExitOnError : false , shouldReturnClient : false } ; const result = await DBAccess . query ( params ) ; if ( result . error ) { return ; } const arrVersion = result . data [ 0 ] . mysql_version . split ( '.' ) ; const majorVersion = arrVersion [ 0 ] ; const minorVersion = arrVersion . slice ( 1 ) . join ( '' ) ; conversion . _mysqlVersion = + ( `${ majorVersion }.${ minorVersion }` ) ; } ; /**
 * Loads source tables and views, that need to be migrated.
 */ export default async ( conversion ) : Promise < Conversion > => { const logTitle = 'StructureLoader::default' ; await getMySqlVersion ( conversion ) ; const haveTablesLoaded = await migrationStateManager . get ( conversion , 'tables_loaded' ) ; let sql = `SHOW FULL TABLES IN \`${ conversion . _mySqlDbName }\` WHERE 1 = 1` ; if ( conversion . _includeTables . length !== 0 ) { sql += ` AND Tables_in_${ conversion . _mySqlDbName } IN(${ conversion . _includeTables . map ( ( table ) => `"${ table }"` ) . join ( ',' ) })` ; } if ( conversion . _excludeTables . length !== 0 ) { sql += ` AND Tables_in_${ conversion . _mySqlDbName } NOT IN(${ conversion . _excludeTables . map ( ( table ) => `"${ table }"` ) . join ( ',' ) })` ; } const params = { conversion : conversion , caller : logTitle , sql : `${ sql };` , vendor : DBVendors . MYSQL , processExitOnError : true , shouldReturnClient : false } ; const result = await DBAccess . query ( params ) ; let tablesCnt = 0 ; let viewsCnt = 0 ; const processTablePromises = [ ] ; result . data . forEach ( ( row ) => { let relationName = row [ `Tables_in_${ conversion . _mySqlDbName }` ] ; if ( row . Table_type === 'BASE TABLE' && conversion . _excludeTables . indexOf ( relationName ) === - 1 ) { relationName = extraConfigProcessor . getTableName ( conversion , relationName , false ) ; conversion . _tablesToMigrate . push ( relationName ) ; conversion . _dicTables [ relationName ] = new Table ( `${ conversion . _logsDirPath }/${ relationName }.log` ) ; processTablePromises . push ( processTableBeforeDataLoading ( conversion , relationName , haveTablesLoaded ) ) ; tablesCnt ++ ; } else if ( row . Table_type === 'VIEW' ) { conversion . _viewsToMigrate . push ( relationName ) ; viewsCnt ++ ; } } ) ; const message = `\t--[${ logTitle }] Source DB structure is loaded...\n
        \t--[${ logTitle }] Tables to migrate: ${ tablesCnt }\n
        \t--[${ logTitle }] Views to migrate: ${ viewsCnt }` ; log ( conversion , message ) ; await Promise . all ( processTablePromises ) ; await migrationStateManager . set ( conversion , 'tables_loaded' ) ; return conversion ; } ;