-
Notifications
You must be signed in to change notification settings - Fork 59
/
migrations_server.js
358 lines (304 loc) · 10.1 KB
/
migrations_server.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { check, Match } from 'meteor/check';
import { Log } from 'meteor/logging';
/*
Adds migration capabilities. Migrations are defined like:
Migrations.add({
up: function() {}, //*required* code to run to migrate upwards
version: 1, //*required* number to identify migration order
down: function() {}, //*optional* code to run to migrate downwards
name: 'Something' //*optional* display name for the migration
});
The ordering of migrations is determined by the version you set.
To run the migrations, set the MIGRATE environment variable to either
'latest' or the version number you want to migrate to. Optionally, append
',exit' if you want the migrations to exit the meteor process, e.g if you're
migrating from a script (remember to pass the --once parameter).
e.g:
MIGRATE="latest" mrt # ensure we'll be at the latest version and run the app
MIGRATE="latest,exit" mrt --once # ensure we'll be at the latest version and exit
MIGRATE="2,exit" mrt --once # migrate to version 2 and exit
Note: Migrations will lock ensuring only 1 app can be migrating at once. If
a migration crashes, the control record in the migrations collection will
remain locked and at the version it was at previously, however the db could
be in an inconsistent state.
*/
// since we'll be at version 0 by default, we should have a migration set for
// it.
const DefaultMigration = { version: 0, up: function() {} };
/**
*
* @type {{_list: {up: DefaultMigration.up, version: number}[], options: {logIfLatest: boolean, log: boolean, logger: null, collectionName: string}, config: Migrations.config}}
*/
export const Migrations = {
_list: [DefaultMigration],
options: {
// false disables logging
log: true,
// null or a function
logger: null,
// enable/disable info log "already at latest."
logIfLatest: true,
// migrations collection name
collectionName: 'migrations',
},
config: function(opts) {
this.options = Object.assign({}, this.options, opts);
},
};
/**
* Logger factory function. Takes a prefix string and options object
* and uses an injected `logger` if provided, else falls back to
* Meteor's `Log` package.
* Will send a log object to the injected logger, on the following form:
* message: String
* level: String (info, warn, error, debug)
* tag: 'Migrations'
* @param prefix
* @returns {(function())|*|(function(*, *): void)}
*/
function createLogger(prefix) {
check(prefix, String);
// Return noop if logging is disabled.
if (Migrations.options.log === false) {
return function() {};
}
return function(level, message) {
check(level, Match.OneOf('info', 'error', 'warn', 'debug'));
check(message, String);
const logger = Migrations.options?.logger;
if (logger && typeof logger === 'function') {
logger({
level: level,
message: message,
tag: prefix,
});
} else {
Log[level]({ message: prefix + ': ' + message });
}
};
}
let log;
Meteor.startup(async function() {
const options = Migrations.options;
// collection holding the control record
Migrations._collection = new Mongo.Collection(options.collectionName);
log = createLogger('Migrations');
['info', 'warn', 'error', 'debug'].forEach(function(level) {
log[level] = (message) => log(level, message)
});
if (process.env.MIGRATE) {
try {
await Migrations.migrateTo(process.env.MIGRATE);
} catch (e) {
log.error('Failed to run migrations')
log.error(e.message || e.reason)
}
}
});
/**
* Add a new migration
* @param migration {Object}
* @param migration.version {Number} required
* @param migration.up {function} required migration function
* @param migration.name {String} Optional name for the migration step
* @param migration.down {function} Optional function to migrate back from this step to previous version
*/
Migrations.add = function(migration) {
if (typeof migration.up !== 'function')
throw new Meteor.Error('Migration must supply an up function.');
if (typeof migration.version !== 'number')
throw new Meteor.Error('Migration must supply a version number.');
if (migration.version <= 0)
throw new Meteor.Error('Migration version must be greater than 0');
// Freeze the migration object to make it hereafter immutable
Object.freeze(migration);
this._list.push(migration);
this._list.sort((a, b) => (a.version > b.version) ? 1 : ((b.version > a.version) ? -1 : 0));
};
/**
* Attempts to run the migrations using command in the form of:
* e.g 'latest', 'latest,exit', 2
* use 'XX,rerun' to re-run the migration at that version
* @param command {string|number}
* @returns {Promise}
*/
Migrations.migrateTo = async function(command) {
if (typeof command === 'undefined' || command === '' || this._list.length === 0)
throw new Error('Cannot migrate using invalid command: ' + command);
let version;
let subcommand;
if (typeof command === 'number') {
version = command;
} else {
version = command.split(',')[0]; //.trim();
subcommand = command.split(',')[1]; //.trim();
}
if (version === 'latest') {
await this._migrateTo(this._list[this._list.length -1].version);
} else {
await this._migrateTo(parseInt(version), subcommand === 'rerun');
}
// remember to run meteor with --once otherwise it will restart
if (subcommand === 'exit') process.exit(0);
};
/**
* Just returns the current version
* @returns {Promise<void>}
*/
Migrations.getVersion = async function() {
const control = await this._getControl()
return control.version;
};
/**
* migrates to the specific version passed in
* @param version {number}
* @param rerun {boolean}
* @returns {Promise<void>}
* @private
*/
Migrations._migrateTo = async function(version, rerun) {
const self = this;
const control = await this._getControl(); // Side effect: upserts control document.
let currentVersion = control.version;
//Avoid unneeded locking, check if migration actually is going to run
if (!rerun && currentVersion === version) {
if (Migrations.options.logIfLatest) {
log.info('Not migrating, already at version ' + version);
}
return;
}
const isLock = await lock()
if (isLock === false) {
log.info('Not migrating, control is locked.');
return;
}
if (rerun) {
log.info('Rerunning version ' + version);
migrate('up', this._findIndexByVersion(version));
log.info('Finished migrating.');
await unlock();
return;
}
const startIdx = this._findIndexByVersion(currentVersion);
const endIdx = this._findIndexByVersion(version);
// log.info('startIdx:' + startIdx + ' endIdx:' + endIdx);
log.info(
'Migrating from version ' +
this._list[startIdx].version +
' -> ' +
this._list[endIdx].version,
);
// run the actual migration
function migrate(direction, idx) {
const migration = self._list[idx];
if (typeof migration[direction] !== 'function') {
unlock();
throw new Meteor.Error(
'Cannot migrate ' + direction + ' on version ' + migration.version,
);
}
function maybeName() {
return migration.name ? ' (' + migration.name + ')' : '';
}
log.info(
'Running ' +
direction +
'() on version ' +
migration.version +
maybeName(),
);
migration[direction](migration);
}
// Returns true if lock was acquired.
async function lock() {
// This is atomic. The selector ensures only one caller at a time will see
// the unlocked control, and locking occurs in the same update's modifier.
// All other simultaneous callers will get false back from the update.
return (
await self._collection.updateAsync(
{ _id: 'control', locked: false },
{ $set: { locked: true, lockedAt: new Date() } },
) === 1
);
}
// Side effect: saves version.
async function unlock() {
await self._setControl({ locked: false, version: currentVersion });
}
async function updateVersion() {
await self._setControl({ locked: true, version: currentVersion });
}
if (currentVersion < version) {
for (let i = startIdx; i < endIdx; i++) {
migrate('up', i + 1);
currentVersion = self._list[i + 1].version;
await updateVersion();
}
} else {
for (let i = startIdx; i > endIdx; i--) {
migrate('down', i);
currentVersion = self._list[i - 1].version;
await updateVersion();
}
}
await unlock();
log.info('Finished migrating.');
};
/**
* gets the current control record, optionally creating it if non-existent
* @returns {Promise<{ version: number, locked: boolean }>}
* @private
*/
Migrations._getControl = async function() {
const control = await this._collection.findOneAsync({ _id: 'control' });
return control || await this._setControl({ version: 0, locked: false });
};
/**
* sets the control record
* @param control {Object}
* @param control.version {number}
* @param control.locked {boolean}
* @returns {Promise<*>}
* @private
*/
Migrations._setControl = async function(control) {
// be quite strict
check(control.version, Number);
check(control.locked, Boolean);
await this._collection.updateAsync(
{ _id: 'control' },
{ $set: { version: control.version, locked: control.locked } },
{ upsert: true },
);
return control;
};
/**
* Returns the migration index in _list or throws if not found
* @param version {number}
* @returns {number}
* @private
*/
Migrations._findIndexByVersion = function(version) {
for (let i = 0; i < this._list.length; i++) {
if (this._list[i].version === version) return i;
}
throw new Meteor.Error("Can't find migration version " + version);
};
/**
* reset (mainly intended for tests)
* @returns {Promise<number>}
* @private
*/
Migrations._reset = async function() {
this._list = [{ version: 0, up: function() {} }];
return await this._collection.removeAsync({});
};
/**
* unlock control
* @returns {Promise<number>}
*/
Migrations.unlock = async function() {
return await this._collection.updateAsync({ _id: 'control' }, { $set: { locked: false } });
};