forked from db-migrate/node-db-migrate
/
connect.js
140 lines (108 loc) · 3.57 KB
/
connect.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
var recursive = require('final-fs').readdirRecursive;
var fs = require('fs');
var driver = require('./lib/driver');
var path = require('path');
var log = require('db-migrate-shared').log;
var internals = {};
exports.connect = function(config, passedClass, callback) {
var internals = {};
if( config.config ) {
internals = config.internals;
config = config.config;
}
driver.connect(config, internals, function(err, db) {
if (err) { callback(err); return; }
if(internals.migrationMode)
{
var dirPath = path.resolve(config['migrations-dir'] || 'migrations');
if(internals.migrationMode !== 'all')
{
var switched = false,
newConf;
try {
newConf = require(path.resolve(config['migrations-dir'] || 'migrations', internals.migrationMode) + '/config.json');
log.info('loaded extra config for migration subfolder: "' + internals.migrationMode + '/config.json"');
switched = true;
} catch(e) {}
if(switched) {
db.switchDatabase(newConf, function()
{
internals.locTitle = internals.migrationMode;
callback(null, new passedClass(db, config['migrations-dir'], internals.mode !== 'static', internals));
});
}
else
{
internals.locTitle = internals.migrationMode;
callback(null, new passedClass(db, config['migrations-dir'], internals.mode !== 'static', internals));
}
}
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,
internals, passedClass, db, oldClose, cb); };
db.close();
});
}
}
else
callback(null, new passedClass(db, config['migrations-dir'], internals.mode !== 'static', internals));
});
};
exports.driver = function(config, callback) {
var internals = {};
var _config = config;
if( config.config ) {
internals = config.internals;
config = config.config;
}
driver.connect(config, internals, callback);
};
function migrationFiles(files, callback, config, internals,
passedClass, db, close, cb) {
var file,
switched = false,
newConf;
if(files.length === 1)
{
db.close = close;
}
file = files.pop();
log.info( 'Enter scope "' + ((file !== '') ? file : '/') + '"' );
if(file !== '')
{
try {
fs.statSync(path.resolve(file + '/config.json'));
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()
{
internals.matching = file.substr(file.indexOf(config['migrations-dir'] || 'migrations') +
(config['migrations-dir'] || 'migrations').length + 1);
if(internals.matching.length === 0)
internals.matching = '';
internals.locTitle = internals.matching;
callback(null, new passedClass(db, config['migrations-dir'], internals.mode !== 'static', internals));
if(typeof(cb) === 'function')
cb();
});
}
exports.createMigration = function(migration, callback) {
migration.write(function(err) {
if (err) {
callback(err);
return;
}
callback(null, migration);
});
};