@launchql/migrate
Version:
PostgreSQL Migration Tools
68 lines (67 loc) • 3.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.deployFast = void 0;
// FASTER than deploy-stream
// Time: 1.056 s
const path_1 = require("path");
const types_1 = require("@launchql/types");
const server_utils_1 = require("@launchql/server-utils");
const launchql_1 = require("./class/launchql");
const package_1 = require("./package");
const deployFastCache = {};
const getCacheKey = (pg, name, database) => {
const { host, port, user } = pg ?? {};
return `${host ?? 'localhost'}:${port ?? 5432}:${user ?? 'user'}:${database}:${name}`;
};
const deployFast = async (options) => {
const { dir, name, database, opts, usePlan, cache = false } = options;
const log = new server_utils_1.Logger('deploy-fast');
const projectRoot = new launchql_1.LaunchQLProject(dir);
const modules = projectRoot.getModuleMap();
log.info(`🔍 Gathering modules from ${dir}...`);
if (!modules[name]) {
log.error(`❌ Module "${name}" not found.`);
throw new Error(`Module "${name}" does not exist.`);
}
log.info(`📦 Resolving dependencies for ${name}...`);
const extensions = projectRoot.getModuleExtensions();
const pgPool = (0, server_utils_1.getRootPgPool)({ ...opts.pg, database });
log.success(`🚀 Deploying to database: ${database}`);
for (const extension of extensions.resolved) {
try {
if (extensions.external.includes(extension)) {
const query = `CREATE EXTENSION IF NOT EXISTS "${extension}" CASCADE;`;
log.info(`📥 Installing external extension: ${extension}`);
log.debug(`> ${query}`);
await pgPool.query(query);
}
else {
const modulePath = (0, path_1.resolve)(projectRoot.workspacePath, modules[extension].path);
const localProject = new launchql_1.LaunchQLProject(modulePath);
const cacheKey = getCacheKey(opts.pg, extension, database);
if (cache && deployFastCache[cacheKey]) {
log.warn(`⚡ Using cached pkg for ${extension}.`);
await pgPool.query(deployFastCache[cacheKey].sql);
continue;
}
const pkg = (0, package_1.packageModule)(localProject.modulePath, { usePlan, extension: false });
log.info(`📂 Deploying local module: ${extension}`);
log.debug(`→ Path: ${modulePath}`);
log.debug(`→ Command: sqitch deploy db:pg:${database}`);
log.debug(`> ${pkg.sql}`);
await pgPool.query(pkg.sql);
if (cache) {
deployFastCache[cacheKey] = pkg;
}
}
}
catch (err) {
log.error(`🛑 Deployment error: ${err instanceof Error ? err.message : err}`);
console.error(err); // Preserve stack trace
throw types_1.errors.DEPLOYMENT_FAILED({ type: 'Deployment', module: extension });
}
}
log.success(`✅ Deployment complete for module: ${name}`);
return extensions;
};
exports.deployFast = deployFast;