This repository has been archived by the owner on Jan 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
194 lines (172 loc) · 4.51 KB
/
index.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
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
183
184
185
186
187
188
189
190
191
192
193
194
/**
* Created by austin on 12/8/15.
*/
"use strict";
var Promise = require("bluebird")
, path = require("path")
, fs = Promise.promisifyAll(require("fs"))
, _ = require("lodash");
var Migration = require("./lib/Migration")
, constants = require("./constants")
, utils = require("./lib/utils");
var _scriptMethods = constants.scriptMethods
, _dumpFormats = constants.dumpFormats;
var argv
, client;
var utils = {
commandline: function (_argv, cb) {
// default the argv values
argv = _argv;
client = _argv.client;
_.defaults(argv, constants.runDefaults);
var cbOrExit = function (err, cb) {
if (cb) {
cb(err);
}
else {
process.exit(err ? 1 : 0);
}
}
, success = function () {
console.log("Migration task complete!");
cbOrExit(null, cb);
}
, fail = function (err) {
console.log("Error in migrate task!", err);
console.log(err.stack);
cbOrExit(err, cb);
};
switch (argv.command) {
case _scriptMethods[0]:
if (argv.schema) {
_loadSchema().then(success).catch(fail);
}
else {
_runMigration(utils.constants.directions.UP).then(success).catch(fail);
}
break;
case _scriptMethods[1]:
_runMigration(utils.constants.directions.DOWN).then(success).catch(fail);
break;
case _scriptMethods[2]:
_dump().then(success).catch(fail);
break;
default :
fail(new Error("Invalid command passed to rdb-migrate/index.js"));
break;
}
},
library: function(config) {
return _dump(config);
}
};
module.exports = utils;
/**
*
* @param direction
* @private
*/
function _runMigration(direction) {
var m = new Migration(client, direction, argv)
, success = function (result) {
console.log("Migration task successful!");
return (!argv.dry ? Promise.delay(500).then(_dump) : null);
}
, fail = function (err) {
console.log("Error in migration task!", err);
console.log(err.stack);
return err;
}
, runMigrationFile = function (file) {
console.log("Compiling migration", file);
require(path.join(__dirname, "migrations", file))(m);
}
, compileMigration = function (files) {
console.log("Compiling migration for %s files", files.length);
return Promise.each(files, runMigrationFile);
}
, runMigration = function () {
console.log("Running migration");
if (!argv.dry) {
return m.run();
}
else {
var dry = m.dryRun();
console.log(dry);
return dry;
}
};
console.log("Starting migration task");
return fs.readdirAsync(path.join(__dirname, "migrations"))
.then(compileMigration)
.then(runMigration)
.then(success)
.catch(fail);
}
/**
*
* @private
*/
function _loadSchema() {
var m = new Migration(client, utils.constants.directions.UP, argv)
, success = function (result) {
return null;
}
, fail = function (err) {
console.log("Error in migrate schema task!", err);
console.log(err.stack);
return err;
};
return fs.readFileAsync(path.resolve(__dirname, argv.out + ".json"))
.then(function (buffer) {
return JSON.parse(buffer.toString());
})
.then(function (j) {
return utils.jsonToMigration(j, m);
})
.then(function (m) {
if (!argv.dry) {
return m.run();
}
else {
var dry = m.dryRun();
console.log(dry);
return dry;
}
})
.then(success)
.catch(fail);
}
/**
*
* @private
*/
function _dump(config) {
var dump = require("./lib/dump")
, success = function (result) {
console.log("Dump task successful");
return null;
}
, fail = function (err) {
console.log("Error in dump task!", err);
console.log(err.stack);
return err;
}
, writeDump = function (result) {
return fs.writeFileAsync(path.resolve(__dirname, './dump/' + argv.out + "." + argv.format), result);
}
, stringify = function (json) {
return JSON.stringify(json, null, 2);
};
if(!!config) {
// this should return a dump object with the correct info
return dump(config);
}
console.log("Starting dump task");
switch (argv.format) {
case _dumpFormats[0]:
return dump.json(client).then(stringify).then(writeDump).then(success).catch(fail);
case _dumpFormats[1]:
return dump.sql(client).then(writeDump).then(success).catch(fail);
}
}