UNPKG

@golemio/cli

Version:

Collection of executables intended for use with Golemio services and modules

146 lines 7.45 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fast_glob_1 = __importDefault(require("fast-glob")); const fs_1 = __importDefault(require("fs")); const is_glob_1 = __importDefault(require("is-glob")); const path_1 = __importDefault(require("path")); const migrate_db_utils_1 = require("../utils/migrate-db.utils"); const command = { description: "Run database migrations", alias: "mdb", run: async ({ parameters, prompt, print, strings }) => { var _a, _b; let [action, migrationName] = parameters.array; let { postgres: isPostgres, schema: postgresSchema, silent: isSilent, path: migrationsDirPath } = parameters.options; const postgresConfig = { connectionString: (_a = process.env.POSTGRES_CONN) !== null && _a !== void 0 ? _a : "", migrationsDirPath: migrationsDirPath ? Array.isArray(migrationsDirPath) ? migrationsDirPath.map((el) => path_1.default.posix.resolve(el)) : [path_1.default.posix.resolve(migrationsDirPath)] : [path_1.default.posix.resolve((_b = process.env.POSTGRES_MIGRATIONS_DIR) !== null && _b !== void 0 ? _b : "db/migrations/postgresql")], }; if (!isPostgres) { isPostgres = true; } switch (action) { case "help": case "h": default: (0, migrate_db_utils_1.printCommandHelp)(); break; case "create": while (!migrationName) { const { newMigrationName } = await prompt.ask([ { type: "input", name: "newMigrationName", message: "Specify the migration name", }, ]); migrationName = strings.kebabCase(newMigrationName.trim()); } if (isPostgres) { if (postgresConfig.migrationsDirPath.length > 1 || (0, is_glob_1.default)(postgresConfig.migrationsDirPath[0])) { print.error("Multiple paths / globs are not supported for this action"); process.exitCode = 1; return; } const { migrations, createLocalConfig } = (0, migrate_db_utils_1.getPostgresMigrateInstances)({ connectionString: postgresConfig.connectionString, defaultSchema: postgresSchema, migrationDir: postgresConfig.migrationsDirPath[0], }); for (const migration of migrations) { await migration.create(migrationName).then(createLocalConfig); } } break; case "up": case "down": case "reset": if (isPostgres) { if (!postgresConfig.connectionString) { print.error("Environmental variable POSTGRES_CONN is not defined"); process.exitCode = 1; return; } const migrationDirs = []; for (const p of postgresConfig.migrationsDirPath) { // migrationsDirPath is glob, resolve all directories if ((0, is_glob_1.default)(p)) { for (const dir of fast_glob_1.default.sync(p, { onlyDirectories: true, absolute: true, })) { migrationDirs.push(dir); } } // migrationsDirPath is path, check else { if (!fs_1.default.existsSync(p) || !fs_1.default.lstatSync(p).isDirectory()) { print.warning(`Migrations directory ${p} does not exist`); } else { migrationDirs.push(p); } } } if (migrationDirs.length < 1) { print.error("Zero migration directories"); process.exitCode = 1; return; } let results = []; const stdoutWriteStream = process.stdout.write; if (isSilent) { process.stdout.write = () => true; } // Workaround to suppress EventEmitter memory leak warnings (required for db-migrate) // TODO think of a better solution, like spawning multiple processes with child_process.fork if (migrationDirs.length >= process.getMaxListeners()) { process.setMaxListeners(migrationDirs.length + 1); } const defaultModulePgSchemas = await (0, migrate_db_utils_1.getDefaultModulePgSchemas)(migrationDirs); const lockfileData = (0, migrate_db_utils_1.readLockfile)(); const migrateInstances = []; for (const dir of migrationDirs) { const { migrations, schemas } = (0, migrate_db_utils_1.getPostgresMigrateInstances)({ connectionString: postgresConfig.connectionString, migrationDir: dir, defaultModulePgSchemas, defaultSchema: postgresSchema, includeLocalConfig: true, lockfileData, }); for (let i = 0; i < migrations.length; i++) { migrateInstances.push({ migration: migrations[i], migrationDir: dir, schema: schemas[i] }); } } for (const { migration, migrationDir, schema } of migrateInstances) { migration.silence(true); try { await migration[action](); results.push([migrationDir, schema, true]); } catch (err) { console.error(print.colors.red(`Migration error at ${path_1.default.relative(process.cwd(), migrationDir)}: ${err.message}`)); results.push([migrationDir, schema, false, err.message]); process.exitCode = 1; break; } } process.stdout.write = stdoutWriteStream; // Print summary table (0, migrate_db_utils_1.printSummaryMatrix)(results); // db-migrate sometime fails to destroy all connections, this is recommended process.exit(); } } }, }; exports.default = command; //# sourceMappingURL=migrate-db.js.map