-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate.js
executable file
·145 lines (118 loc) · 3.11 KB
/
migrate.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
#!/usr/bin/env node
"use strict";
const client = require("./includes/redisClient");
const step = require("step");
const h = require("whispeerHelper");
const fs = require("fs");
const path = require("path");
var migrationState, availableMigrations, highestMigrationState;
const migrationToRun = process.argv[2];
var runMigrations;
const setup = require("./includes/setup");
function fileNameToID(name) {
return h.parseDecimal(name.split("-")[0]);
}
function getMigrationPath(id) {
var i;
for (i = 0; i < availableMigrations.length; i += 1) {
if (fileNameToID(availableMigrations[i]) === id) {
return "./migrations/" + availableMigrations[i];
}
}
throw new Error("Migration not found: " + id);
}
function updateMigrationState(state, cb) {
step(function () {
client.set("server:migrationState", migrationState + 1, this);
}, h.sF(function () {
migrationState += 1;
if (migrationState < highestMigrationState) {
runMigrations(this);
} else {
this.ne();
}
}), cb);
}
function runMigration(id, cb) {
console.log("runing migration " + id);
step(function () {
var toRun = getMigrationPath(id);
require(toRun)(this);
}, h.sF(function (success) {
if (success) {
console.log("Migration Completed: " + (id));
this.ne();
} else {
throw new Error("Migration failed! " + id);
}
}), cb);
}
runMigrations = function (cb) {
step(function () {
runMigration(migrationState + 1, this);
}, h.sF(function () {
updateMigrationState(migrationState + 1, this);
}), cb);
};
function loadMigrations(cb) {
step(function () {
fs.readdir(path.resolve(__dirname, "migrations"), this);
}, h.sF(function (files) {
availableMigrations = files.filter(function (file) {
return fs.statSync(path.resolve(__dirname, "migrations", file)).isFile();
});
this.ne();
}), cb);
}
function getNewestMigrationCount(cb) {
step(function () {
loadMigrations(this);
}, h.sF(function () {
var highest = 0;
availableMigrations.forEach(function (file) {
var current = fileNameToID(file);
highest = Math.max(highest, current);
});
console.log("Highest available Migration: " + highest);
this.ne(highest);
}), cb);
}
if (migrationToRun) {
step(function () {
setup(this);
}, h.sF(function () {
loadMigrations(this);
}), h.sF(function () {
runMigration(h.parseDecimal(migrationToRun), this);
}), function (e) {
if (e) {
throw e;
}
console.log("Migration completed");
process.exit(0);
});
} else {
step(function () {
setup(this);
}, h.sF(function () {
client.get("server:migrationState", this);
}), h.sF(function (_migrationState) {
migrationState = h.parseDecimal(_migrationState) || 0;
console.log("Current Migration State: " + migrationState);
getNewestMigrationCount(this);
}), h.sF(function (_highestMigrationState) {
highestMigrationState = _highestMigrationState;
if (highestMigrationState === migrationState) {
this.last.ne();
console.log("No Migrations available");
} else {
console.log("Running " + (highestMigrationState - migrationState) + " Migration(s)");
runMigrations(this);
}
}), function (e) {
if (e) {
throw e;
}
process.exit(0);
});
}