import { log } from './FsOps' ; import Conversion from './Conversion' ; import DBAccess from './DBAccess' ; import DBVendors from './DBVendors' ; import DBAccessQueryResult from './DBAccessQueryResult' ; import IDBAccessQueryParams from './IDBAccessQueryParams' ; import * as extraConfigProcessor from './ExtraConfigProcessor' ; /**
 * Returns PostgreSQL index type, that correlates to given MySQL index type.
 */ const getIndexType = ( conversion , indexType ) : string => { return indexType in conversion . _indexTypesMap ? conversion . _indexTypesMap [ indexType ] : 'BTREE' ; } ; /**
 * Creates primary key and indices.
 */ export default async ( conversion , tableName ) : Promise < void > => { const logTitle = 'IndexAndKeyProcessor::default' ; const originalTableName = extraConfigProcessor . getTableName ( conversion , tableName , true ) ; const params = { conversion : conversion , caller : logTitle , sql : `SHOW INDEX FROM \`${ originalTableName }\`;` , vendor : DBVendors . MYSQL , processExitOnError : false , shouldReturnClient : false } ; const showIndexResult = await DBAccess . query ( params ) ; if ( showIndexResult . error ) { return ; } const objPgIndices = Object . create ( null ) ; let cnt = 0 ; let indexType = '' ; showIndexResult . data . forEach ( ( index ) => { const pgColumnName = extraConfigProcessor . getColumnName ( conversion , originalTableName , index . Column_name , false ) ; if ( index . Key_name in objPgIndices ) { objPgIndices [ index . Key_name ] . column_name . push ( `"${ pgColumnName }"` ) ; return ; } objPgIndices [ index . Key_name ] = { is_unique : index . Non_unique === 0 , column_name : [ `"${ pgColumnName }"` ] , index_type : ` USING ${ getIndexType ( conversion , index . Index_type ) }` , } ; } ) ; const addIndexPromises = Object . keys ( objPgIndices ) . map ( async ( index ) => { let sqlAddIndex = '' ; if ( index . toLowerCase ( ) === 'primary' ) { indexType = 'PK' ; sqlAddIndex = `ALTER TABLE "${ conversion . _schema }"."${ tableName }" 
                ADD PRIMARY KEY(${ objPgIndices [ index ] . column_name . join ( ',' ) });` ; } else { const columnName = objPgIndices [ index ] . column_name [ 0 ] . slice ( 1 , - 1 ) + cnt ++ ; indexType = 'index' ; sqlAddIndex = `CREATE ${ ( objPgIndices [ index ] . is_unique ? 'UNIQUE ' : '' ) }INDEX "${ conversion . _schema }_${ tableName }_${ columnName }_idx" 
            ON "${ conversion . _schema }"."${ tableName }" 
            ${ objPgIndices [ index ] . index_type } (${ objPgIndices [ index ] . column_name . join ( ',' ) });` ; } params . vendor = DBVendors . PG ; params . sql = sqlAddIndex ; await DBAccess . query ( params ) ; } ) ; await Promise . all ( addIndexPromises ) ; const successMsg = `\t--[${ logTitle }] "${ conversion . _schema }"."${ tableName }": PK/indices are successfully set...` ; log ( conversion , successMsg , conversion . _dicTables [ tableName ] . tableLogPath ) ; } ;