zumito-db
Version:
Multi-driver database abstraction layer with decorator-based models
42 lines (41 loc) • 1.24 kB
JavaScript
export class Migrator {
db;
runner;
constructor(db, runner) {
this.db = db;
this.runner = runner;
}
async latest(migrations) {
const executed = await this.runner.getExecuted();
const pending = migrations.filter(m => !executed.includes(m.name));
const applied = [];
for (const migration of pending) {
await this.runner.runUp(migration);
applied.push(migration.name);
}
return applied;
}
async rollback(migrations) {
const executed = await this.runner.getExecuted();
// Roll back the last executed migration
const toRollback = migrations
.filter(m => executed.includes(m.name))
.reverse();
const rolled = [];
if (toRollback.length > 0) {
await this.runner.runDown(toRollback[0]);
rolled.push(toRollback[0].name);
}
return rolled;
}
async status(migrations) {
const executed = await this.runner.getExecuted();
return migrations.map(m => ({
name: m.name,
executed: executed.includes(m.name),
}));
}
async list(migrations) {
return migrations;
}
}