-
Notifications
You must be signed in to change notification settings - Fork 26
/
core_functions.js
209 lines (181 loc) · 6.49 KB
/
core_functions.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
var fs = require("fs");
var fileFunctions = require('./file');
var queryFunctions = require('./query');
var colors = require('colors');
var exec = require('child_process').exec;
var table = require('./config')['table'];
function add_migration(argv, path, cb) {
fileFunctions.validate_file_name(argv[4]);
fileFunctions.readFolder(path, function (files) {
var file_name = Date.now() + "_" + argv[4];
var file_path = path + '/' + file_name + '.js';
var sql_json = {
up : '',
down : ''
};
if (argv.length > 5) {
sql_json['up'] = argv[5];
}
var content = 'module.exports = ' + JSON.stringify(sql_json, null, 4);
fs.writeFile(file_path, content, 'utf-8', function (err) {
if (err) {
throw err;
}
console.log("Added file " + file_name);
cb();
});
});
}
function up_migrations(conn, max_count, path, cb) {
queryFunctions.run_query(conn, "SELECT timestamp FROM " + table + " ORDER BY timestamp DESC LIMIT 1", function (results) {
var file_paths = [];
var max_timestamp = 0;
if (results.length) {
max_timestamp = results[0].timestamp;
}
fileFunctions.readFolder(path, function (files) {
files.forEach(function (file) {
var timestamp_split = file.split("_", 1);
if (timestamp_split.length) {
var timestamp = parseInt(timestamp_split[0]);
if (Number.isInteger(timestamp) && timestamp.toString().length == 13 && timestamp > max_timestamp) {
file_paths.push({ timestamp : timestamp, file_path : file});
}
} else {
throw new Error('Invalid file ' + file);
}
});
var final_file_paths = file_paths.sort(function(a, b) { return (a.timestamp - b.timestamp)}).slice(0, max_count);
queryFunctions.execute_query(conn, path, final_file_paths, 'up', cb);
});
});
}
function up_migrations_all(conn, max_count, path, cb) {
queryFunctions.run_query(conn, "SELECT timestamp FROM " + table, function (results) {
var file_paths = [];
var timestamps = results.map(r => parseInt(r.timestamp));
fileFunctions.readFolder(path, function (files) {
files.forEach(function (file) {
var timestamp_split = file.split("_", 1);
if (timestamp_split.length) {
var timestamp = parseInt(timestamp_split[0]);
if (Number.isInteger(timestamp) && timestamp.toString().length == 13 && !timestamps.includes(timestamp)) {
file_paths.push({ timestamp : timestamp, file_path : file});
}
} else {
throw new Error('Invalid file ' + file);
}
});
var final_file_paths = file_paths.sort(function(a, b) { return (a.timestamp - b.timestamp)}).slice(0, max_count);
queryFunctions.execute_query(conn, path, final_file_paths, 'up', cb);
});
});
}
function down_migrations(conn, max_count, path, cb) {
queryFunctions.run_query(conn, "SELECT timestamp FROM " + table + " ORDER BY timestamp DESC LIMIT " + max_count, function (results) {
var file_paths = [];
var max_timestamp = 0;
if (results.length) {
var temp_timestamps = results.map(function(ele) {
return ele.timestamp;
});
fileFunctions.readFolder(path, function (files) {
files.forEach(function (file) {
var timestamp = file.split("_", 1)[0];
if (temp_timestamps.indexOf(timestamp) > -1) {
file_paths.push({ timestamp : timestamp, file_path : file});
}
});
var final_file_paths = file_paths.sort(function(a, b) { return (b.timestamp - a.timestamp)}).slice(0, max_count);
queryFunctions.execute_query(conn, path, final_file_paths, 'down', cb);
});
}
});
}
function run_migration_directly(file, type, conn, path, cb) {
var current_file_path = path + "/" + file;
var query = require(current_file_path)[type];
queryFunctions.run_query(conn, query, cb);
}
function update_schema(conn, path, cb) {
var conn_config = conn.config.connectionConfig;
var filePath = path + '/' + 'schema.sql';
fs.unlink(filePath, function() {
var cmd = "mysqldump --no-data ";
if (conn_config.host) {
cmd = cmd + " -h " + conn_config.host;
}
if (conn_config.port) {
cmd = cmd + " --port=" + conn_config.port;
}
if (conn_config.user) {
cmd = cmd + " --user=" + conn_config.user;
}
if (conn_config.password) {
cmd = cmd + " --password=" + conn_config.password;
}
cmd = cmd + " " + conn_config.database;
exec(cmd, function(error, stdout, stderr) {
fs.writeFile(filePath, stdout, function(err) {
if (err) {
console.log(colors.red("Could not save schema file"));
}
cb();
});
});
});
}
function createFromSchema(conn, path, cb) {
var conn_config = conn.config.connectionConfig;
var filePath = path + '/' + 'schema.sql';
if (fs.existsSync(filePath)) {
var cmd = "mysql ";
if (conn_config.host) {
cmd = cmd + " -h " + conn_config.host;
}
if (conn_config.port) {
cmd = cmd + " --port=" + conn_config.port;
}
if (conn_config.user) {
cmd = cmd + " --user=" + conn_config.user;
}
if (conn_config.password) {
cmd = cmd + " --password=" + conn_config.password;
}
cmd = cmd + " " + conn_config.database;
cmd = cmd + " < " + filePath;
exec(cmd, function(error, stdout, stderr) {
if (error) {
console.log(colors.red("Could not load from Schema: " + error));
cb();
} else {
var file_paths = [];
fileFunctions.readFolder(path, function (files) {
files.forEach(function (file) {
var timestamp_split = file.split("_", 1);
var timestamp = parseInt(timestamp_split[0]);
if (timestamp_split.length) {
file_paths.push({ timestamp : timestamp, file_path : file});
} else {
throw new Error('Invalid file ' + file);
}
});
var final_file_paths = file_paths.sort(function(a, b) { return (a.timestamp - b.timestamp)}).slice(0, 9999999);
queryFunctions.execute_query(conn, path, final_file_paths, 'up', cb, false);
});
}
});
} else {
console.log(colors.red("Schema Missing: " + filePath));
cb();
}
}
module.exports = {
add_migration: add_migration,
up_migrations: up_migrations,
up_migrations_all: up_migrations_all,
down_migrations: down_migrations,
run_migration_directly: run_migration_directly,
update_schema: update_schema,
createFromSchema: createFromSchema
};