UNPKG

dynamit-cli

Version:
87 lines (86 loc) 3.54 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const yargs_1 = require("../core/yargs"); const logger_1 = __importDefault(require("../helpers/logger")); exports.default = { builder: yargs => yargs_1.baseOptions(yargs) .option('to', { describe: 'Migration name to run migrations until', type: 'string' }) .option('from', { describe: 'Migration name to start migrations from (excluding)', type: 'string' }), handler: yargs_1.baseHandler((args, migrator) => __awaiter(void 0, void 0, void 0, function* () { const command = args._[0]; switch (command) { case 'migrate': yield migrate(args, migrator); break; case 'migrate:status': yield migrationStatus(migrator); break; } process.exit(0); })) }; function migrate(args, migrator) { return __awaiter(this, void 0, void 0, function* () { try { const migrations = yield migrator.pending(); const options = {}; if (migrations.length === 0) { logger_1.default.log('No migrations were executed, database schema was already up to date.'); process.exit(0); } if (args.to) { if (migrations.filter(migration => migration.file === args.to).length === 0) { logger_1.default.log('No migrations were executed, database schema was already up to date.'); process.exit(0); } options.to = args.to; } if (args.from) { if (migrations.map(migration => migration.file).lastIndexOf(args.from) === -1) { logger_1.default.log('No migrations were executed, database schema was already up to date.'); process.exit(0); } options.from = args.from; } yield migrator.up(options); } catch (e) { logger_1.default.error(e); } }); } function migrationStatus(migrator) { return __awaiter(this, void 0, void 0, function* () { try { const executedMigrations = yield migrator.executed(); executedMigrations.forEach(migration => { logger_1.default.log('up', migration.file); }); const pendingMigrations = yield migrator.pending(); pendingMigrations.forEach(migration => { logger_1.default.log('down', migration.file); }); } catch (e) { logger_1.default.error(e); } }); }