UNPKG

@opengis/fastify-table

Version:

core-plugins

40 lines (31 loc) 1.28 kB
import path from 'node:path'; import { readdirSync, existsSync } from 'node:fs'; import config from '../../../config.js'; import execSql from './exec.sql.js'; import pgClients from '../pg/pgClients.js'; // import getCallerDir from './get.caller.dir.js'; const time = Date.now(); export default async function execMigrations(dirPath, pg = pgClients.client, iscore = false) { if ( !dirPath || (config.migrationsCore === false && iscore) || config.migrations === false || process.env.NODE_ENV === 'unit test' ) { console.log('migrations skip', 'core: ', !!iscore, 'dir: ', dirPath || 'not specified'); return; } console.log('migrations start', dirPath, Date.now() - time); const exists = existsSync(dirPath); if (exists) { // get directory sql file list const content = readdirSync(dirPath, { withFileTypes: true }) ?.filter((el) => el.isFile() && path.extname(el.name) === '.sql') ?.map((el) => el.name) || []; // execute sql files if (content?.length) { await content.reduce((promise, filename) => promise.then(() => execSql(path.join(dirPath, filename), pg)), Promise.resolve()); } } console.log('migrations finish', dirPath, exists, Date.now() - time); }