forked from worknenjoy/gitpay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.js
235 lines (203 loc) · 6.16 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
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
'use strict'
const path = require('path');
const child_process = require('child_process');
const Promise = require('bluebird');
const env = process.env.NODE_ENV || 'development';
const database_env = {
'development': 'databaseDev',
'staging': 'databaseStaging',
'production': 'databaseProd',
'test': 'databaseTest'
};
const config = require('./config/secrets')[database_env[env]];
const Sequelize = require('sequelize');
const Umzug = require('umzug');
let sequelize = {};
if (env === 'production' || env === 'staging') {
const database_url = process.env.DATABASE_URL;
const database_settings = database_url.split(':');
const port = database_settings[4];
const host = database_settings[3];
sequelize = new Sequelize(database_url, {
dialect: 'postgres',
protocol: 'postgres',
port: port,
host: host,
logging: false,
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false
}
}
});
console.log('running production migration');
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}
const migrationType = process.env.TYPE
const umzug = new Umzug({
storage: 'sequelize',
storageOptions: {
sequelize: sequelize,
},
migrations: {
params: [
sequelize.getQueryInterface(), // queryInterface
sequelize.constructor, // DataTypes
() => {
throw new Error('Migration tried to use old style "done" callback. Please upgrade to "umzug" and return a promise instead.');
}
],
path: migrationType === 'seed' ? './migration/seeders' : './migration/migrations',
pattern: /\.js$/
},
logging: () => {
//console.log.apply(null, arguments);
},
});
function logUmzugEvent(eventName) {
return (name, migration) => {
console.log(`${name} ${eventName}`);
}
}
umzug.on('migrating', logUmzugEvent('migrating'));
umzug.on('migrated', logUmzugEvent('migrated'));
umzug.on('reverting', logUmzugEvent('reverting'));
umzug.on('reverted', logUmzugEvent('reverted'));
function cmdStatus() {
let result = {};
return umzug.executed()
.then(executed => {
result.executed = executed;
return umzug.pending();
}).then(pending => {
result.pending = pending;
return result;
}).then(({ executed, pending }) => {
executed = executed.map(m => {
m.name = path.basename(m.file, '.js');
return m;
});
pending = pending.map(m => {
m.name = path.basename(m.file, '.js');
return m;
});
const current = executed.length > 0 ? executed[0].file : '<NO_MIGRATIONS>';
const status = {
current: current,
executed: executed.map(m => m.file),
pending: pending.map(m => m.file),
}
//console.log(JSON.stringify(status, null, 2))
return { executed, pending };
})
}
function cmdMigrate() {
return umzug.up();
}
function cmdMigrateNext() {
return cmdStatus()
.then(({ executed, pending }) => {
if (pending.length === 0) {
return Promise.reject(new Error('No pending migrations'));
}
const next = pending[0].name;
return umzug.up({ to: next });
})
}
function cmdUpdateAll() {
return cmdStatus()
.then(({ executed, pending }) => {
let length = pending.length;
if (length === 0) {
return Promise.reject(new Error('No pending migrations'));
}
length -= 1;
const next = pending[0].name;
const last = pending[length].name;
return umzug.up({ from: next, to: last });
})
}
function cmdReset() {
return umzug.down({ to: 0 });
}
function cmdResetPrev() {
return cmdStatus()
.then(({ executed, pending }) => {
if (executed.length === 0) {
return Promise.reject(new Error('Already at initial state'));
}
const prev = executed[executed.length - 1].name;
return umzug.down({ to: prev });
})
}
function cmdHardReset() {
return new Promise((resolve, reject) => {
setImmediate(() => {
try {
console.log(`dropdb ${config.database}`);
child_process.spawnSync(`dropdb ${config.database}`);
console.log(`createdb ${config.database} --username ${config.username}`);
child_process.spawnSync(`createdb ${config.database} --username ${config.username}`);
resolve();
} catch (e) {
// console.log(e);
reject(e);
}
});
});
}
const cmd = process.argv[2].trim();
let executedCmd;
// console.log(`${cmd.toUpperCase()} BEGIN`);
switch (cmd) {
case 'status':
executedCmd = cmdStatus();
break;
case 'up':
case 'migrate':
executedCmd = cmdMigrate();
break;
case 'next':
case 'migrate-next':
executedCmd = cmdMigrateNext();
break;
case 'down':
case 'reset':
executedCmd = cmdReset();
break;
case 'prev':
case 'reset-prev':
executedCmd = cmdResetPrev();
break;
case 'reset-hard':
executedCmd = cmdHardReset();
break;
case 'update':
executedCmd = cmdUpdateAll();
break;
default:
console.log(`invalid cmd: ${cmd}`);
process.exit(1);
}
executedCmd
.then((result) => {
const doneStr = `${cmd.toUpperCase()} DONE`;
// console.log(doneStr);
console.log("=".repeat(doneStr.length));
})
.catch(err => {
const errorStr = `${cmd.toUpperCase()} ERROR`;
// console.log(errorStr);
console.log("=".repeat(errorStr.length));
// console.log(err);
console.log("=".repeat(errorStr.length));
})
.then(() => {
if (cmd !== 'status' && cmd !== 'reset-hard') {
return cmdStatus()
}
return Promise.resolve();
})
.then(() => process.exit(0))