-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration.js
133 lines (119 loc) · 4.72 KB
/
migration.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
const database = require('./src/js/chatbot/database');
const locales = require('./src/js/chatbot/locales');
const fs = require('fs');
const glob = require('glob');
const moment = require('moment');
const yargs = require('yargs');
const migrationFolder = './data/migration/';
let pastMigrations = [];
const versionExtract = /^.\/data\/migration\/(.*).js$/;
const argv = yargs
.option('direction', {
alias: 'd',
default: 'up',
description: 'Migration direction)',
type: 'string'
})
.option('file', {
alias: 'f',
description: 'Execute one file (e.g.: -f version-1.0.0)',
type: 'string'
})
.option('locale', {
alias: 'l',
default: 'en',
description: 'Locale for log messages',
type: 'string'
})
.option('log', {
default: true,
description: 'Show logs in CLI',
type: 'boolean'
})
.help()
.alias('help', 'h')
.argv;
if (argv.locale) {
locales.changeLanguage(argv.locale);
}
function log(message) {
if (argv.log === true) {
console.log(message);
}
}
// if file matches pattern
if (/^version-[0-9]+.[0-9]+.[0-9]+$/.test(argv.file)) {
// prepend folder path and append file extension
argv.file = migrationFolder + argv.file + '.js';
} else if (typeof argv.file === 'string' && argv.file.length) {
// echo error message if "-f" was defined but not matches pattern
log(locales.t('migration-file-incorrect', [argv.file]));
// abort futher script execution
process.exit(1);
}
function executeMigarationFilesRecursive(migrationFiles, index) {
// if is end of array
if (typeof migrationFiles[index] === 'undefined') {
return;
} else {
let migration = require(migrationFiles[index]);
// if migration was executed in the past
if (pastMigrations.indexOf(migrationFiles[index]) > -1) {
if (typeof argv.file === 'string') {
log(locales.t('migration-file-executed', [migrationFiles[index].replace(versionExtract, '$1')]));
}
executeMigarationFilesRecursive(migrationFiles, ++index);
} else {
// if migration was not executed in the past and direction up
if (pastMigrations.indexOf(migrationFiles[index]) === -1 && argv.direction === 'up') {
migration[argv.direction](function() {
let time = moment().unix();
let values = {
name: migrationFiles[index],
updatedAt: time,
createdAt: time
};
database.insert('migration', [values], function(migrationInsert) {
log(locales.t('migration-executed', [migrationFiles[index].replace(versionExtract, '$1'), locales.t(argv.direction)]));
executeMigarationFilesRecursive(migrationFiles, ++index);
});
});
}
// if migration was not executed in the past and direction down
if (pastMigrations.indexOf(migrationFiles[index]) !== -1 && argv.direction === 'down') {
migration[argv.direction](function() {
let where = ['name = ?'];
let prepare = [migrationFiles[index]];
database.remove('migration', where, prepare, function(migrationRemove) {
log(locales.t('migration-executed', [migrationFiles[index].replace(versionExtract, '$1'), locales.t(argv.direction)]));
executeMigarationFilesRecursive(migrationFiles, ++index);
});
});
}
}
}
}
// if folder exists
if (fs.existsSync(migrationFolder)) {
database.find('*', 'migration', '', [], '', 'name', 0, [], function(migrationRows) {
for (let i = 0; i < migrationRows.length; i++) {
pastMigrations.push(migrationRows[i].name);
}
// if execute a single file
if (typeof argv.file !== 'undefined' && fs.existsSync(argv.file)) {
executeMigarationFilesRecursive([argv.file], 0);
} else {
glob(migrationFolder + 'version-*.js', {}, function(error, migrationFiles) {
if (argv.direction === 'down') {
migrationFiles.reverse();
}
if ((argv.direction === 'up' && pastMigrations.length === migrationFiles.length)
|| (argv.direction === 'down' && !pastMigrations.length)) {
log(locales.t('migration-all-executed'));
} else {
executeMigarationFilesRecursive(migrationFiles, 0);
}
});
}
});
}