2021-01-22 19:41:11 +02:00
|
|
|
import BaseService from './BaseService';
|
|
|
|
import Migration from '../models/Migration';
|
2019-05-11 18:55:40 +02:00
|
|
|
|
2021-01-22 19:41:11 +02:00
|
|
|
export default class MigrationService extends BaseService {
|
|
|
|
|
|
|
|
private static instance_: MigrationService;
|
2019-05-11 18:55:40 +02:00
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public static instance() {
|
2019-05-11 18:55:40 +02:00
|
|
|
if (this.instance_) return this.instance_;
|
|
|
|
this.instance_ = new MigrationService();
|
|
|
|
return this.instance_;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public async runScript(num: number) {
|
2019-05-12 12:41:07 +02:00
|
|
|
const script = Migration.script(num);
|
|
|
|
await script.exec();
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public async run() {
|
2019-05-11 18:55:40 +02:00
|
|
|
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);
|
2024-03-09 12:33:05 +02:00
|
|
|
await Migration.delete(migration.id, { sourceDescription: 'MigrationService' });
|
2019-05-11 18:55:40 +02:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|