forked from percolatestudio/meteor-migrations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrations_server.js
201 lines (163 loc) · 5.99 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
/*
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 inconsistant state.
*/
// since we'll be at version 0 by default, we should have a migration set for
// it.
var DefaultMigration = {version: 0, up: function(){}};
Migrations = {
_list: [DefaultMigration],
}
// collection holding the control record
Migrations._collection = new Mongo.Collection('migrations');
Meteor.startup(function () {
if (process.env.MIGRATE)
Migrations.migrateTo(process.env.MIGRATE);
});
// Add a new migration:
// {up: function *required
// version: Number *required
// down: function *optional
// name: String *optional
// }
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');
this._list.push(migration);
this._list = _.sortBy(this._list, function(m) {return m.version;});
}
// 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
Migrations.migrateTo = function(command) {
if (_.isUndefined(command) || command === '' || this._list.length === 0)
throw new Error("Cannot migrate using invalid command: " + command);
if (typeof command === 'number') {
var version = command;
} else {
var version = command.split(',')[0];
var subcommand = command.split(',')[1];
}
if (version === 'latest') {
this._migrateTo(_.last(this._list).version);
} else {
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
Migrations.getVersion = function() {
return this._getControl().version;
}
// migrates to the specific version passed in
Migrations._migrateTo = function(version, rerun) {
var self = this;
var control = this._getControl();
var currentVersion = control.version;
if (rerun) {
console.log('Rerunning version ' + version);
setLocked(true);
migrate('up', version);
setLocked(false);
console.log('Finished migrating.');
return;
}
if (currentVersion === version) {
console.log('Not migrating, already at version ' + version);
return;
}
if (control.locked) {
console.log('Not migrating, control is locked.');
return;
}
var startIdx = this._findIndexByVersion(currentVersion);
var endIdx = this._findIndexByVersion(version);
// console.log('startIdx:' + startIdx + ' endIdx:' + endIdx);
console.log('Migrating from version ' + this._list[startIdx].version
+ ' -> ' + this._list[endIdx].version);
// run the actual migration
function migrate(direction, idx) {
var migration = self._list[idx];
if (typeof migration[direction] !== 'function') {
throw new Meteor.Error('Cannot migrate ' + direction + ' on version '
+ migration.version);
}
function maybeName() {
return migration.name ? ' (' + migration.name + ')' : '';
}
console.log('Running ' + direction + '() on version '
+ migration.version + maybeName());
migration[direction].call();
}
// sets the current version to be locked/unlocked
function setLocked(locked) {
self._setControl({version:currentVersion, locked: locked});
}
setLocked(true);
if (currentVersion < version) {
for (var i = startIdx;i < endIdx;i++) {
migrate('up', i + 1);
currentVersion = self._list[i + 1].version;
setLocked(true);
}
} else {
for (var i = startIdx;i > endIdx;i--) {
migrate('down', i);
currentVersion = self._list[i - 1].version;
setLocked(true);
}
}
setLocked(false);
console.log('Finished migrating.');
}
// gets the current control record, optionally creating it if non-existant
Migrations._getControl = function() {
var control = this._collection.findOne({_id: 'control'});
return control || this._setControl({version: 0, locked: false});
}
// sets the control record
Migrations._setControl = function(control) {
// be quite strict
check(control.version, Number);
check(control.locked, Boolean);
this._collection.update({_id: 'control'},
{$set: {version: control.version, locked: control.locked}}, {upsert: true});
return control;
}
// returns the migration index in _list or throws if not found
Migrations._findIndexByVersion = function(version) {
for (var 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)
Migrations._reset = function() {
this._list = [{version: 0, up: function(){}}];
this._collection.remove({});
}