/
migrate-mongo.js
executable file
·127 lines (114 loc) · 3.29 KB
/
migrate-mongo.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
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
#! /usr/bin/env node
const program = require("commander");
const _ = require("lodash");
const Table = require("cli-table3");
const migrateMongo = require("../lib/migrate-mongo");
const pkgjson = require("../package.json");
function printMigrated(migrated = []) {
migrated.forEach(migratedItem => {
console.log(`MIGRATED UP: ${migratedItem}`);
});
}
function handleError(err) {
console.error(`ERROR: ${err.message}`, err.stack);
process.exit(1);
}
function printStatusTable(statusItems) {
return migrateMongo.config.read().then(config => {
const useFileHash = config.useFileHash === true;
const table = new Table({ head: useFileHash ? ["Filename", "Hash", "Applied At"] : ["Filename", "Applied At"]});
statusItems.forEach(item => table.push(_.values(item)));
console.log(table.toString());
})
}
program.version(pkgjson.version);
program
.command("init")
.description("initialize a new migration project")
.action(() =>
migrateMongo
.init()
.then(() =>
console.log(
`Initialization successful. Please edit the generated \`${migrateMongo.config.getConfigFilename()}\` file`
)
)
.catch(err => handleError(err))
);
program
.command("create [description]")
.description("create a new database migration with the provided description")
.option("-f --file <file>", "use a custom config file")
.action((description, options) => {
global.options = options;
migrateMongo
.create(description)
.then(fileName =>
migrateMongo.config.read().then(config => {
console.log(`Created: ${config.migrationsDir}/${fileName}`);
})
)
.catch(err => handleError(err));
});
program
.command("up")
.description("run all pending database migrations")
.option("-f --file <file>", "use a custom config file")
.action(options => {
global.options = options;
migrateMongo.database
.connect()
.then(({db, client}) => migrateMongo.up(db, client))
.then(migrated => {
printMigrated(migrated);
})
.then(() => {
process.exit(0);
})
.catch(err => {
handleError(err);
printMigrated(err.migrated);
});
});
program
.command("down")
.description("undo the last applied database migration")
.option("-f --file <file>", "use a custom config file")
.action(options => {
global.options = options;
migrateMongo.database
.connect()
.then(({db, client}) => migrateMongo.down(db, client))
.then(migrated => {
migrated.forEach(migratedItem => {
console.log(`MIGRATED DOWN: ${migratedItem}`);
});
})
.then(() => {
process.exit(0);
})
.catch(err => {
handleError(err);
});
});
program
.command("status")
.description("print the changelog of the database")
.option("-f --file <file>", "use a custom config file")
.action(options => {
global.options = options;
migrateMongo.database
.connect()
.then(({db, client}) => migrateMongo.status(db, client))
.then(statusItems => printStatusTable(statusItems))
.then(() => {
process.exit(0);
})
.catch(err => {
handleError(err);
});
});
program.parse(process.argv);
if (_.isEmpty(program.rawArgs)) {
program.outputHelp();
}