UNPKG

@launchql/migrate

Version:
57 lines (56 loc) 2.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.resolveWithPlan = exports.resolve = void 0; const fs_1 = require("fs"); const deps_1 = require("./deps"); const extensions_1 = require("./extensions"); /** * Resolves SQL scripts for deployment or reversion. * * @param pkgDir - The package directory (defaults to the current working directory). * @param scriptType - The type of script to resolve (`deploy` or `revert`). * @returns A single concatenated SQL script as a string. */ const resolve = (pkgDir = process.cwd(), scriptType = 'deploy') => { const sqlfile = []; const name = (0, extensions_1.getExtensionName)(pkgDir); const { resolved, external } = (0, deps_1.getDeps)(pkgDir, name); const scripts = scriptType === 'revert' ? [...resolved].reverse() : resolved; for (const script of scripts) { if (external.includes(script)) continue; const file = `${pkgDir}/${scriptType}/${script}.sql`; const dscript = (0, fs_1.readFileSync)(file, 'utf-8'); sqlfile.push(dscript); } return sqlfile.join('\n'); }; exports.resolve = resolve; /** * Resolves SQL scripts based on the `sqitch.plan` file. * * @param pkgDir - The package directory (defaults to the current working directory). * @param scriptType - The type of script to resolve (`deploy` or `revert`). * @returns A single concatenated SQL script as a string. */ const resolveWithPlan = (pkgDir = process.cwd(), scriptType = 'deploy') => { const sqlfile = []; const plan = (0, fs_1.readFileSync)(`${pkgDir}/sqitch.plan`, 'utf-8'); let resolved = plan .split('\n') .filter((line) => line.trim().length > 0 && // Exclude empty lines !line.trim().startsWith('%') && // Exclude initial project settings !line.trim().startsWith('@') // Exclude tags ) .map((line) => line.split(' ')[0]); if (scriptType === 'revert') { resolved = resolved.reverse(); } for (const script of resolved) { const file = `${pkgDir}/${scriptType}/${script}.sql`; const dscript = (0, fs_1.readFileSync)(file, 'utf-8'); sqlfile.push(dscript); } return sqlfile.join('\n'); }; exports.resolveWithPlan = resolveWithPlan;