-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.js
112 lines (103 loc) · 3.01 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
var fs = require("fs");
var coreFunctions = require('./core_functions');
var queryFunctions = require('./query');
var config = require('./config');
var table = config['table'];
var migrations_types = config['migrations_types'];
var updateSchema = false;
var migrate_all = false;
function migration(conn, path, cb, options) {
if(cb == null)
cb = () => {};
argv = process.argv;
var updateSchemaIndex = argv.indexOf("--update-schema");
if (updateSchemaIndex > -1) {
updateSchema = true;
argv.splice(updateSchemaIndex, 1);
}
var migrate_index = argv.indexOf("--migrate-all");
if (migrate_index > -1) {
migrate_all = true;
argv.splice(migrate_index, 1);
}
if (options instanceof Array) {
if (options.indexOf("--migrate-all") > -1) {
migrate_all = true;
}
if (options.indexOf("--update-schema") > -1) {
updateSchema = true;
}
}
queryFunctions.run_query(conn, "CREATE TABLE IF NOT EXISTS `" + table + "` (`timestamp` varchar(254) NOT NULL UNIQUE)", function (res) {
handle(argv, conn, path, cb);
});
}
function handle(argv, conn, path, cb) {
if (argv.length > 2 && argv.length <= 6) {
if (argv[2] == 'add' && (argv[3] == 'migration' || argv[3] == 'seed')) {
coreFunctions.add_migration(argv, path, function () {
conn.end();
cb();
});
} else if (argv[2] == 'up') {
var count = null;
if (argv.length > 3) {
count = parseInt(argv[3]);
} else {
count = 999999;
}
if (migrate_all) {
coreFunctions.up_migrations_all(conn, count, path, function () {
updateSchemaAndEnd(conn, path);
cb();
});
} else {
coreFunctions.up_migrations(conn, count, path, function () {
updateSchemaAndEnd(conn, path);
cb();
});
}
} else if (argv[2] == 'down') {
var count = null;
if (argv.length > 3) {
count = parseInt(argv[3]);
} else count = 1;
coreFunctions.down_migrations(conn, count, path, function () {
updateSchemaAndEnd(conn, path);
cb();
});
} else if (argv[2] == 'refresh') {
coreFunctions.down_migrations(conn, 999999, path, function () {
coreFunctions.up_migrations(conn, 999999, path, function () {
updateSchemaAndEnd(conn, path);
cb();
});
});
} else if (argv[2] == 'run' && migrations_types.indexOf(argv[4]) > -1) {
coreFunctions.run_migration_directly(argv[3], argv[4], conn, path, function () {
updateSchemaAndEnd(conn, path);
cb();
});
} else if (argv[2] == 'load-from-schema') {
coreFunctions.createFromSchema(conn, path, function () {
conn.end();
cb();
});
}
else {
throw new Error('command not found : ' + argv.join(" "));
}
}
}
function updateSchemaAndEnd(conn, path) {
if (updateSchema) {
coreFunctions.update_schema(conn, path, function() {
conn.end();
})
} else {
conn.end();
}
}
module.exports = {
init: migration
}