@fewer/cli
Version:
The CLI to scaffold and perform operations for Fewer.
108 lines • 4.65 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const getConfig_1 = __importDefault(require("./getConfig"));
const utils_1 = require("./utils");
class MigrationRunner {
constructor(migrations) {
this.migrations = migrations;
this.connectionPool = new Set();
// Include the TS transpiler to allow TS syntax inside of migration files:
require('ts-node/register/transpile-only');
}
async ensureAdapterConnecter(adapter) {
this.connectionPool.add(adapter);
await adapter.connect();
}
async run(direction, migration) {
console.log(`Migrating version ${migration.version} ${direction}...`);
await this.ensureAdapterConnecter(migration.database.adapter);
await migration.run(direction);
}
resolveVersion(version) {
const migrationFile = this.migrations.find(filename => path_1.default.basename(filename).startsWith(version));
if (!migrationFile) {
throw new Error(`No migration found for version "${version}"`);
}
return this.loadMigration(migrationFile);
}
loadMigration(migrationFile) {
const migration = require(migrationFile);
return migration.default || migration;
}
loadDatabase(dbFile) {
const dbModule = require(path_1.default.join(process.cwd(), dbFile));
return dbModule.default || dbModule;
}
async redo(steps = 1) {
const config = await getConfig_1.default();
let dbFile = config.databases[0];
if (config.databases.length > 1) {
dbFile = await utils_1.prompt({
type: 'select',
message: 'Which Database would you like to rollback?',
choices: config.databases,
});
}
const database = this.loadDatabase(dbFile);
await this.ensureAdapterConnecter(database.adapter);
const versions = await database.adapter.migrateGetVersions();
// TODO: We should just have the hook return a sorted list of verisons, rather than having to do the property access here:
const migrationsToRun = versions
.slice(-1 * steps)
.map(version => this.resolveVersion(version));
for (const migration of migrationsToRun) {
await this.run('down', migration);
}
for (const migration of [...migrationsToRun].reverse()) {
await this.run('up', migration);
}
}
async rollback(steps = 1) {
const config = await getConfig_1.default();
let dbFile = config.databases[0];
if (config.databases.length > 1) {
dbFile = await utils_1.prompt({
type: 'select',
message: 'Which Database would you like to rollback?',
choices: config.databases,
});
}
const database = this.loadDatabase(dbFile);
await this.ensureAdapterConnecter(database.adapter);
const versions = await database.adapter.migrateGetVersions();
// TODO: We should just have the hook return a sorted list of verisons, rather than having to do the property access here:
const migrationsToRun = versions
.slice(-1 * steps)
.map(version => this.resolveVersion(version));
for (const migration of migrationsToRun) {
await this.run('down', migration);
}
}
async up(version) {
await this.run('up', this.resolveVersion(version));
}
async down(version) {
await this.run('down', this.resolveVersion(version));
}
async latest() {
const config = await getConfig_1.default();
for (const dbFile of config.databases) {
const database = this.loadDatabase(dbFile);
await this.ensureAdapterConnecter(database.adapter);
const migratedVersions = await database.adapter.migrateGetVersions();
const migrationsToRun = this.migrations.filter(filename => !migratedVersions.some(version => path_1.default.basename(filename).startsWith(version)));
for (const migrationFile of migrationsToRun) {
await this.run('up', this.loadMigration(migrationFile));
}
}
}
async cleanup() {
await Promise.all([...this.connectionPool].map(adapter => adapter.disconnect()));
}
}
exports.default = MigrationRunner;
//# sourceMappingURL=MigrationRunner.js.map