-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
498 lines (421 loc) · 15.4 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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
const file_system = require('fs');
const md5_sum = require('md5-file');
/**
* entry point to start migration util
*
* @param mysql_connection {Connection} - to work with database
* @param migrations_folder {string} - path to migrations folder
*/
module.exports.migrate = function(mysql_connection, migrations_folder) {
if (!migrations_folder) {
throw new Error('migrations folder are required');
}
let command = parse_execute_params();
if (!command) {
return;
}
if (command === 'migrate') {
migrate(mysql_connection, migrations_folder);
} else if (command === 'clean') {
clean(mysql_connection);
} else if (command === 'init') {
init(mysql_connection);
}
};
/**
* create empty migration schema table
*
* @param mysql_connection {Connection} - to work with database
*/
function init(mysql_connection) {
"use strict";
let query = `CREATE TABLE \`migration_schema\` (
\`version\` INT PRIMARY KEY,
\`name\` TEXT NOT NULL,
\`hash_sum\` VARCHAR(50) NOT NULL,
\`date\` DATETIME DEFAULT CURRENT_TIMESTAMP) ENGINE = InnoDB`;
mysql_connection.query(query, function (err) {
if (err) {
error(err.message);
mysql_connection.end();
} else {
info('created empty migration schema');
mysql_connection.end();
}
});
}
/**
* drop all tables in database
*
* @param mysql_connection {Connection} - to work with database
*/
function clean(mysql_connection) {
"use strict";
mysql_connection.beginTransaction(function(err){
if (err) {
error(err.message);
end();
} else {
mysql_connection.query(`SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='${mysql_connection.config.database}'`, function (err, result) {
if (err) {
error(err.message);
end();
} else {
let table_names = [];
result.forEach(function (row) {
table_names.push(row.TABLE_NAME);
});
if (table_names.length > 0) {
mysql_connection.query('SET FOREIGN_KEY_CHECKS = 0', function (err) {
if (err) {
mysql_connection.rollback();
error(err.message);
end();
} else {
mysql_connection.query(`DROP TABLE ${table_names.join(',')}`, function (err) {
if (err) {
mysql_connection.rollback();
error(err.message);
end();
} else {
mysql_connection.query('SET FOREIGN_KEY_CHECKS = 0', function (err) {
if (err) {
mysql_connection.rollback();
error(err.message);
end();
} else {
info('database is empty now');
end();
}
});
}
});
}
});
} else {
warning('database already empty');
end();
}
}
});
}
});
function end() {
mysql_connection.end();
}
}
/**
* this function is an entry point to execute new migrations
* if new are exists
*
* @param mysql_connection {Connection} - to work with database
* @param migrations_folder {string} - path to migrations folder
*/
function migrate(mysql_connection, migrations_folder) {
"use strict";
file_system.readdir(migrations_folder, (err, files) => {
if (files) {
if (files.length < 1) {
warning('migration folder [' + migrations_folder + '] does not contains any migration');
} else {
info('found [' + files.length + '] migrations');
let migrations = [];
for (let i = 0; i < files.length; ++i) {
try {
let result = parse_file(files[i], migrations_folder + '/' + files[i]);
migrations.push(result);
} catch (e) {
error(e.message);
return;
}
}
precess_migrations(mysql_connection, migrations);
}
} else {
error(err.message);
mysql_connection.end();
}
});
}
/**
* this function executes new migrations
* if new are exists
*
* @param mysql_connection {Connection} - to work with database
* @param migrations {object[]} - all existed migrations data
*/
function precess_migrations(mysql_connection, migrations) {
"use strict";
migrations.sort(function(a, b){
if (a.version > b.version) {
return 1;
}
if (a.version < b.version) {
return -1;
}
return 0;
});
check_old_migrations_checksum(mysql_connection, migrations, once(function(applied_result){
if (!applied_result) {
info('all existed migrations successfully applied to database');
} else {
let type = applied_result.type,
version = applied_result.version;
if (type) {
if (type === 'SCRIPT_CHANGED') {
error('script version[' + version + '] is change since last migration');
} else {
error('can not apply migration version[' + version + '] since there is en unexpected error');
}
mysql_connection.end();
} else {
info('new not applied migration is [' + version + ']');
for (let i = 0; i < migrations.length; ++i) {
if (migrations[i].version === version) {
migrations = migrations.splice(i);
break;
}
}
if (migrations.length > 0) {
try {
let promise = apply_migration(mysql_connection, migrations[0], file_system.readFileSync(migrations[0].absolute_path, "utf8"))
.catch(function (version) {
console.log(version);
});
for (let i = 1; i < migrations.length; ++i) {
promise =
promise.then(function () {
return apply_migration(mysql_connection, migrations[i], file_system.readFileSync(migrations[i].absolute_path, "utf8"));
})
.catch(function (version) {
console.log(version);
});
}
promise
.then(function () {
info('all migrations successfully applied to database');
mysql_connection.end();
})
.catch(function (version) {
console.log(version);
mysql_connection.end();
});
} catch (e) {
console.log(e);
}
}
}
}
}));
}
function apply_migration(mysql_connection, migration, content) {
"use strict";
function rollback_end() {
mysql_connection.rollback();
mysql_connection.end();
}
return new Promise(function(global_resolve){
new Promise(function(resolve){
mysql_connection.beginTransaction(function(err){
if (err) {
rollback_end();
error('can not start transaction. reason [' + err.message + ']');
} else {
resolve();
}
});
}).then(function(){
new Promise(function(resolve){
mysql_connection.query(content, function (err) {
if (err) {
rollback_end();
error('can not apply migration[' + migration.version + ']');
error('can not execute query. reason [' + err.message + ']');
} else {
resolve();
}
});
}).then(function(){
new Promise(function(resolve){
let to_insert = {
version: migration.version,
hash_sum: migration.hash_sum,
name: migration.name
};
mysql_connection.query('INSERT INTO migration_schema SET ?', to_insert, function(err){
if (err) {
rollback_end();
error('can not apply migration[' + migration.version + ']');
error('can not execute query. reason [' + err.message + ']');
} else {
resolve();
}
});
}).then(function(){
new Promise(function(resolve){
mysql_connection.commit(function(err) {
if (err) {
rollback_end();
error('can not apply migration[' + migration.version + ']');
error('can not commit transaction. reason [' + err.message + ']');
} else {
info('migration [' + migration.version + '][' + migration.name + '] successfully applied');
resolve();
}
});
}).then(function(){
global_resolve();
});
});
});
});
});
}
/**
* checks all existed migrations in database if they does not changed
*
* @param mysql_connection {Connection} - to work with database
* @param migrations {object[]} - all existed migrations data
* @param callback {function} - callback on check end. accept one parameter: not applied migration version or null if all is applied
*/
function check_old_migrations_checksum(mysql_connection, migrations, callback) {
"use strict";
let promise = sync_check_migration(mysql_connection, migrations[0])
.catch(function (version) {
callback(version);
});
for (let i = 1; i < migrations.length; ++i) {
promise =
promise.then(function () {
return sync_check_migration(mysql_connection, migrations[i]);
})
.catch(function (version) {
callback(version);
});
}
promise
.then(function () {
callback(null);
})
.catch(function (version) {
callback(version);
});
}
/**
* make sync promise-chain base call to database to check last applied migration
*
* @param mysql_connection {Connection} - to work with database
* @param migration {object} - migration to check
* @return {Promise} - contains the result if {migration} is successfully applied
*/
function sync_check_migration(mysql_connection, migration) {
"use strict";
return new Promise(function(resolve, reject){
let migration_inside = migration;
mysql_connection.query('SELECT hash_sum FROM migration_schema WHERE version=' + migration_inside.version, function(err, result){
if (err || !result || result.length < 1) {
reject({
version: migration_inside.version
});
} else {
let row = result[0];
if (row.hash_sum !== migration.hash_sum) {
reject({
version: migration_inside.version,
type: 'SCRIPT_CHANGED'
});
} else {
resolve();
}
}
});
});
}
/**
* function the will be executed only for once
*
* @param fn - function to be executed only once
* @param context
* @return {Function}
*/
function once(fn, context) {
let result;
return function() {
if(fn) {
result = fn.apply(context || this, arguments);
fn = null;
}
return result;
};
}
/**
* parse file name {version, name, hash_sum}
*
* @param file_name {string} - file name
* @param full_path_to_file {string} - absolute file path
*/
function parse_file(file_name, full_path_to_file) {
"use strict";
let matches = /V(\d+)__([\w\_]+)\.sql/g.exec(file_name);
if (!matches || matches.index < 0) {
throw new Error(`file ['${file_name}'] has an invalid file name template\nSee help for more information`);
}
return {
version: parseInt(matches[1]),
name: matches[2].replace(/_/g, ' '),
hash_sum: md5_sum.sync(full_path_to_file),
absolute_path: full_path_to_file
}
}
/**
* parse execution arguments and return command name
*
* @return {string|null} - command name
*/
function parse_execute_params() {
"use strict";
let args = process.argv.splice(2);
if (args.length < 1) {
// required at least one params
// otherwise print "help"
help_message();
return null;
}
let command = args[0];
if (command === 'help') {
help_message();
return null;
}
return command;
}
/**
* print help message
*/
function help_message() {
"use strict";
info('========================================================');
info('required execution params');
info('possible params are');
info('[help] - print help message');
info('[migrate] - migrate DB using new migrations if new is existed');
info('[clean] - drop all tables in database');
info('[init] - create an empty migration schema in DB');
info('file name pattern is:');
info('\t\t V(version name)__name_of_script_separated_with_lower_underline.sql');
info('\t\t V1__init_tables.sql');
info('\t\t V2__add_new_column.sql');
info('for more info visit: https://github.com/borsch/node-mysql-migration/blob/master/README.md');
info('========================================================');
}
function info(message) {
"use strict";
console.log('[INFO] ' + message);
}
function warning(message) {
"use strict";
console.warn('[WARNING] ' + message);
}
function error(message) {
"use strict";
console.error('[ERROR] ' + message);
}