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 { PoolClient } from 'pg' ; import * as extraConfigProcessor from './ExtraConfigProcessor' ; /**
 * Sets sequence value.
 */ export const setSequenceValue = async ( conversion , tableName ) : Promise < void > => { const originalTableName = extraConfigProcessor . getTableName ( conversion , tableName , true ) ; const autoIncrementedColumn = conversion . _dicTables [ tableName ] . arrTableColumns . find ( ( column ) => column . Extra === 'auto_increment' ) ; if ( ! autoIncrementedColumn ) { return ; } const logTitle = 'SequenceProcessor::setSequenceValue' ; const columnName = extraConfigProcessor . getColumnName ( conversion , originalTableName , autoIncrementedColumn . Field , false ) ; const seqName = `${ tableName }_${ columnName }_seq` ; const sql = `SELECT SETVAL(\'"${ conversion . _schema }"."${ seqName }"\', 
                (SELECT MAX("${ columnName }") FROM "${ conversion . _schema }"."${ tableName }"));` ; const params = { conversion : conversion , caller : logTitle , sql : sql , vendor : DBVendors . PG , processExitOnError : false , shouldReturnClient : false } ; const result = await DBAccess . query ( params ) ; if ( ! result . error ) { const successMsg = `\t--[${ logTitle }] Sequence "${ conversion . _schema }"."${ seqName }" is created...` ; log ( conversion , successMsg , conversion . _dicTables [ tableName ] . tableLogPath ) ; } } ; /**
 * Defines which column in given table has the "auto_increment" attribute.
 * Creates an appropriate sequence.
 */ export const createSequence = async ( conversion , tableName ) : Promise < void > => { const originalTableName = extraConfigProcessor . getTableName ( conversion , tableName , true ) ; const autoIncrementedColumn = conversion . _dicTables [ tableName ] . arrTableColumns . find ( ( column ) => column . Extra === 'auto_increment' ) ; if ( ! autoIncrementedColumn ) { return ; } const columnName = extraConfigProcessor . getColumnName ( conversion , originalTableName , autoIncrementedColumn . Field , false ) ; const logTitle = 'SequencesProcessor::createSequence' ; const seqName = `${ tableName }_${ columnName }_seq` ; const params = { conversion : conversion , caller : logTitle , sql : `CREATE SEQUENCE "${ conversion . _schema }"."${ seqName }";` , vendor : DBVendors . PG , processExitOnError : false , shouldReturnClient : true } ; const createSequenceResult = await DBAccess . query ( params ) ; if ( createSequenceResult . error ) { await DBAccess . releaseDbClient ( conversion , < PoolClient > createSequenceResult . client ) ; return ; } params . client = createSequenceResult . client ; params . sql = `ALTER TABLE "${ conversion . _schema }"."${ tableName }" ALTER COLUMN "${ columnName }" 
        SET DEFAULT NEXTVAL('${ conversion . _schema }.${ seqName }');` ; const setNextValResult = await DBAccess . query ( params ) ; if ( setNextValResult . error ) { await DBAccess . releaseDbClient ( conversion , < PoolClient > setNextValResult . client ) ; return ; } params . client = setNextValResult . client ; params . sql = `ALTER SEQUENCE "${ conversion . _schema }"."${ seqName }" OWNED BY "${ conversion . _schema }"."${ tableName }"."${ columnName }";` ; const setSequenceOwnerResult = await DBAccess . query ( params ) ; if ( setSequenceOwnerResult . error ) { await DBAccess . releaseDbClient ( conversion , < PoolClient > setSequenceOwnerResult . client ) ; return ; } params . client = setSequenceOwnerResult . client ; params . shouldReturnClient = false ; params . sql = `SELECT SETVAL(\'"${ conversion . _schema }"."${ seqName }"\', (SELECT MAX("${ columnName }") FROM "${ conversion . _schema }"."${ tableName }"));` ; const sqlSetSequenceValueResult = await DBAccess . query ( params ) ; if ( ! sqlSetSequenceValueResult . error ) { const successMsg = `\t--[${ logTitle }] Sequence "${ conversion . _schema }"."${ seqName }" is created...` ; log ( conversion , successMsg , conversion . _dicTables [ tableName ] . tableLogPath ) ; } } ;