forked from db-migrate/node-db-migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
118 lines (92 loc) · 2.98 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
var recursive = require('final-fs').readdirRecursive;
var fs = require('fs');
var driver = require('./lib/driver');
var path = require('path');
var log = require('./lib/log');
var Migrator = require('./lib/migrator');
exports.dataType = require('./lib/data_type');
exports.config = require('./lib/config');
exports.connect = function(config, callback) {
driver.connect(config, function(err, db) {
if (err) { callback(err); return; }
if(global.migrationMode)
{
var dirPath = path.resolve(config['migrations-dir'] || 'migrations');
if(global.migrationMode !== 'all')
{
var switched = false,
newConf;
try {
newConf = require(path.resolve(config['migrations-dir'] || 'migrations', global.migrationMode) + '/config.json');
log.info('loaded extra config for migration subfolder: "' + global.migrationMode + '/config.json"');
switched = true;
} catch(e) {}
if(switched) {
db.switchDatabase(newConf, function()
{
global.locTitle = global.migrationMode;
callback(null, new Migrator(db, config['migrations-dir']));
});
}
else
{
global.locTitle = global.migrationMode;
callback(null, new Migrator(db, config['migrations-dir']));
}
}
else
{
recursive(dirPath, false, config['migrations-dir'] || 'migrations')
.then(function(files) {
var oldClose = db.close;
files = files.filter(function (file) {
return file !== 'migrations' && fs.statSync(file).isDirectory();
});
files.push('');
db.close = function(cb) { migrationFiles(files, callback, config, db, oldClose, cb); };
db.close();
});
}
}
else
callback(null, new Migrator(db, config['migrations-dir']));
});
};
exports.driver = function(config, callback) {
driver.connect(config, callback);
};
function migrationFiles(files, callback, config, db, close, cb) {
var file,
switched = false,
newConf;
if(files.length === 1)
{
db.close = close;
}
file = files.pop();
if(file !== '')
{
try {
newConf = require(path.resolve(file + '/config.json'));
log.info('loaded extra config for migration subfolder: "' + file + '/config.json"');
switched = true;
} catch(e) {}
}
db.switchDatabase((switched) ? newConf : config.database, function()
{
global.matching = file.substr(file.indexOf(config['migrations-dir'] || 'migrations') +
(config['migrations-dir'] || 'migrations').length + 1);
if(global.matching.length === 0)
global.matching = '';
global.locTitle = global.matching;
callback(null, new Migrator(db, config['migrations-dir']));
if(typeof(cb) === 'function')
cb();
});
}
exports.createMigration = function(migration, callback) {
migration.write(function(err) {
if (err) { callback(err); return; }
callback(null, migration);
});
};