import * as fs from 'fs' ; import * as path from 'path' ; import { Client } from 'pg' ; import config from '../config' ; import logger from '../utils/logger' ; param databaseConfig export default async function syncDatabaseSchema ( databaseConfig ) { const sqlDirectories = [ 'extensions' , 'schema' , 'functions' , 'triggers' , 'migrations' , ] ; if ( databaseConfig . options . seedDatabase ) { sqlDirectories . push ( 'seed' ) ; } for ( const directory of sqlDirectories ) { const files = await fs . promises . readdir ( path . join ( __dirname , directory ) ) ; for ( const file of files ) { await executeSqlFile ( path . join ( __dirname , directory , file ) , databaseConfig ) ; } } } param file param databaseConfig async function executeSqlFile ( file , databaseConfig ) { const sql = await fs . promises . readFile ( file , 'utf8' ) ; const client = new Client ( databaseConfig . connection ) ; try { await client . connect ( ) ; logger . debug ( `Executing query:\n${ sql }` ) ; await client . query ( sql ) ; await client . end ( ) ; } catch ( err ) { logger . fatal ( '\nerror running query' , file , err . message , '\n\nCheck that Postgres is running and that there is no port conflict\n' ) ; process . exit ( 1 ) ; } }