machinomy
Version:
Micropayments powered by Ethereum
32 lines • 1.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const DBMigrate = require("db-migrate");
const files = require("../util/files");
class SqlMigrator {
constructor(log, config) {
this.log = log;
log.debug('migrator config %o', config);
this.migrationsPath = config.cmdOptions['migrations-dir'];
this.dbmigrate = DBMigrate.getInstance(true, config);
}
async isLatest() {
let r = await this.dbmigrate.check();
r && r.length > 0 ? this.log.info('Have migrations to be applied') : this.log.info('Latest migration is applied');
return !(r && r.length > 0);
}
async sync(n) {
let destination = n || await this.lastMigrationNumber();
this.log.info('Syncing migrations till %s', destination);
return this.dbmigrate.sync(destination);
}
async lastMigrationNumber() {
let allFiles = await files.readdir(this.migrationsPath);
let migrations = allFiles.reduce((acc, filename) => {
let match = filename.match(/^(\d+)[\w-]+\.[jt]s$/);
return match ? acc.concat([match[1]]) : acc;
}, []).sort();
return migrations[migrations.length - 1];
}
}
exports.default = SqlMigrator;
//# sourceMappingURL=SqlMigrator.js.map