import DBAccess from './DBAccess' ; import DBAccessQueryResult from './DBAccessQueryResult' ; import IDBAccessQueryParams from './IDBAccessQueryParams' ; import DBVendors from './DBVendors' ; import { log } from './FsOps' ; import Conversion from './Conversion' ; /**
 * Returns the state logs table name.
 */ export const getStateLogsTableName = ( conversion , getRowName = false ) : string => { const rowName = `state_logs_${ conversion . _schema }${ conversion . _mySqlDbName }` ; return getRowName ? rowName : `"${ conversion . _schema }"."${ rowName }"` ; } ; /**
 * Retrieves state-log.
 */ export const get = async ( conversion , param ) : Promise < boolean > => { const params = { conversion : conversion , caller : 'MigrationStateManager::get' , sql : `SELECT ${ param } FROM ${ getStateLogsTableName ( conversion ) };` , vendor : DBVendors . PG , processExitOnError : true , shouldReturnClient : false } ; const result = await DBAccess . query ( params ) ; return result . data . rows [ 0 ] [ param ] ; } ; /**
 * Updates the state-log.
 */ export const set = async ( conversion , ... states ) : Promise < void > => { const statesSql = states . map ( ( state ) => `${ state } = TRUE` ) . join ( ',' ) ; const params = { conversion : conversion , caller : 'MigrationStateManager::set' , sql : `UPDATE ${ getStateLogsTableName ( conversion ) } SET ${ statesSql };` , vendor : DBVendors . PG , processExitOnError : true , shouldReturnClient : false } ; await DBAccess . query ( params ) ; } ; /**
 * Creates the "{schema}"."state_logs_{self._schema + self._mySqlDbName}" temporary table.
 */ export const createStateLogsTable = async ( conversion ) : Promise < Conversion > => { const logTitle = 'MigrationStateManager::createStateLogsTable' ; const sql = `CREATE TABLE IF NOT EXISTS ${ getStateLogsTableName ( conversion ) }(
        "tables_loaded" BOOLEAN, "per_table_constraints_loaded" BOOLEAN, "foreign_keys_loaded" BOOLEAN, "views_loaded" BOOLEAN);` ; const params = { conversion : conversion , caller : logTitle , sql : sql , vendor : DBVendors . PG , processExitOnError : true , shouldReturnClient : true } ; let result = await DBAccess . query ( params ) ; params . sql = `SELECT COUNT(1) AS cnt FROM ${ getStateLogsTableName ( conversion ) };` ; params . client = result . client ; result = await DBAccess . query ( params ) ; if ( + result . data . rows [ 0 ] . cnt === 0 ) { params . sql = `INSERT INTO ${ getStateLogsTableName ( conversion ) } VALUES (FALSE, FALSE, FALSE, FALSE);` ; params . client = result . client ; params . shouldReturnClient = false ; await DBAccess . query ( params ) ; return conversion ; } const msg = `\t--[${ logTitle }] table ${ getStateLogsTableName ( conversion ) } is created...` ; log ( conversion , msg ) ; return conversion ; } ; /**
 * Drop the "{schema}"."state_logs_{self._schema + self._mySqlDbName}" temporary table.
 */ export const dropStateLogsTable = async ( conversion ) : Promise < Conversion > => { const params = { conversion : conversion , caller : 'MigrationStateManager::dropStateLogsTable' , sql : `DROP TABLE ${ getStateLogsTableName ( conversion ) };` , vendor : DBVendors . PG , processExitOnError : false , shouldReturnClient : false } ; await DBAccess . query ( params ) ; return conversion ; } ;