2020-10-09 19:35:46 +02:00
|
|
|
const BaseService = require('lib/services/BaseService').default;
|
2019-05-11 18:55:40 +02:00
|
|
|
const Migration = require('lib/models/Migration');
|
|
|
|
|
|
|
|
class MigrationService extends BaseService {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
static instance() {
|
|
|
|
if (this.instance_) return this.instance_;
|
|
|
|
this.instance_ = new MigrationService();
|
|
|
|
return this.instance_;
|
|
|
|
}
|
|
|
|
|
2019-05-12 12:41:07 +02:00
|
|
|
async runScript(num) {
|
|
|
|
const script = Migration.script(num);
|
|
|
|
await script.exec();
|
|
|
|
}
|
|
|
|
|
2019-05-11 18:55:40 +02:00
|
|
|
async run() {
|
|
|
|
const migrations = await Migration.migrationsToDo();
|
|
|
|
|
|
|
|
for (const migration of migrations) {
|
2019-09-19 23:51:18 +02:00
|
|
|
this.logger().info(`Running migration: ${migration.number}`);
|
2019-05-11 18:55:40 +02:00
|
|
|
|
|
|
|
try {
|
2019-05-12 12:41:07 +02:00
|
|
|
await this.runScript(migration.number);
|
2019-05-11 18:55:40 +02:00
|
|
|
await Migration.delete(migration.id);
|
|
|
|
} catch (error) {
|
2019-09-19 23:51:18 +02:00
|
|
|
this.logger().error(`Cannot run migration: ${migration.number}`, error);
|
2019-05-11 18:55:40 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
module.exports = MigrationService;
|