-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-migrations.js
44 lines (40 loc) · 1.12 KB
/
run-migrations.js
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
'use strict'
const Migration = require('./migration')
module.exports = async function * runMigrations (migrations, _methods) {
for (let migration of migrations) {
let methods = _methods
if (!Array.isArray(methods)) {
methods = [methods]
}
// Run each method in the array
for (let method of methods) {
// Missing direction method
if (typeof migration[method] !== 'function') {
throw new TypeError(`Migration ${migration.title} does not have method ${method}`)
}
try {
// Execute migration
await migration[method]()
// Set state
switch (method) {
case 'up':
migration.state = Migration.STATES.RAN_UP
break
case 'down':
migration.state = Migration.STATES.RAN_DOWN
break
}
} catch (e) {
// Set error state
migration.state = Migration.STATES.ERRORED
migration.error = e
e.migration = migration
throw e
} finally {
// Set timestamp of action
migration.timestamp = new Date()
}
}
yield migration
}
}