db-migrate 1.0.0-beta.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +487 -0
- package/LICENSE +22 -0
- package/README.md +100 -0
- package/api.js +463 -0
- package/bin/db-migrate +39 -0
- package/connect.js +198 -0
- package/get.js +67 -0
- package/index.js +85 -0
- package/lib/chain.js +157 -0
- package/lib/class.js +75 -0
- package/lib/commands/check.js +52 -0
- package/lib/commands/create-migration.js +252 -0
- package/lib/commands/db.js +63 -0
- package/lib/commands/down.js +45 -0
- package/lib/commands/dump.js +22 -0
- package/lib/commands/fn/plugin.js +45 -0
- package/lib/commands/generated.js +32 -0
- package/lib/commands/helper/assert.js +15 -0
- package/lib/commands/helper/load-config.js +26 -0
- package/lib/commands/helper/migration-hook.js +4 -0
- package/lib/commands/helper/register-events.js +19 -0
- package/lib/commands/index.js +9 -0
- package/lib/commands/on-complete.js +28 -0
- package/lib/commands/run.js +122 -0
- package/lib/commands/seed.js +48 -0
- package/lib/commands/set-default-argv.js +169 -0
- package/lib/commands/sync.js +52 -0
- package/lib/commands/undo-seed.js +54 -0
- package/lib/commands/up.js +52 -0
- package/lib/config.js +207 -0
- package/lib/driver/index.js +156 -0
- package/lib/driver/shadow.js +74 -0
- package/lib/executors/versioned/v1.js +48 -0
- package/lib/executors/versioned/v2.js +78 -0
- package/lib/file.js +190 -0
- package/lib/interface/migratorInterface.js +127 -0
- package/lib/interface/seederInterface.js +145 -0
- package/lib/learn.js +369 -0
- package/lib/methods/v2/conventions.js +47 -0
- package/lib/methods/v2/migrate.js +5 -0
- package/lib/methods/v2/statetravel.js +86 -0
- package/lib/methods/v2/translatestate.js +117 -0
- package/lib/relation.js +37 -0
- package/lib/state.js +154 -0
- package/lib/template.js +312 -0
- package/lib/templates/v2.js +12 -0
- package/lib/temputils.js +62 -0
- package/lib/walker.js +266 -0
- package/package.json +87 -0
- package/pnpm-lock.yaml +2574 -0
- package/te.js +70 -0
package/api.js
ADDED
|
@@ -0,0 +1,463 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports.version = require('./package.json').version;
|
|
4
|
+
|
|
5
|
+
let load;
|
|
6
|
+
var log = require('db-migrate-shared').log;
|
|
7
|
+
var Promise;
|
|
8
|
+
let onComplete;
|
|
9
|
+
var config = require('rc')('db-migrate');
|
|
10
|
+
|
|
11
|
+
// constant hooks for this file
|
|
12
|
+
var APIHooks = {
|
|
13
|
+
'init:api:addfunction:hook': function (name, fn) {
|
|
14
|
+
this[name] = fn;
|
|
15
|
+
},
|
|
16
|
+
'init:api:accessapi:hook': function (cb) {
|
|
17
|
+
return this;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
function dbmigrate (plugins, isModule, options, callback) {
|
|
22
|
+
if (!options.staticLoader) load = require('./lib/commands');
|
|
23
|
+
else load = require('./lib/commands/generated.js');
|
|
24
|
+
|
|
25
|
+
onComplete = load('on-complete');
|
|
26
|
+
|
|
27
|
+
var dotenv = require('dotenv');
|
|
28
|
+
var setDefaultArgv = load('set-default-argv');
|
|
29
|
+
|
|
30
|
+
this.internals = {
|
|
31
|
+
onComplete: onComplete,
|
|
32
|
+
migrationProtocol: 1,
|
|
33
|
+
load
|
|
34
|
+
};
|
|
35
|
+
if (typeof isModule !== 'function') {
|
|
36
|
+
this.internals.isModule = isModule;
|
|
37
|
+
}
|
|
38
|
+
var internals = this.internals;
|
|
39
|
+
|
|
40
|
+
this.internals.plugins = load('fn/plugin')(plugins);
|
|
41
|
+
|
|
42
|
+
if (typeof callback === 'function') this.internals.onComplete = callback;
|
|
43
|
+
else if (typeof options === 'function') this.internals.onComplete = options;
|
|
44
|
+
|
|
45
|
+
this.internals.dbm = require('./');
|
|
46
|
+
this.dataType = this.internals.dbm.dataType;
|
|
47
|
+
this.version = this.internals.dbm.version;
|
|
48
|
+
|
|
49
|
+
var dotenvConfig = { silent: true };
|
|
50
|
+
if (config.dotenvCustomPath) {
|
|
51
|
+
dotenvConfig.path = config.dotenvCustomPath;
|
|
52
|
+
}
|
|
53
|
+
dotenv.load(dotenvConfig);
|
|
54
|
+
|
|
55
|
+
/* $lab:coverage:off$ */
|
|
56
|
+
if (!options || !options.throwUncatched) load('helper/register-events')();
|
|
57
|
+
/* $lab:coverage:on$ */
|
|
58
|
+
|
|
59
|
+
if (typeof options === 'object') {
|
|
60
|
+
if (typeof options.config === 'string') {
|
|
61
|
+
internals.configFile = options.config;
|
|
62
|
+
} else if (typeof options.config === 'object') {
|
|
63
|
+
internals.configObject = options.config;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (typeof options.env === 'string') internals.currentEnv = options.env;
|
|
67
|
+
|
|
68
|
+
if (typeof options.cwd === 'string') internals.cwd = options.cwd;
|
|
69
|
+
else internals.cwd = process.cwd();
|
|
70
|
+
|
|
71
|
+
if (typeof options.cmdOptions === 'object') {
|
|
72
|
+
internals.cmdOptions = options.cmdOptions;
|
|
73
|
+
}
|
|
74
|
+
} else internals.cwd = process.cwd();
|
|
75
|
+
|
|
76
|
+
if (typeof isModule === 'function') {
|
|
77
|
+
this.internals.onComplete = isModule;
|
|
78
|
+
setDefaultArgv(this.internals);
|
|
79
|
+
} else setDefaultArgv(this.internals, isModule);
|
|
80
|
+
|
|
81
|
+
this.config = load('helper/load-config')(
|
|
82
|
+
require('./lib/config.js'),
|
|
83
|
+
this.internals
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
// delayed loading of bluebird
|
|
87
|
+
Promise = require('bluebird');
|
|
88
|
+
this.internals.migrationOptions = {
|
|
89
|
+
dbmigrate: {
|
|
90
|
+
version: this.internals.dbm.version,
|
|
91
|
+
dataType: this.internals.dbm.dataType
|
|
92
|
+
},
|
|
93
|
+
dryRun: this.internals.dryRun,
|
|
94
|
+
cwd: this.internals.cwd,
|
|
95
|
+
noTransactions: this.internals.notransactions,
|
|
96
|
+
verbose: this.internals.verbose,
|
|
97
|
+
type: this.internals.dbm.dataType,
|
|
98
|
+
log: log,
|
|
99
|
+
ignoreOnInit: this.internals.argv['ignore-on-init'],
|
|
100
|
+
Promise: Promise
|
|
101
|
+
};
|
|
102
|
+
this.internals.safeOptions = this.internals.migrationOptions;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
dbmigrate.prototype = {
|
|
106
|
+
/**
|
|
107
|
+
* Add a global defined variable to db-migrate, to enable access from
|
|
108
|
+
* local migrations without configuring pathes.
|
|
109
|
+
*
|
|
110
|
+
* @return boolean
|
|
111
|
+
*/
|
|
112
|
+
addGlobal: function (library) {
|
|
113
|
+
try {
|
|
114
|
+
require(library);
|
|
115
|
+
} catch (e) {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return true;
|
|
120
|
+
},
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Registers and initializes hooks.
|
|
124
|
+
*
|
|
125
|
+
* @returns Promise
|
|
126
|
+
*/
|
|
127
|
+
registerAPIHook: function (callback) {
|
|
128
|
+
var plugins = this.internals.plugins;
|
|
129
|
+
var self = this;
|
|
130
|
+
|
|
131
|
+
return Promise.resolve(Object.keys(APIHooks))
|
|
132
|
+
.each(function (hook) {
|
|
133
|
+
var plugin = plugins.hook(hook);
|
|
134
|
+
if (!plugin) return;
|
|
135
|
+
|
|
136
|
+
var APIHook = APIHooks[hook].bind(self);
|
|
137
|
+
|
|
138
|
+
return Promise.resolve(plugin)
|
|
139
|
+
.map(function (plugin) {
|
|
140
|
+
return plugin[hook]();
|
|
141
|
+
})
|
|
142
|
+
.each(function (args) {
|
|
143
|
+
return APIHook.apply(self, args);
|
|
144
|
+
});
|
|
145
|
+
})
|
|
146
|
+
.asCallback(callback);
|
|
147
|
+
},
|
|
148
|
+
|
|
149
|
+
_internals: this.internals,
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Add a configuration option to dbmigrate.
|
|
153
|
+
*
|
|
154
|
+
* @return boolean
|
|
155
|
+
*/
|
|
156
|
+
addConfiguration: function (description, args, type) {
|
|
157
|
+
var name = args.shift();
|
|
158
|
+
this.internals.argv.describe(name, description);
|
|
159
|
+
|
|
160
|
+
for (var i = 0; i < args.length; ++i) {
|
|
161
|
+
this.internals.argv.alias(args[i], name);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
switch (type) {
|
|
165
|
+
case 'string':
|
|
166
|
+
this.internals.argv.string(name);
|
|
167
|
+
break;
|
|
168
|
+
|
|
169
|
+
case 'boolean':
|
|
170
|
+
this.internals.argv.boolean(name);
|
|
171
|
+
break;
|
|
172
|
+
|
|
173
|
+
default:
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return true;
|
|
178
|
+
},
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Resets and sets argv to a specified new argv.
|
|
182
|
+
*/
|
|
183
|
+
resetConfiguration: function (argv) {
|
|
184
|
+
this.internals.argv = argv;
|
|
185
|
+
},
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Executes up a given number of migrations or a specific one.
|
|
189
|
+
*
|
|
190
|
+
* Defaults to up all migrations if no count is given.
|
|
191
|
+
*/
|
|
192
|
+
up: function (specification, opts, callback) {
|
|
193
|
+
var executeUp = load('up');
|
|
194
|
+
|
|
195
|
+
if (arguments.length > 0) {
|
|
196
|
+
if (typeof specification === 'string') {
|
|
197
|
+
this.internals.argv.destination = specification;
|
|
198
|
+
} else if (typeof specification === 'number') {
|
|
199
|
+
this.internals.argv.count = specification;
|
|
200
|
+
} else if (typeof specification === 'function') {
|
|
201
|
+
callback = specification;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (typeof opts === 'string') {
|
|
205
|
+
this.internals.migrationMode = opts;
|
|
206
|
+
this.internals.matching = opts;
|
|
207
|
+
} else if (typeof opts === 'function') {
|
|
208
|
+
callback = opts;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return Promise.resolve(executeUp(this.internals, this.config)).nodeify(
|
|
213
|
+
callback
|
|
214
|
+
);
|
|
215
|
+
},
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Executes up a given number of migrations or a specific one.
|
|
219
|
+
*
|
|
220
|
+
* Defaults to up all migrations if no count is given.
|
|
221
|
+
*/
|
|
222
|
+
down: function (specification, opts, callback) {
|
|
223
|
+
var executeDown = load('down');
|
|
224
|
+
|
|
225
|
+
if (arguments.length > 0) {
|
|
226
|
+
if (typeof specification === 'number') {
|
|
227
|
+
this.internals.argv.count = arguments[0];
|
|
228
|
+
} else if (typeof specification === 'string') {
|
|
229
|
+
this.internals.argv.destination = specification;
|
|
230
|
+
this.internals.argv.count = Number.MAX_VALUE;
|
|
231
|
+
} else if (typeof specification === 'function') {
|
|
232
|
+
callback = specification;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (typeof opts === 'string') {
|
|
236
|
+
this.internals.migrationMode = opts;
|
|
237
|
+
this.internals.matching = opts;
|
|
238
|
+
} else if (typeof opts === 'function') {
|
|
239
|
+
callback = opts;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return Promise.resolve(executeDown(this.internals, this.config)).asCallback(
|
|
244
|
+
callback
|
|
245
|
+
);
|
|
246
|
+
},
|
|
247
|
+
|
|
248
|
+
check: function (specification, opts, callback) {
|
|
249
|
+
var executeCheck = load('check');
|
|
250
|
+
|
|
251
|
+
if (arguments.length > 0) {
|
|
252
|
+
if (typeof specification === 'number') {
|
|
253
|
+
this.internals.argv.count = arguments[0];
|
|
254
|
+
} else if (typeof specification === 'function') {
|
|
255
|
+
callback = specification;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (typeof opts === 'string') {
|
|
259
|
+
this.internals.migrationMode = opts;
|
|
260
|
+
this.internals.matching = opts;
|
|
261
|
+
} else if (typeof opts === 'function') {
|
|
262
|
+
callback = opts;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
return Promise.resolve(
|
|
267
|
+
executeCheck(this.internals, this.config)
|
|
268
|
+
).asCallback(callback);
|
|
269
|
+
},
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Executes up a given number of migrations or a specific one.
|
|
273
|
+
*
|
|
274
|
+
* Defaults to up all migrations if no count is given.
|
|
275
|
+
*/
|
|
276
|
+
sync: function (specification, opts, callback) {
|
|
277
|
+
var executeSync = load('sync');
|
|
278
|
+
|
|
279
|
+
if (arguments.length > 0) {
|
|
280
|
+
if (typeof specification === 'string') {
|
|
281
|
+
this.internals.argv.destination = specification;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
if (typeof opts === 'string') {
|
|
285
|
+
this.internals.migrationMode = opts;
|
|
286
|
+
this.internals.matching = opts;
|
|
287
|
+
} else if (typeof opts === 'function') {
|
|
288
|
+
callback = opts;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
return Promise.resolve(executeSync(this.internals, this.config)).asCallback(
|
|
293
|
+
callback
|
|
294
|
+
);
|
|
295
|
+
},
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Executes down for all currently migrated migrations.
|
|
299
|
+
*/
|
|
300
|
+
reset: function (scope, callback) {
|
|
301
|
+
var executeDown = load('down');
|
|
302
|
+
|
|
303
|
+
if (typeof scope === 'string') {
|
|
304
|
+
this.internals.migrationMode = scope;
|
|
305
|
+
this.internals.matching = scope;
|
|
306
|
+
} else if (typeof scope === 'function') {
|
|
307
|
+
callback = scope;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
this.internals.argv.count = Number.MAX_VALUE;
|
|
311
|
+
return Promise.resolve(executeDown(this.internals, this.config)).asCallback(
|
|
312
|
+
callback
|
|
313
|
+
);
|
|
314
|
+
},
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Silence the log output completely.
|
|
318
|
+
*/
|
|
319
|
+
silence: function (isSilent) {
|
|
320
|
+
return log.silence(isSilent);
|
|
321
|
+
},
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Creates a correctly formatted migration
|
|
325
|
+
*/
|
|
326
|
+
create: function (migrationName, scope, callback) {
|
|
327
|
+
var executeCreateMigration = load('create-migration');
|
|
328
|
+
if (typeof scope === 'function') {
|
|
329
|
+
callback = scope;
|
|
330
|
+
} else if (scope) {
|
|
331
|
+
this.internals.migrationMode = scope;
|
|
332
|
+
this.internals.matching = scope;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
this.internals.argv._.unshift(migrationName);
|
|
336
|
+
return Promise.resolve(
|
|
337
|
+
executeCreateMigration(this.internals, this.config)
|
|
338
|
+
).asCallback(callback);
|
|
339
|
+
},
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* Creates a database of the given dbname.
|
|
343
|
+
*/
|
|
344
|
+
createDatabase: function (dbname, callback) {
|
|
345
|
+
var executeDB = load('db');
|
|
346
|
+
this.internals.argv._.push(dbname);
|
|
347
|
+
this.internals.mode = 'create';
|
|
348
|
+
return Promise.resolve(executeDB(this.internals, this.config)).asCallback(
|
|
349
|
+
callback
|
|
350
|
+
);
|
|
351
|
+
},
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Drops a database of the given dbname.
|
|
355
|
+
*/
|
|
356
|
+
dropDatabase: function (dbname, callback) {
|
|
357
|
+
var executeDB = load('db');
|
|
358
|
+
this.internals.argv._.push(dbname);
|
|
359
|
+
this.internals.mode = 'drop';
|
|
360
|
+
return Promise.resolve(executeDB(this.internals, this.config)).asCallback(
|
|
361
|
+
callback
|
|
362
|
+
);
|
|
363
|
+
},
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Sets a config variable to the given value.
|
|
367
|
+
*
|
|
368
|
+
* @return value
|
|
369
|
+
*/
|
|
370
|
+
setConfigParam: function (param, value) {
|
|
371
|
+
return (this.internals.argv[param] = value);
|
|
372
|
+
},
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Sets the callback to the default onComplete
|
|
376
|
+
*/
|
|
377
|
+
setDefaultCallback: function () {
|
|
378
|
+
this.internals.onComplete = onComplete;
|
|
379
|
+
},
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Let's the user customize the callback, which gets called after all
|
|
383
|
+
* migrations have been done.
|
|
384
|
+
*/
|
|
385
|
+
setCustomCallback: function (callback) {
|
|
386
|
+
this.internals.onComplete = callback;
|
|
387
|
+
},
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Seeds either the static or version controlled seeders, controlled by
|
|
391
|
+
* the passed mode.
|
|
392
|
+
*/
|
|
393
|
+
seed: function (mode, scope, callback) {
|
|
394
|
+
var executeSeed = load('seed');
|
|
395
|
+
if (scope) {
|
|
396
|
+
this.internals.migrationMode = scope;
|
|
397
|
+
this.internals.matching = scope;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
this.internals.mode = mode || 'vc';
|
|
401
|
+
return Promise.resolve(executeSeed(this.internals, this.config)).asCallback(
|
|
402
|
+
callback
|
|
403
|
+
);
|
|
404
|
+
},
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* Execute the down function of currently executed seeds.
|
|
408
|
+
*/
|
|
409
|
+
undoSeed: function (specification, scope, callback) {
|
|
410
|
+
var executeUndoSeed = load('undo-seed');
|
|
411
|
+
if (arguments.length > 0) {
|
|
412
|
+
if (typeof specification === 'number') {
|
|
413
|
+
this.internals.argv.count = specification;
|
|
414
|
+
|
|
415
|
+
if (scope) {
|
|
416
|
+
this.internals.migrationMode = scope;
|
|
417
|
+
this.internals.matching = scope;
|
|
418
|
+
}
|
|
419
|
+
} else if (typeof specification === 'string') {
|
|
420
|
+
this.internals.migrationMode = scope;
|
|
421
|
+
this.internals.matching = scope;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
return Promise.resolve(
|
|
426
|
+
executeUndoSeed(this.internals, this.config)
|
|
427
|
+
).asCallback(callback);
|
|
428
|
+
},
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Execute the reset function of currently executed seeds.
|
|
432
|
+
*/
|
|
433
|
+
resetSeed: function (specification, scope, callback) {
|
|
434
|
+
var executeUndoSeed = load('undo-seed');
|
|
435
|
+
if (arguments.length > 0) {
|
|
436
|
+
if (typeof specification === 'number') {
|
|
437
|
+
this.internals.argv.count = specification;
|
|
438
|
+
|
|
439
|
+
if (scope) {
|
|
440
|
+
this.internals.migrationMode = scope;
|
|
441
|
+
this.internals.matching = scope;
|
|
442
|
+
}
|
|
443
|
+
} else if (typeof specification === 'string') {
|
|
444
|
+
this.internals.migrationMode = scope;
|
|
445
|
+
this.internals.matching = scope;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
this.internals.argv.count = Number.MAX_VALUE;
|
|
450
|
+
return Promise.resolve(
|
|
451
|
+
executeUndoSeed(this.internals, this.config)
|
|
452
|
+
).asCallback(callback);
|
|
453
|
+
},
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Executes the default routine.
|
|
457
|
+
*/
|
|
458
|
+
run: function () {
|
|
459
|
+
load('run')(this.internals, this.config);
|
|
460
|
+
}
|
|
461
|
+
};
|
|
462
|
+
|
|
463
|
+
module.exports = dbmigrate;
|
package/bin/db-migrate
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
var resolve = require('resolve');
|
|
4
|
+
var log = require('db-migrate-shared').log;
|
|
5
|
+
|
|
6
|
+
process.title = 'db-migrate';
|
|
7
|
+
if (process.argv.indexOf('--verbose') !== -1 ||
|
|
8
|
+
process.argv.indexOf('-v') !== -1
|
|
9
|
+
) { global.verbose = true; }
|
|
10
|
+
|
|
11
|
+
resolve('db-migrate', {
|
|
12
|
+
|
|
13
|
+
basedir: process.cwd()
|
|
14
|
+
}, function (error, localModule) {
|
|
15
|
+
var DBMigrate, dbmigrate;
|
|
16
|
+
|
|
17
|
+
if (error) {
|
|
18
|
+
DBMigrate = require('../');
|
|
19
|
+
} else {
|
|
20
|
+
DBMigrate = require(localModule);
|
|
21
|
+
log.verbose('Detected and using the projects local version of db-migrate. ' +
|
|
22
|
+
'\'' + localModule + '\'');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (typeof (DBMigrate.getInstance) !== 'function') {
|
|
26
|
+
DBMigrate = require('../');
|
|
27
|
+
|
|
28
|
+
log.warn('Using global instead of local detected version as you have a ' +
|
|
29
|
+
'version older than 0.10.0 in your projects package.json!');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
dbmigrate = DBMigrate.getInstance();
|
|
33
|
+
if (dbmigrate.registerAPIHook) {
|
|
34
|
+
dbmigrate.registerAPIHook()
|
|
35
|
+
.then(function () {
|
|
36
|
+
dbmigrate.run();
|
|
37
|
+
});
|
|
38
|
+
} else { dbmigrate.run(); }
|
|
39
|
+
});
|
package/connect.js
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is going to disappear.
|
|
3
|
+
* Only still here for backwards compatibility.
|
|
4
|
+
* */
|
|
5
|
+
var recursive = require('final-fs').readdirRecursive;
|
|
6
|
+
var fs = require('fs');
|
|
7
|
+
var driver = require('./lib/driver');
|
|
8
|
+
var path = require('path');
|
|
9
|
+
var log = require('db-migrate-shared').log;
|
|
10
|
+
const Promise = require('bluebird');
|
|
11
|
+
|
|
12
|
+
exports.connect = function (config, PassedClass) {
|
|
13
|
+
var internals = {};
|
|
14
|
+
var prefix = 'migration';
|
|
15
|
+
if (config.config) {
|
|
16
|
+
prefix = config.prefix || prefix;
|
|
17
|
+
internals = config.internals;
|
|
18
|
+
config = config.config;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return Promise.fromCallback(callback => {
|
|
22
|
+
driver.connect(config, internals, function (err, db) {
|
|
23
|
+
if (err) {
|
|
24
|
+
callback(err);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
var dirPath = path.resolve(
|
|
29
|
+
internals.argv['migrations-dir'] || 'migrations'
|
|
30
|
+
);
|
|
31
|
+
if (internals.migrationMode) {
|
|
32
|
+
if (internals.migrationMode !== 'all') {
|
|
33
|
+
var switched = false;
|
|
34
|
+
var newConf;
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
newConf = require(path.resolve(
|
|
38
|
+
internals.argv['migrations-dir'] || 'migrations',
|
|
39
|
+
internals.migrationMode
|
|
40
|
+
) + '/config.json');
|
|
41
|
+
log.info(
|
|
42
|
+
'loaded extra config for migration subfolder: "' +
|
|
43
|
+
internals.migrationMode +
|
|
44
|
+
'/config.json"'
|
|
45
|
+
);
|
|
46
|
+
switched = true;
|
|
47
|
+
} catch (e) {}
|
|
48
|
+
|
|
49
|
+
if (switched) {
|
|
50
|
+
db.switchDatabase(newConf, function (err) {
|
|
51
|
+
if (err) {
|
|
52
|
+
return callback(err);
|
|
53
|
+
}
|
|
54
|
+
internals.locTitle = internals.migrationMode;
|
|
55
|
+
callback(
|
|
56
|
+
null,
|
|
57
|
+
new PassedClass(
|
|
58
|
+
db,
|
|
59
|
+
dirPath,
|
|
60
|
+
internals.mode !== 'static',
|
|
61
|
+
internals,
|
|
62
|
+
prefix
|
|
63
|
+
)
|
|
64
|
+
);
|
|
65
|
+
});
|
|
66
|
+
} else {
|
|
67
|
+
internals.locTitle = internals.migrationMode;
|
|
68
|
+
callback(
|
|
69
|
+
null,
|
|
70
|
+
new PassedClass(
|
|
71
|
+
db,
|
|
72
|
+
dirPath,
|
|
73
|
+
internals.mode !== 'static',
|
|
74
|
+
internals,
|
|
75
|
+
prefix
|
|
76
|
+
)
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
} else {
|
|
80
|
+
recursive(
|
|
81
|
+
dirPath,
|
|
82
|
+
false,
|
|
83
|
+
internals.argv['migrations-dir'] || 'migrations'
|
|
84
|
+
).then(function (files) {
|
|
85
|
+
var oldClose = db.close;
|
|
86
|
+
|
|
87
|
+
files = files.filter(function (file) {
|
|
88
|
+
return file !== 'migrations' && fs.statSync(file).isDirectory();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
files.push('');
|
|
92
|
+
|
|
93
|
+
db.close = function (cb) {
|
|
94
|
+
migrationFiles(
|
|
95
|
+
files,
|
|
96
|
+
callback,
|
|
97
|
+
config,
|
|
98
|
+
internals,
|
|
99
|
+
PassedClass,
|
|
100
|
+
db,
|
|
101
|
+
oldClose,
|
|
102
|
+
prefix,
|
|
103
|
+
cb
|
|
104
|
+
);
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
db.close();
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
} else {
|
|
111
|
+
callback(
|
|
112
|
+
null,
|
|
113
|
+
new PassedClass(
|
|
114
|
+
db,
|
|
115
|
+
dirPath,
|
|
116
|
+
internals.mode !== 'static',
|
|
117
|
+
internals,
|
|
118
|
+
prefix
|
|
119
|
+
)
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
exports.driver = function (config, callback) {
|
|
127
|
+
var internals = {};
|
|
128
|
+
if (config.config) {
|
|
129
|
+
internals = config.internals;
|
|
130
|
+
config = config.config;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
driver.connect(config, internals, callback);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
function migrationFiles (
|
|
137
|
+
files,
|
|
138
|
+
callback,
|
|
139
|
+
config,
|
|
140
|
+
internals,
|
|
141
|
+
PassedClass,
|
|
142
|
+
db,
|
|
143
|
+
close,
|
|
144
|
+
prefix,
|
|
145
|
+
cb
|
|
146
|
+
) {
|
|
147
|
+
var file;
|
|
148
|
+
var switched = false;
|
|
149
|
+
var newConf;
|
|
150
|
+
|
|
151
|
+
if (files.length === 1) {
|
|
152
|
+
db.close = close;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
file = files.pop();
|
|
156
|
+
log.info('Enter scope "' + (file !== '' ? file : '/') + '"');
|
|
157
|
+
|
|
158
|
+
if (file !== '') {
|
|
159
|
+
try {
|
|
160
|
+
fs.statSync(path.resolve(file + '/config.json'));
|
|
161
|
+
newConf = require(path.resolve(file + '/config.json'));
|
|
162
|
+
log.info(
|
|
163
|
+
'loaded extra config for migration subfolder: "' +
|
|
164
|
+
file +
|
|
165
|
+
'/config.json"'
|
|
166
|
+
);
|
|
167
|
+
switched = true;
|
|
168
|
+
} catch (e) {}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
db.switchDatabase(switched ? newConf : config.database, function () {
|
|
172
|
+
internals.matching = file.substr(
|
|
173
|
+
file.indexOf(internals.argv['migrations-dir'] || 'migrations') +
|
|
174
|
+
(internals.argv['migrations-dir'] || 'migrations').length +
|
|
175
|
+
1
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
if (internals.matching.length === 0) {
|
|
179
|
+
internals.matching = '';
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
internals.locTitle = internals.matching;
|
|
183
|
+
callback(
|
|
184
|
+
null,
|
|
185
|
+
new PassedClass(
|
|
186
|
+
db,
|
|
187
|
+
internals.argv['migrations-dir'],
|
|
188
|
+
internals.mode !== 'static',
|
|
189
|
+
internals,
|
|
190
|
+
prefix
|
|
191
|
+
)
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
if (typeof cb === 'function') {
|
|
195
|
+
cb();
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
}
|