import { log } from './FsOps' ; import Conversion from './Conversion' ; import DBAccess from './DBAccess' ; import DBAccessQueryResult from './DBAccessQueryResult' ; import IDBAccessQueryParams from './IDBAccessQueryParams' ; import DBVendors from './DBVendors' ; import * as extraConfigProcessor from './ExtraConfigProcessor' ; /**
 * Converts MySQL data types to corresponding PostgreSQL data types.
 * This conversion performs in accordance to mapping rules in './config/data_types_map.json'.
 * './config/data_types_map.json' can be customized.
 */ export const mapDataTypes = ( objDataTypesMap , mySqlDataType ) : string => { let retVal = '' ; const arrDataTypeDetails = mySqlDataType . split ( ' ' ) ; mySqlDataType = arrDataTypeDetails [ 0 ] . toLowerCase ( ) ; const increaseOriginalSize = arrDataTypeDetails . indexOf ( 'unsigned' ) !== - 1 || arrDataTypeDetails . indexOf ( 'zerofill' ) !== - 1 ; if ( mySqlDataType . indexOf ( '(' ) === - 1 ) { retVal = increaseOriginalSize ? objDataTypesMap [ mySqlDataType ] . increased_size : objDataTypesMap [ mySqlDataType ] . type ; } else { const arrDataType = mySqlDataType . split ( '(' ) ; const strDataType = arrDataType [ 0 ] . toLowerCase ( ) ; const strDataTypeDisplayWidth = arrDataType [ 1 ] ; if ( 'enum' === strDataType || 'set' === strDataType ) { retVal = 'character varying(255)' ; } else if ( 'decimal' === strDataType || 'numeric' === strDataType ) { retVal = `${ objDataTypesMap [ strDataType ] . type }(${ strDataTypeDisplayWidth }` ; } else if ( 'decimal(19,2)' === mySqlDataType || objDataTypesMap [ strDataType ] . mySqlVarLenPgSqlFixedLen ) { retVal = increaseOriginalSize ? objDataTypesMap [ strDataType ] . increased_size : objDataTypesMap [ strDataType ] . type ; } else { retVal = increaseOriginalSize ? `${ objDataTypesMap [ strDataType ] . increased_size }(${ strDataTypeDisplayWidth }` : `${ objDataTypesMap [ strDataType ] . type }(${ strDataTypeDisplayWidth }` ; } } if ( retVal === 'character(0)' ) { retVal = 'character(1)' ; } else if ( retVal === 'character varying(0)' ) { retVal = 'character varying(1)' ; } return retVal ; } ; /**
 * Migrates structure of a single table to PostgreSql server.
 */ export const createTable = async ( conversion , tableName ) : Promise < void > => { const logTitle = 'TableProcessor::createTable' ; log ( conversion , `\t--[${ logTitle }] Currently creating table: \`${ tableName }\`` , conversion . _dicTables [ tableName ] . tableLogPath ) ; const originalTableName = extraConfigProcessor . getTableName ( conversion , tableName , true ) ; const params = { conversion : conversion , caller : logTitle , sql : `SHOW FULL COLUMNS FROM \`${ originalTableName }\`;` , vendor : DBVendors . MYSQL , processExitOnError : false , shouldReturnClient : false } ; const columns = await DBAccess . query ( params ) ; if ( columns . error ) { return ; } conversion . _dicTables [ tableName ] . arrTableColumns = columns . data ; if ( conversion . shouldMigrateOnlyData ( ) ) { return ; } const columnsDefinition = columns . data . map ( ( column ) => { const colName = extraConfigProcessor . getColumnName ( conversion , originalTableName , column . Field , false ) ; const colType = mapDataTypes ( conversion . _dataTypesMap , column . Type ) ; return `"${ colName }" ${ colType }` ; } ) . join ( ',' ) ; params . sql = `CREATE TABLE IF NOT EXISTS "${ conversion . _schema }"."${ tableName }"(${ columnsDefinition });` ; params . processExitOnError = true ; params . vendor = DBVendors . PG ; const createTableResult = await DBAccess . query ( params ) ; if ( ! createTableResult . error ) { log ( conversion , `\t--[${ logTitle }] Table "${ conversion . _schema }"."${ tableName }" is created...` , conversion . _dicTables [ tableName ] . tableLogPath ) ; } } ;