-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathmigrate.js
More file actions
379 lines (341 loc) · 10.7 KB
/
Copy pathmigrate.js
File metadata and controls
379 lines (341 loc) · 10.7 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
var r,
fs = require('ts-fs-promise'),
path = require('path'),
chalk = require('chalk'),
nconf = require('nconf'),
_ = require('lodash');
var MIGRATION_TABLE_NAME = '_migrations';
var levels = [
'debug',
'info',
'warning',
'error',
'none'
];
var logLevel, autoToArray, requiresWait;
function selectDriver() {
const modules = path.resolve('../node_modules/');
try {
fs.statSync(modules + 'rethinkdbdash');
return 'rethinkdbdash';
} catch(err) {
try {
fs.statSync(modules + 'rethinkdb');
return 'rethinkdb';
} catch(err) {
logError('No rethinkdb driver installed locally.');
logError('Run ' + chalk.cyan('npm install --save rethinkdbdash'));
logError('...or ' + chalk.cyan('npm install --save rethinkdbdash'));
process.exit(1);
}
}
}
function requireRethink(options) {
if(r) { return r; }
var modules = process.cwd() + '/node_modules/';
var driver = options.driver;
if(!driver) {
driver = selectDriver();
}
if(driver === 'rethinkdbdash') {
autoToArray = options.cursor || true;
requiresWait = true;
r = require(modules + 'rethinkdbdash')(options);
} else {
autoToArray = false;
r = require(modules + 'rethinkdb');
}
return r;
}
function toArray(cursor) {
return autoToArray ? cursor : cursor.toArray();
}
// Shim Promise for older versions of Node
if('function' !== typeof global.Promise) {
global.Promise = require('promise');
}
function wait(connection, options) {
if(!requiresWait) { return; }
debug('Waiting for', MIGRATION_TABLE_NAME, 'to be ready for writes');
return r.db(options.db).wait([{waitFor: 'ready_for_writes', timeout: options.timeout}]).run(connection);
}
/*
Read config from
- arguments
- environment variables
- /database.json
*/
function getConfig(root) {
nconf
.argv()
.env()
.file({ file: path.join(root, 'database.json') });
var config = {
host: nconf.get('host'),
port: nconf.get('port'),
db: nconf.get('db'),
discovery: Boolean(nconf.get('discovery')) || false,
timeout: nconf.get('timeout') || (5 * 60),
authKey: nconf.get('authKey'),
ssl: nconf.get('ssl')
};
return Promise.resolve(config);
}
/*
Connect to db
If db does not yet exist, create it
*/
function connectToDb(config) {
r = requireRethink(config);
return r.connect(_.omit(config, ['db']))
.then(function (connection) {
connection.on('close', function () {
debug('Connection closed');
});
return r.dbList().run(connection)
.then(function (dbs) {
if(dbs.indexOf(config.db) === -1) {
logInfo('Creating database', chalk.yellow(config.db));
return r.dbCreate(config.db).run(connection);
}
})
.then(function () {
debug('Use', config.db);
connection.use(config.db);
return wait(connection, config);
})
.then(function () {
return connection;
});
});
}
/*
Check for existence of _migrations table
If needed, create it
*/
function ensureMigrationsTable(connection) {
return r.tableList().run(connection)
.then(function (tables) {
if(tables.indexOf(MIGRATION_TABLE_NAME) === -1) {
debug('Creating', MIGRATION_TABLE_NAME, 'table');
return r.tableCreate(MIGRATION_TABLE_NAME).run(connection)
.then(function () {
debug('Creating', MIGRATION_TABLE_NAME, '"timestamp" index');
return r.table(MIGRATION_TABLE_NAME).indexCreate('timestamp').run(connection);
})
.then(function () {
debug('Waiting for', MIGRATION_TABLE_NAME, '"timestamp" index to resolve');
return r.table(MIGRATION_TABLE_NAME).indexWait().run(connection);
});
}
});
}
/*
Get all migrations stored in _migrations table
*/
function getCompletedMigrations(connection) {
debug('Read completed migrations');
return ensureMigrationsTable(connection)
.then(function () {
return r.table(MIGRATION_TABLE_NAME)
.orderBy({index: 'timestamp'})
.run(connection)
.then(toArray);
});
}
var rxMigrationFile = /^\d{14}-.*\.js$/;
function filterMigrationFiles(file) {
return file.match(rxMigrationFile);
}
/*
Compares completed migrations to files on disk
Returns the migrations scripts with a timestamp newer than last
completed migration in db.
TODO: Change so that all non run migration scripts are returned
*/
function getMigrationsExcept(completedMigration, root) {
debug('List migration files');
var dir = path.join(root, 'migrations');
return fs.readdir(dir)
.then(function (files) {
return files.filter(filterMigrationFiles);
})
.then(function (files) {
return files
.map(function (filename) {
var tsix = filename.indexOf('-');
return {
name: filename.substring(tsix + 1, filename.lastIndexOf('.')),
timestamp: filename.substring(0, tsix),
filename: filename
};
})
.filter(function (migration) {
return !completedMigration || migration.timestamp > completedMigration.timestamp;
});
})
.then(function (migrations) {
return Promise.all(migrations.map(function (migration) {
debug('Requiring migration', migration.timestamp, migration.name);
var filepath = path.join(dir, migration.filename);
return require(filepath);
}))
.then(function (codes) {
return migrations.map(function (migration, ix) {
return _.merge(migration, {code: codes[ix]});
});
});
});
}
/*
Takes a list of migration file paths and requires them
*/
function requireMigrations(migrations, root) {
return migrations.map(function (migration) {
var filename = migration.timestamp + '-' + migration.name + '.js';
var filepath = path.join(root, 'migrations', filename);
debug('Requiring migration', migration.timestamp, migration.name);
return _.merge(migration, {
filename: filename,
code: require(filepath)
});
});
}
/*
Writes all run migrations to db
TODO: update this on every migration run
*/
function updateMigrationTimestamps(migrations, connection) {
return migrations.reduce(function (promise, migration) {
return promise.then(function () {
debug('Writing migration', migration.timestamp, migration.name, 'to', MIGRATION_TABLE_NAME, 'table');
return r.table(MIGRATION_TABLE_NAME).insert(_.omit(migration, ['filename', 'code'])).run(connection);
});
}, Promise.resolve());
}
/*
Run all new up migrations
*/
function migrateUp(params) {
params = params || {};
logLevel = params.logLevel || 'info';
var root = params.root || process.cwd();
var config;
logInfo('Connecting to database');
return getConfig(root)
.then(function (_config) {
config = _config;
debug('config', JSON.stringify(config));
return connectToDb(config);
})
.then(function (connection) {
logInfo('Connected');
return getCompletedMigrations(connection)
.then(function (completedMigrations) {
var latest = completedMigrations && completedMigrations.length && completedMigrations[completedMigrations.length -1];
return getMigrationsExcept(latest, root)
.then(function (migrations) {
if(!migrations.length) {
logInfo('No new migrations');
}
var transaction = migrations.reduce(function (promise, migration) {
return promise
.then(function () {
logInfo(chalk.black.bgGreen(' ↑ up ↑ '), migration.timestamp, chalk.yellow(migration.name));
return migration.code.up(r, connection);
})
.then(function () {
return wait(connection, config);
});
}, Promise.resolve());
return transaction
.then(function () {
return updateMigrationTimestamps(migrations, connection);
});
});
});
})
.then(function () {
logInfo('Migration successful');
})
.catch(function (error) {
logError('Migration failed', error);
});
}
/*
Rollback one or all migrations
*/
function migrateDown(params) {
params = params || {};
logLevel = params.logLevel || 'info';
var root = params.root || process.cwd();
logInfo('Connecting to database');
return getConfig(root)
.then(function (config) {
return connectToDb(config);
})
.then(function (connection) {
logInfo('Connected');
return getCompletedMigrations(connection)
.then(function (completedMigrations) {
if(!completedMigrations || !completedMigrations.length) {
logWarning('No migrations to run');
}
var migrationsToRollBack = (params.all) ? completedMigrations.reverse() : [completedMigrations[completedMigrations.length -1]];
return requireMigrations(migrationsToRollBack, root);
})
.then(function (migrations) {
if(!migrations) { return; }
return migrations.reduce(function (promise, migration) {
return promise
.then(function () {
logInfo(chalk.black.bgYellow(' ↓ down ↓ '), migration.timestamp, chalk.yellow(migration.name));
return migration.code.down(r, connection);
})
.then(function () {
return r.table(MIGRATION_TABLE_NAME).get(migration.id).delete().run(connection);
});
}, Promise.resolve());
});
})
.then(function () {
logInfo('Migration successful');
})
.catch(function (error) {
logError('Migration failed', error);
});
}
function debug() {
if(levels.indexOf(logLevel) > levels.indexOf('debug')) { return; }
var args = Array.prototype.slice.call(arguments);
args.unshift(chalk.gray('[rethink-migrate]'));
console.log(args.join(' '));
}
function logInfo() {
if(levels.indexOf(logLevel) > levels.indexOf('info')) { return; }
var args = Array.prototype.slice.call(arguments);
args.unshift(chalk.blue('[rethink-migrate]'));
console.log(args.join(' '));
}
function logWarning() {
if(levels.indexOf(logLevel) > levels.indexOf('warning')) { return; }
var args = Array.prototype.slice.call(arguments);
args.unshift(chalk.yellow('[rethink-migrate]'));
console.log(args.join(' '));
}
function logError(txt, error) {
if(levels.indexOf(logLevel) > levels.indexOf('error')) { return; }
console.error(chalk.red('[rethink-migrate] ') + txt);
if(error) {
console.error(error);
if(error.stack) {
console.error(error.stack);
}
}
}
module.exports = {
up: migrateUp,
down: migrateDown,
r: requireRethink,
toArray: toArray
};