-
-
Notifications
You must be signed in to change notification settings - Fork 527
Expand file tree
/
Copy pathmigrate.js
More file actions
182 lines (168 loc) · 5.17 KB
/
migrate.js
File metadata and controls
182 lines (168 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import process from 'process';
import { _baseOptions } from '../core/yargs';
import {
getMigrator,
ensureCurrentMetaSchema,
addTimestampsToSchema,
} from '../core/migrator';
import helpers from '../helpers';
import _ from 'lodash';
exports.builder = (yargs) =>
_baseOptions(yargs)
.option('to', {
describe: 'Migration name to run migrations until',
type: 'string',
})
.option('from', {
describe: 'Migration name to start migrations from (excluding)',
type: 'string',
})
.option('name', {
describe:
'Migration name. When specified, only this migration will be run. Mutually exclusive with --to and --from',
type: 'string',
conflicts: ['to', 'from'],
})
.option('skip-execution', {
describe: 'Mark migration as completed without actually executing it',
default: false,
type: 'boolean',
}).argv;
exports.handler = async function (args) {
const command = args._[0];
// legacy, gulp used to do this
await helpers.config.init();
switch (command) {
case 'db:migrate':
await migrate(args);
break;
case 'db:migrate:schema:timestamps:add':
await migrateSchemaTimestampAdd(args);
break;
case 'db:migrate:status':
await migrationStatus(args);
break;
}
process.exit(0);
};
function migrate(args) {
return getMigrator('migration', args)
.then((migrator) => {
return ensureCurrentMetaSchema(migrator)
.then(() => migrator.pending())
.then((migrations) => {
const options = {};
if (migrations.length === 0) {
helpers.view.log(
'No migrations were executed, database schema was already up to date.'
);
process.exit(0);
}
if (args.to) {
if (
migrations.filter((migration) => migration.file === args.to)
.length === 0
) {
helpers.view.log(
'No migrations were executed, database schema was already up to date.'
);
process.exit(0);
}
options.to = args.to;
}
if (args.from) {
if (
migrations
.map((migration) => migration.file)
.lastIndexOf(args.from) === -1
) {
helpers.view.log(
'No migrations were executed, database schema was already up to date.'
);
process.exit(0);
}
options.from = args.from;
}
if (args['skip-execution']) {
let migrationsToRun = migrations;
if (args.name) {
migrationsToRun = [args.name];
} else {
if (options.from) {
const fromIndex = migrations.findIndex(
(m) => m.file === options.from
);
if (fromIndex !== -1) {
migrationsToRun = migrations.slice(fromIndex + 1);
}
}
if (options.to) {
const toIndex = migrationsToRun.findIndex(
(m) => m.file === options.to
);
if (toIndex !== -1) {
migrationsToRun = migrationsToRun.slice(0, toIndex + 1);
}
}
}
if (migrationsToRun.length === 0) {
helpers.view.log('No migrations to skip.');
process.exit(0);
}
return Promise.all(
migrationsToRun.map((migration) => {
helpers.view.log(
'Marking migration as executed:',
migration.file
);
return migrator.storage.logMigration(migration.file);
})
).then(() => {
return { executed: true };
});
}
return options;
})
.then((options) => {
if (options && options.executed) return;
if (args.name) {
return migrator.up(args.name);
} else {
return migrator.up(options);
}
});
})
.catch((e) => helpers.view.error(e));
}
function migrationStatus(args) {
return getMigrator('migration', args)
.then((migrator) => {
return ensureCurrentMetaSchema(migrator)
.then(() => migrator.executed())
.then((migrations) => {
_.forEach(migrations, (migration) => {
helpers.view.log('up', migration.file);
});
})
.then(() => migrator.pending())
.then((migrations) => {
_.forEach(migrations, (migration) => {
helpers.view.log('down', migration.file);
});
});
})
.catch((e) => helpers.view.error(e));
}
function migrateSchemaTimestampAdd(args) {
return getMigrator('migration', args)
.then((migrator) => {
return addTimestampsToSchema(migrator).then((items) => {
if (items) {
helpers.view.log('Successfully added timestamps to MetaTable.');
} else {
helpers.view.log('MetaTable already has timestamps.');
}
});
})
.catch((e) => helpers.view.error(e));
}