forked from db-migrate/node-db-migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.js
198 lines (179 loc) · 4.68 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
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
/**
* This file is going to disappear.
* Only still here for backwards compatibility.
* */
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;
const Promise = require('bluebird');
exports.connect = function (config, PassedClass) {
var internals = {};
var prefix = 'migration';
if (config.config) {
prefix = config.prefix || prefix;
internals = config.internals;
config = config.config;
}
return Promise.fromCallback(callback => {
driver.connect(config, internals, function (err, db) {
if (err) {
callback(err);
return;
}
if (internals.migrationMode) {
var dirPath = path.resolve(
internals.argv['migrations-dir'] || 'migrations'
);
if (internals.migrationMode !== 'all') {
var switched = false;
var newConf;
try {
newConf = require(path.resolve(
internals.argv['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 (err) {
if (err) {
return callback(err);
}
internals.locTitle = internals.migrationMode;
callback(
null,
new PassedClass(
db,
internals.argv['migrations-dir'],
internals.mode !== 'static',
internals,
prefix
)
);
});
} else {
internals.locTitle = internals.migrationMode;
callback(
null,
new PassedClass(
db,
internals.argv['migrations-dir'],
internals.mode !== 'static',
internals,
prefix
)
);
}
} else {
recursive(
dirPath,
false,
internals.argv['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,
prefix,
cb
);
};
db.close();
});
}
} else {
callback(
null,
new PassedClass(
db,
internals.argv['migrations-dir'],
internals.mode !== 'static',
internals,
prefix
)
);
}
});
});
};
exports.driver = function (config, callback) {
var internals = {};
if (config.config) {
internals = config.internals;
config = config.config;
}
driver.connect(config, internals, callback);
};
function migrationFiles (
files,
callback,
config,
internals,
PassedClass,
db,
close,
prefix,
cb
) {
var file;
var switched = false;
var 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(internals.argv['migrations-dir'] || 'migrations') +
(internals.argv['migrations-dir'] || 'migrations').length +
1
);
if (internals.matching.length === 0) {
internals.matching = '';
}
internals.locTitle = internals.matching;
callback(
null,
new PassedClass(
db,
internals.argv['migrations-dir'],
internals.mode !== 'static',
internals,
prefix
)
);
if (typeof cb === 'function') {
cb();
}
});
}