@riao/dbal
Version:
116 lines • 5.1 kB
JavaScript
;
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MigrationRunner = void 0;
const fs = require("fs");
const path_1 = require("path");
const ts_import_ts_1 = require("ts-import-ts");
const create_migration_table_1 = require("./create-migration-table");
/**
* Runs migrations
*/
class MigrationRunner {
constructor(db) {
this.db = db;
}
/**
* Run migrations
*
* @param migrations (Optional) Folder to find migrations in. Default is
* db.getMigraitonDirectory()
* @param log (Optional) Log function. Defaults to console.log
* @param direction (Optional) Run migrations up or down?
* @param steps (Optional) Run a certain number of steps?
*/
run(migrations,
/* eslint-disable-next-line no-console */
log = console.log, direction = 'up', steps) {
return __awaiter(this, void 0, void 0, function* () {
if (!migrations) {
migrations = this.db.getMigrationsDirectory();
}
if (steps === -1) {
steps = undefined;
}
if (steps !== undefined && steps < 0) {
throw new Error('Steps must be a positive integer, or -1 for all.');
}
// Create migration table, if not existing
const createMigrationTable = new create_migration_table_1.CreateMigrationTable(this.db);
yield createMigrationTable.up();
// Query migrations that have already run
const repo = this.db.getQueryRepository();
const alreadyRanMigrations = yield repo.find({
columns: ['name'],
table: 'riao_migration',
});
const alreadyRanNames = alreadyRanMigrations.map((migration) => migration.name);
// Get migration files in folder
const migrationsInPath = fs
.readdirSync(migrations)
.filter((fname) => /\.ts$/.test(fname));
if (!migrationsInPath.length) {
log('No migrations found!');
return;
}
// Check which migrations need to run
let migrationsToRun;
if (direction === 'up') {
migrationsToRun = migrationsInPath.filter((file) => !alreadyRanNames.includes(this.getMigrationName(file)));
}
else {
migrationsToRun = alreadyRanNames.reverse();
}
if (!migrationsToRun.length) {
log('All migrations have already run');
return;
}
log(`Discovered ${migrationsToRun.length} ` +
'pending migrations in this direction.');
if (steps !== undefined) {
migrationsToRun = migrationsToRun.slice(0, steps);
}
log(`Running ${migrationsToRun.length} migrations...`);
// Run each migration
for (const migrationFile of migrationsToRun) {
// Get the migration path & name
const path = (0, path_1.join)(migrations, migrationFile);
const name = this.getMigrationName(path);
// Run the migration
log(direction.toLocaleUpperCase() + ' | ', name);
const migrationType = (0, ts_import_ts_1.tsimport)(path);
const migration = new migrationType(this.db);
yield migration[direction]();
// Save the migration record
if (direction === 'up') {
yield repo.insert({
table: 'riao_migration',
records: { name },
});
}
else {
yield repo.delete({
table: 'riao_migration',
where: { name },
});
}
}
log('Rebuilding Schema...');
yield this.db.buildSchema();
log('Migrations Complete!');
});
}
getMigrationName(filename) {
return (0, path_1.basename)(filename.toLowerCase(), (0, path_1.extname)(filename));
}
}
exports.MigrationRunner = MigrationRunner;
//# sourceMappingURL=migration-runner.js.map