-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.coffee
646 lines (503 loc) · 20.5 KB
/
server.coffee
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
import semver from 'semver'
import util from 'util'
globals = @
allMigrationDescriptors = []
try
# Migration of migrations collection.
new DirectCollection('migrations').renameCollection 'peerdb.migrations'
catch error
throw error unless /source namespace does not exist|Source collection .* does not exist/.test "#{error}"
# Fields:
# serial
# migrationName
# oldCollectionName
# newCollectionName
# oldVersion
# newVersion
# timestamp
# migrated
# all
#
# We use a lower case collection name to signal it is a system collection.
globals.Document.Migrations = new Meteor.Collection 'peerdb.migrations'
class globals.Document._Migration
updateAll: (document, collection, currentSchema, intoSchema) ->
@_updateAll = true
forward: (document, collection, currentSchema, newSchema) ->
migrated: 0
all: collection.update {_schema: currentSchema}, {$set: _schema: newSchema}, {multi: true}
backward: (document, collection, currentSchema, oldSchema) ->
migrated: 0
all: collection.update {_schema: currentSchema}, {$set: _schema: oldSchema}, {multi: true}
class globals.Document.PatchMigration extends globals.Document._Migration
class globals.Document.MinorMigration extends globals.Document._Migration
class globals.Document.MajorMigration extends globals.Document._Migration
class globals.Document.AddReferenceFieldsMigration extends globals.Document.MinorMigration
forward: (document, collection, currentSchema, newSchema) ->
@updateAll document, collection, currentSchema, newSchema
counts = super document, collection, currentSchema, newSchema
counts.migrated = counts.all
counts
backward: (document, collection, currentSchema, oldSchema) ->
@updateAll document, collection, currentSchema, oldSchema
counts = super document, collection, currentSchema, oldSchema
counts.migrated = counts.all
counts
class globals.Document.RemoveReferenceFieldsMigration extends globals.Document.MajorMigration
forward: (document, collection, currentSchema, newSchema) ->
@updateAll document, collection, currentSchema, newSchema
counts = super document, collection, currentSchema, newSchema
counts.migrated = counts.all
counts
backward: (document, collection, currentSchema, oldSchema) ->
@updateAll document, collection, currentSchema, oldSchema
counts = super document, collection, currentSchema, oldSchema
counts.migrated = counts.all
counts
class globals.Document.AddGeneratedFieldsMigration extends globals.Document.MinorMigration
# Fields is an array.
constructor: (fields) ->
super()
@fields = fields if fields
forward: (document, collection, currentSchema, newSchema) ->
assert @fields
@updateAll document, collection, currentSchema, newSchema
counts = super document, collection, currentSchema, newSchema
counts.migrated = counts.all
counts
backward: (document, collection, currentSchema, oldSchema) ->
update =
$unset: {}
$set:
_schema: oldSchema
for field in @fields
update.$unset[field] = ''
count = collection.update {_schema: currentSchema}, update, {multi: true}
counts = super document, collection, currentSchema, oldSchema
counts.migrated += count
counts.all += count
counts
class globals.Document.ModifyGeneratedFieldsMigration extends globals.Document.PatchMigration
# Fields is an array.
constructor: (fields) ->
super()
@fields = fields if fields
forward: (document, collection, currentSchema, newSchema) ->
assert @fields
@updateAll document, collection, currentSchema, newSchema
counts = super document, collection, currentSchema, newSchema
counts.migrated = counts.all
counts
backward: (document, collection, currentSchema, oldSchema) ->
assert @fields
@updateAll document, collection, currentSchema, oldSchema
counts = super document, collection, currentSchema, oldSchema
counts.migrated = counts.all
counts
class globals.Document.RemoveGeneratedFieldsMigration extends globals.Document.MajorMigration
# Fields is an array.
constructor: (fields) ->
super()
@fields = fields if fields
forward: (document, collection, currentSchema, newSchema) ->
update =
$unset: {}
$set:
_schema: newSchema
for field in @fields
update.$unset[field] = ''
count = collection.update {_schema: currentSchema}, update, {multi: true}
counts = super document, collection, currentSchema, newSchema
counts.migrated += count
counts.all += count
counts
backward: (document, collection, currentSchema, oldSchema) ->
assert @fields
@updateAll document, collection, currentSchema, oldSchema
counts = super document, collection, currentSchema, oldSchema
counts.migrated = counts.all
counts
class globals.Document.AddOptionalFieldsMigration extends globals.Document.MinorMigration
# Fields is an array.
constructor: (fields) ->
super()
@fields = fields if fields
forward: (document, collection, currentSchema, newSchema) ->
assert @fields
super document, collection, currentSchema, newSchema
backward: (document, collection, currentSchema, oldSchema) ->
update =
$unset: {}
$set:
_schema: oldSchema
for field in @fields
update.$unset[field] = ''
count = collection.update {_schema: currentSchema}, update, {multi: true}
counts = super document, collection, currentSchema, oldSchema
counts.migrated += count
counts.all += count
counts
class globals.Document.AddRequiredFieldsMigration extends globals.Document.MinorMigration
# Fields is an object.
constructor: (fields) ->
super()
@fields = fields if fields
forward: (document, collection, currentSchema, newSchema) ->
selector =
_schema: currentSchema
for field, value of @fields
selector[field] =
$exists: false
update =
$set:
_schema: newSchema
for field, value of @fields
if _.isFunction value
update.$set[field] = value()
else
update.$set[field] = value
count = collection.update selector, update, {multi: true}
counts = super document, collection, currentSchema, newSchema
counts.migrated += count
counts.all += count
counts
backward: (document, collection, currentSchema, oldSchema) ->
update =
$unset: {}
$set:
_schema: oldSchema
for field, value of @fields
update.$unset[field] = ''
count = collection.update {_schema: currentSchema}, update, {multi: true}
counts = super document, collection, currentSchema, oldSchema
counts.migrated += count
counts.all += count
counts
class globals.Document.RemoveFieldsMigration extends globals.Document.MajorMigration
# Fields is an object.
constructor: (fields) ->
super()
@fields = fields if fields
forward: (document, collection, currentSchema, newSchema) ->
update =
$unset: {}
$set:
_schema: newSchema
for field, value of @fields
update.$unset[field] = ''
count = collection.update {_schema: currentSchema}, update, {multi: true}
counts = super document, collection, currentSchema, newSchema
counts.migrated += count
counts.all += count
counts
backward: (document, collection, currentSchema, oldSchema) ->
selector =
_schema: currentSchema
for field, value of @fields
selector[field] =
$exists: false
update =
$set:
_schema: oldSchema
for field, value of @fields
if _.isFunction value
v = value()
else
v = value
update.$set[field] = v unless _.isUndefined v
count = collection.update selector, update, {multi: true}
counts = super document, collection, currentSchema, oldSchema
counts.migrated += count
counts.all += count
counts
class globals.Document.RenameFieldsMigration extends globals.Document.MajorMigration
# Fields is object.
constructor: (fields) ->
super()
@fields = fields if fields
forward: (document, collection, currentSchema, newSchema) ->
update =
$set:
_schema: newSchema
$rename: {}
for from, to of @fields
update.$rename[from] = to
count = collection.update {_schema: currentSchema}, update, {multi: true}
counts = super document, collection, currentSchema, newSchema
counts.migrated += count
counts.all += count
counts
backward: (document, collection, currentSchema, oldSchema) ->
update =
$set:
_schema: oldSchema
$rename: {}
for from, to of @fields
# Reversed.
update.$rename[to] = from
count = collection.update {_schema: currentSchema}, update, {multi: true}
counts = super document, collection, currentSchema, oldSchema
counts.migrated += count
counts.all += count
counts
class globals.Document._RenameCollectionMigration extends globals.Document.MajorMigration
constructor: (@oldName, @newName) ->
super()
@name = "Renaming collection from '#{@oldName}' to '#{@newName}'"
_rename: (collection, to) ->
try
collection.renameCollection to
catch error
throw error unless /source namespace does not exist|Source collection .* does not exist/.test "#{error}"
forward: (document, collection, currentSchema, newSchema) ->
assert.equal collection.name, @oldName
@_rename collection, @newName
collection.name = @newName
# We renamed the collection, so let's update all documents to new schema version.
counts = super document, collection, currentSchema, newSchema
# We migrated everything.
counts.migrated = counts.all
counts
backward: (document, collection, currentSchema, newSchema) ->
assert.equal collection.name, @newName
@_rename collection, @oldName
collection.name = @oldName
# We renamed the collection, so let's update all documents to old schema version.
counts = super document, collection, currentSchema, newSchema
# We migrated everything.
counts.migrated = counts.all
counts
globals.Document.addMigration = (migration) ->
throw new Error "Migration is missing a name" unless migration.name
throw new Error "Migration is not a migration instance" unless migration instanceof @_Migration
throw new Error "Migration with the name '#{migration.name}' already exists" if migration.name in _.pluck @Meta.migrations, 'name'
@Meta.migrations.push migration
allMigrationDescriptors.push
document: @
migration: migration
globals.Document.renameCollectionMigration = (oldName, newName) ->
@addMigration new @_RenameCollectionMigration oldName, newName
globals.Document.migrateForward = (untilMigration) ->
schemas = ['1.0.0']
currentSchema = '1.0.0'
currentSerial = 0
initialName = @Meta.collection._name
for migration in @Meta.migrations by -1 when migration instanceof @_RenameCollectionMigration
throw new Error "Inconsistent document renaming, renaming from '#{migration.oldName}' to '#{migration.newName}', but current name is '#{initialName}' (new name and current name should match)" if migration.newName isnt initialName
initialName = migration.oldName
migrationsPending = Number.POSITIVE_INFINITY
currentName = initialName
for migration, i in @Meta.migrations
if migration instanceof @PatchMigration
newSchema = semver.inc currentSchema, 'patch'
else if migration instanceof @MinorMigration
newSchema = semver.inc currentSchema, 'minor'
else if migration instanceof @MajorMigration
newSchema = semver.inc currentSchema, 'major'
if migration instanceof @_RenameCollectionMigration
newName = migration.newName
else
newName = currentName
migrations = globals.Document.Migrations.find(
serial:
$gt: currentSerial
oldCollectionName:
$in: [currentName, newName]
,
sort: [
['serial', 'asc']
]
).fetch()
if migrations[0]
throw new Error "Unexpected migration recorded: #{util.inspect migrations[0], depth: 10}" if migrationsPending < Number.POSITIVE_INFINITY
if migrations[0].migrationName is migration.name and migrations[0].oldCollectionName is currentName and migrations[0].newCollectionName is newName and migrations[0].oldVersion is currentSchema and migrations[0].newVersion is newSchema
currentSerial = migrations[0].serial
else
throw new Error "Inconsistent migration recorded, expected migrationName='#{migration.name}', oldCollectionName='#{currentName}', newCollectionName='#{newName}', oldVersion='#{currentSchema}', newVersion='#{newSchema}', got: #{util.inspect migrations[0], depth: 10}"
else if migrationsPending is Number.POSITIVE_INFINITY
migrationsPending = i
currentSchema = newSchema
schemas.push currentSchema
currentName = newName
unknownSchema = _.pluck @Meta.collection.find(
_schema:
$nin: schemas
$exists: true
,
fields:
_id: 1
).fetch(), '_id'
throw new Error "Documents with unknown schema version: #{unknownSchema}" if unknownSchema.length
updateAll = false
currentSchema = '1.0.0'
currentSerial = 0
currentName = initialName
previousMigration = null
for migration, i in @Meta.migrations
if previousMigration is untilMigration
break
previousMigration = migration
if migration instanceof @PatchMigration
newSchema = semver.inc currentSchema, 'patch'
else if migration instanceof @MinorMigration
newSchema = semver.inc currentSchema, 'minor'
else if migration instanceof @MajorMigration
newSchema = semver.inc currentSchema, 'major'
if i < migrationsPending and migration instanceof @_RenameCollectionMigration
# We skip all already done rename migrations (but we run other old migrations again, just with the last known collection name).
currentSchema = newSchema
currentName = migration.newName
continue
if migration instanceof @_RenameCollectionMigration
newName = migration.newName
else
newName = currentName
if globals.Document.migrationsDisabled
# Migrations are disabled but we are still running
# the code just to compute the latest schema version.
currentSchema = newSchema
currentName = newName
continue
migration._updateAll = false
counts = migration.forward @, new DirectCollection(currentName), currentSchema, newSchema
throw new Error "Invalid return value from migration: #{util.inspect counts}" unless 'migrated' of counts and 'all' of counts
updateAll = true if counts.migrated and migration._updateAll
if i < migrationsPending
count = globals.Document.Migrations.update
migrationName: migration.name
oldCollectionName: currentName
newCollectionName: newName
oldVersion: currentSchema
newVersion: newSchema
,
$inc:
migrated: counts.migrated
all: counts.all
,
# To catch any errors.
multi: true
throw new Error "Inconsistent migration record state, missing migrationName='#{migration.name}', oldCollectionName='#{currentName}', newCollectionName='#{newName}', oldVersion='#{currentSchema}', newVersion='#{newSchema}'" unless count is 1
else
count = globals.Document.Migrations.find(
migrationName: migration.name
oldCollectionName: currentName
newCollectionName: newName
oldVersion: currentSchema
newVersion: newSchema
).count()
throw new Error "Inconsistent migration record state, unexpected migrationName='#{migration.name}', oldCollectionName='#{currentName}', newCollectionName='#{newName}', oldVersion='#{currentSchema}', newVersion='#{newSchema}'" unless count is 0
globals.Document.Migrations.insert
# Things should not be running in parallel here anyway, so we can get next serial in this way.
serial: globals.Document.Migrations.findOne({}, {sort: [['serial', 'desc']]}).serial + 1
migrationName: migration.name
oldCollectionName: currentName
newCollectionName: newName
oldVersion: currentSchema
newVersion: newSchema
migrated: counts.migrated
all: counts.all
timestamp: new Date()
if migration instanceof @_RenameCollectionMigration
Log.info "Renamed collection '#{currentName}' to '#{newName}'"
Log.info "Migrated #{counts.migrated}/#{counts.all} document(s) (from #{currentSchema} to #{newSchema}): #{migration.name}" if counts.all
else
Log.info "Migrated #{counts.migrated}/#{counts.all} document(s) in '#{currentName}' collection (from #{currentSchema} to #{newSchema}): #{migration.name}" if counts.all
currentSchema = newSchema
currentName = newName
# We do not check for not migrated documents if migrations are disabled.
unless globals.Document.migrationsDisabled
# For all those documents which lack schema information we assume they have the last schema.
@Meta.collection.update
_schema:
$exists: false
,
$set:
_schema: currentSchema
,
multi: true
@Meta.schema = currentSchema
# Return if updateAll should be called.
updateAll
globals.Document.migrateBackward = (untilMigration) ->
# TODO: Implement.
throw new Error "Not implemented yet"
globals.Document._setupMigrationsObserve = ->
@Meta.collection.find(
_schema:
$exists: false
,
fields:
_id: 1
_schema: 1
).observeChanges
added: globals.Document._observerCallback true, (id, fields) =>
# TODO: Check if schema is known and complain if not.
# TODO: We could automatically migrate old documents if we know of newer schema.
return if fields._schema
@Meta.collection.update id,
$set:
_schema: @Meta.schema
getReplacedDocument = (document) ->
return document unless document.Meta._replaced
for current in globals.Document.list
parentMeta = current.Meta
while parentMeta
if parentMeta is document.Meta
return current
parentMeta = parentMeta.parent
throw new Error "Cannot find a replaced document for '#{document.Meta._name}'."
migrateAllForward = ->
updateAll = false
for migrationDescriptor in allMigrationDescriptors
updateAll = getReplacedDocument(migrationDescriptor.document).migrateForward(migrationDescriptor.migration) or updateAll
# We set initial schema value for all documents in server collections which do not have migrations.
for document in globals.Document.list when document.Meta.collection._connection is Meteor.server
unless document.Meta.schema
document.migrateForward(null)
# Return if updateAll should be called.
updateAll
# TODO: What happens if this is called multiple times? We should make sure that for each document observers are made only once.
setupMigrationsObserve = ->
# Setup migrations' observe only for server collections.
for document in globals.Document.list when document.Meta.collection._connection is Meteor.server
document._setupMigrationsObserve()
migrations = ->
if not globals.Document.migrationsDisabled and globals.Document.Migrations.find({}, limit: 1).count() == 0
globals.Document.Migrations.insert
serial: 1
migrationName: null
oldCollectionName: null
newCollectionName: null
oldVersion: null
newVersion: null
timestamp: new Date()
migrated: 0
all: 0
# Even with disabled migrations this computes the latest schema versions for every document.
updateAll = migrateAllForward()
# Check that everything has been migrated to the latest schema version.
unless globals.Document.migrationsDisabled
# Check only for server collections.
for document in globals.Document.list when document.Meta.collection._connection is Meteor.server
notMigrated = _.pluck document.Meta.collection.find(
_schema:
$ne: document.Meta.schema
,
fields:
_id: 1
).fetch(), '_id'
throw new Error "Not all documents migrated to the latest schema version (#{document.Meta.schema}) for '#{document.Meta._name}': #{notMigrated}" if notMigrated.length
unless globals.Document.instanceDisabled
setupMigrationsObserve()
# "updateAll" can be "true" only if migrations are not disabled.
if updateAll
Log.info "Migrations requested updating all references..."
globals.Document.updateAll()
globals.Document.migrationsDisabled = !!process.env.PEERDB_MIGRATIONS_DISABLED
globals.Document.prepare ->
Log.info "Skipped migrations" if globals.Document.migrationsDisabled
# We still run the code to determine schema version and setup
# observer to set schema version when inserting new documents,
# but we then inside the code skip running migrations themselves.
migrations()
Document = globals.Document