1 |
|
2 |
|
3 |
|
4 | 'use strict';
|
5 |
|
6 | const Base = require('../base/Base');
|
7 |
|
8 | module.exports = class Migrator extends Base {
|
9 |
|
10 | constructor (config) {
|
11 | super({
|
12 |
|
13 | ...config
|
14 | });
|
15 | }
|
16 |
|
17 | getDb (id) {
|
18 | return this.module.getDb(id || this.db);
|
19 | }
|
20 |
|
21 | getPath () {
|
22 | return this.module.getPath(...arguments);
|
23 | }
|
24 |
|
25 | async migrate (action, files) {
|
26 | if (action !== 'apply' && action !== 'revert') {
|
27 | throw new Error(`Migration action (apply/revert) is not set: ${action}`);
|
28 | }
|
29 | if (!Array.isArray(files)) {
|
30 | return this.createMigration(files, action);
|
31 | }
|
32 | for (const file of files) {
|
33 | await this.createMigration(file, action);
|
34 | }
|
35 | }
|
36 |
|
37 | async createMigration (file, action) {
|
38 | this.log('info', `Start to ${action}: ${file}`);
|
39 | const Migration = require(this.getPath(file));
|
40 | const migration = this.spawn(Migration, {migrator: this});
|
41 | await migration[action]();
|
42 | this.log('info', `Done: ${file}`);
|
43 | }
|
44 |
|
45 | log () {
|
46 | CommonHelper.log(this.module, this.constructor.name, ...arguments);
|
47 | }
|
48 | };
|
49 |
|
50 | const CommonHelper = require('../helper/CommonHelper');
|