forked from ngageoint/mage-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.js
56 lines (48 loc) · 1.5 KB
/
migrate.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
45
46
47
48
49
50
51
52
53
54
const
migrations = require('mongodb-migrations'),
log = require('winston'),
path = require('path'),
env = require('./environment/env'),
models = require('./models'),
waitForMongooseConnection = require('./utilities/waitForMongooseConnection'),
migrateConfig = require('./mm-config.js');
class MigrationContext {
constructor() {
this.migrator = new migrations.Migrator(migrateConfig, (level, msg) => {
log.info(msg);
});
}
runFromDir(dir) {
return new Promise(function(resolve, reject) {
this.resolve = resolve;
this.reject = reject;
this.migrator.runFromDir(dir, this.onFinished.bind(this), this.onProgress.bind(this));
}.bind(this));
}
onProgress(id, result) {
if (result.status == 'error') {
log.error(`migration ${id} error: `, result.error);
}
}
onFinished(err, results) {
if (err) {
log.error('database mirgrations failed: ', err);
log.error('migration results:\n' + JSON.stringify(results, null, 2));
process.exit(1);
}
this.resolve();
}
}
module.exports.runDatabaseMigrations = function() {
return waitForMongooseConnection()
.then(function() {
models.initializeModels();
const migrationsDir = path.resolve(migrateConfig.directory);
log.info(`running database migrations in directory ${migrationsDir} ...`);
return new MigrationContext().runFromDir(migrationsDir);
})
.catch(function(err) {
log.error(`error connecting to database:`, err);
process.exit(1);
});
};