/
databaseMigration.ts
716 lines (630 loc) · 25.4 KB
/
databaseMigration.ts
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
import {
parseVersion,
getCoreSystem,
setCoreSystemVersion,
Version,
GENESIS_SYSTEM_VERSION
} from '../../lib/collections/CoreSystem'
import { Meteor } from 'meteor/meteor'
import * as _ from 'underscore'
import {
MigrationMethods,
RunMigrationResult,
MigrationChunk,
MigrationStepType,
GetMigrationStatusResult
} from '../../lib/api/migration'
import {
MigrationStepInput,
MigrationStepInputResult,
MigrationStepInputFilteredResult,
MigrationStep,
MigrationStepBase,
ValidateFunctionCore,
MigrateFunctionCore,
ValidateFunctionStudio,
ValidateFunctionShowStyle,
MigrateFunctionStudio,
MigrateFunctionShowStyle,
InputFunctionCore,
InputFunctionStudio,
InputFunctionShowStyle,
MigrationContextStudio as IMigrationContextStudio,
MigrationContextShowStyle as IMigrationContextShowStyle,
BlueprintManifestType,
ShowStyleBlueprintManifest,
StudioBlueprintManifest
} from 'tv-automation-sofie-blueprints-integration'
import { setMeteorMethods } from '../methods'
import { logger } from '../../lib/logging'
import { storeSystemSnapshot } from '../api/snapshot'
import { ShowStyleBases } from '../../lib/collections/ShowStyleBases'
import { Blueprints } from '../../lib/collections/Blueprints'
import { Studios } from '../../lib/collections/Studios'
import { MigrationContextStudio, MigrationContextShowStyle } from '../api/blueprints/migrationContext'
import { getHash } from '../../lib/lib'
import * as semver from 'semver'
import { evalBlueprints } from '../api/blueprints/cache'
/** The current database version, x.y.z
* 0.16.0: Release 3 (2018-10-26)
* 0.17.0: Release 3.1 (2018-11-14)
* 0.18.0: Release 4 (2018-11-26)
* 0.19.0: Release 5 (2019-01-11)
* 0.20.0: Release 5.1 (2019-02-05)
* 0.21.0: Release 6 (never released)
* 0.22.0: Release 7 (2019-03-15)
* 0.23.0: Release 8 (2019-04-08)
* 0.24.0: Release 9 (2019-05-16)
* 0.25.0: Release 10 (2019-07-05)
* 0.26.0: Release 11 -
* 1.0.0: Release 12 (2019-09-11)
* 1.1.0: Release 13 (2019-10-17)
* 1.2.0: Release 14 (2019-11-06)
* 1.3.0: Release 15 (2019-11-25)
* 1.4.0: Release 16 (TBD)
* 1.5.0: Release 17 (TBD)
*/
export const CURRENT_SYSTEM_VERSION = '1.5.0'
/**
* These versions are not supported anymore (breaking changes occurred after these versions)
* This means that things like snaphots from older versions cannot be restored from
*/
export const UNSUPPORTED_VERSIONS = [
// 0.18.0 to 0.19.0: Major refactoring, (ShowStyles was split into ShowStyleBase &
// ShowStyleVariant, configs & layers wher emoved from studio to ShowStyles)
'<=0.18',
// 0.24.0 to 0.25.0: Major refactoring, Renaming of RunningOrders, segmentLines & segmentLineItems to Rundowns, parts & pieces. And a lot more
'<=0.24'
]
export function isVersionSupported (version: Version) {
let isSupported: boolean = true
_.each(UNSUPPORTED_VERSIONS, (uv) => {
if (semver.satisfies(version, uv)) {
isSupported = false
}
})
return isSupported
}
interface MigrationStepInternal extends MigrationStep {
chunk: MigrationChunk
_rank: number
_version: Version // step version
_validateResult: string | boolean
}
const coreMigrationSteps: Array<MigrationStep> = []
/**
* Add new system Migration step
* @param step
*/
export function addMigrationStep (step: MigrationStep) {
coreMigrationSteps.push(step)
}
/**
* Convenience method to add multiple steps of the same version
* @param version
* @param steps
*/
export function addMigrationSteps (version: string, steps: Array<MigrationStepBase>) {
_.each(steps, (step) => {
addMigrationStep(_.extend(step, {
version: version
}))
})
}
/** Removes all migration steps (used in tests) */
export function clearMigrationSteps () {
coreMigrationSteps.splice(0, 99999)
}
export interface PreparedMigration {
hash: string
chunks: MigrationChunk[]
steps: MigrationStepInternal[]
migrationNeeded: boolean
automaticStepCount: number
manualStepCount: number
ignoredStepCount: number
manualInputs: MigrationStepInput[]
partialMigration: boolean
}
export function prepareMigration (returnAllChunks?: boolean): PreparedMigration {
let databaseSystem = getCoreSystem()
if (!databaseSystem) throw new Meteor.Error(500, 'System version not set up')
// Discover applicable migration steps:
let migrationNeeded: boolean = false
let allMigrationSteps: Array<MigrationStepInternal> = []
let migrationChunks: Array<MigrationChunk> = []
let rank: number = 0
const databaseVersion = parseVersion(databaseSystem.version)
const targetVersion = parseVersion(CURRENT_SYSTEM_VERSION)
if (!semver.eq(databaseVersion, targetVersion)) migrationNeeded = true
// Collect migration steps from core system:
let chunk: MigrationChunk = {
sourceType: MigrationStepType.CORE,
sourceName: 'system',
_dbVersion: databaseVersion,
_targetVersion: targetVersion,
_steps: []
}
migrationChunks.push(chunk)
// Collect migration steps from system:
_.each(coreMigrationSteps, (step) => {
allMigrationSteps.push({
id: step.id,
overrideSteps: step.overrideSteps,
validate: step.validate,
canBeRunAutomatically: step.canBeRunAutomatically,
migrate: step.migrate,
input: step.input,
dependOnResultFrom: step.dependOnResultFrom,
version: step.version,
_version: parseVersion(step.version),
_validateResult: false, // to be set later
_rank: rank++,
chunk: chunk
})
})
// Collect migration steps from blueprints:
Blueprints.find({}).forEach((blueprint) => {
if (blueprint.code) {
const rawBlueprint = evalBlueprints(blueprint)
// @ts-ignore
if (!blueprint.databaseVersion || _.isString(blueprint.databaseVersion)) blueprint.databaseVersion = {}
if (!blueprint.databaseVersion.showStyle) blueprint.databaseVersion.showStyle = {}
if (!blueprint.databaseVersion.studio) blueprint.databaseVersion.studio = {}
if (blueprint.blueprintType === BlueprintManifestType.SHOWSTYLE) {
const bp = rawBlueprint as ShowStyleBlueprintManifest
// Find all showStyles that uses this blueprint:
ShowStyleBases.find({
blueprintId: blueprint._id
}).forEach((showStyleBase) => {
let chunk: MigrationChunk = {
sourceType: MigrationStepType.SHOWSTYLE,
sourceName: 'Blueprint ' + blueprint.name + ' for showStyle ' + showStyleBase.name,
blueprintId: blueprint._id,
sourceId: showStyleBase._id,
_dbVersion: parseVersion(blueprint.databaseVersion.showStyle[showStyleBase._id] || '0.0.0'),
_targetVersion: parseVersion(bp.blueprintVersion),
_steps: []
}
migrationChunks.push(chunk)
// Add show-style migration steps from blueprint:
_.each(bp.showStyleMigrations, (step) => {
allMigrationSteps.push(prefixIdsOnStep('blueprint_' + blueprint._id + '_showStyle_' + showStyleBase._id + '_', {
id: step.id,
overrideSteps: step.overrideSteps,
validate: step.validate,
canBeRunAutomatically: step.canBeRunAutomatically,
migrate: step.migrate,
input: step.input,
dependOnResultFrom: step.dependOnResultFrom,
version: step.version,
_version: parseVersion(step.version),
_validateResult: false, // to be set later
_rank: rank++,
chunk: chunk
}))
})
})
} else if (blueprint.blueprintType === BlueprintManifestType.STUDIO) {
const bp = rawBlueprint as StudioBlueprintManifest
// Find all studios that use this blueprint
Studios.find({
blueprintId: blueprint._id
}).forEach((studio) => {
let chunk: MigrationChunk = {
sourceType: MigrationStepType.STUDIO,
sourceName: 'Blueprint ' + blueprint.name + ' for studio ' + studio.name,
blueprintId: blueprint._id,
sourceId: studio._id,
_dbVersion: parseVersion(blueprint.databaseVersion.studio[studio._id] || '0.0.0'),
_targetVersion: parseVersion(bp.blueprintVersion),
_steps: []
}
migrationChunks.push(chunk)
// Add studio migration steps from blueprint:
_.each(bp.studioMigrations, (step) => {
allMigrationSteps.push(prefixIdsOnStep('blueprint_' + blueprint._id + '_studio_' + studio._id + '_', {
id: step.id,
overrideSteps: step.overrideSteps,
validate: step.validate,
canBeRunAutomatically: step.canBeRunAutomatically,
migrate: step.migrate,
input: step.input,
dependOnResultFrom: step.dependOnResultFrom,
version: step.version,
_version: parseVersion(step.version),
_validateResult: false, // to be set later
_rank: rank++,
chunk: chunk
}))
})
})
} else {
// No migrations for system blueprints
}
}
})
// Sort, smallest version first:
allMigrationSteps.sort((a, b) => {
// First, sort by type:
if (a.chunk.sourceType === MigrationStepType.CORE && b.chunk.sourceType !== MigrationStepType.CORE) return -1
if (a.chunk.sourceType !== MigrationStepType.CORE && b.chunk.sourceType === MigrationStepType.CORE) return 1
// if (a.chunk.sourceType === MigrationStepType.SYSTEM && b.chunk.sourceType !== MigrationStepType.SYSTEM) return -1
// if (a.chunk.sourceType !== MigrationStepType.SYSTEM && b.chunk.sourceType === MigrationStepType.SYSTEM) return 1
if (a.chunk.sourceType === MigrationStepType.STUDIO && b.chunk.sourceType !== MigrationStepType.STUDIO) return -1
if (a.chunk.sourceType !== MigrationStepType.STUDIO && b.chunk.sourceType === MigrationStepType.STUDIO) return 1
if (a.chunk.sourceType === MigrationStepType.SHOWSTYLE && b.chunk.sourceType !== MigrationStepType.SHOWSTYLE) return -1
if (a.chunk.sourceType !== MigrationStepType.SHOWSTYLE && b.chunk.sourceType === MigrationStepType.SHOWSTYLE) return 1
// Then, sort by version:
if (semver.gt(a._version, b._version)) return 1
if (semver.lt(a._version, b._version)) return -1
// Lastly, keep ranking:
if (a._rank > b._rank) return 1
if (a._rank < b._rank) return -1
return 0
})
let automaticStepCount: number = 0
let manualStepCount: number = 0
let ignoredStepCount: number = 0
let partialMigration: boolean = false
// Filter steps:
let migrationSteps: {[id: string]: MigrationStepInternal} = {}
let ignoredSteps: {[id: string]: true} = {}
_.each(allMigrationSteps, (step: MigrationStepInternal) => {
if (!step.canBeRunAutomatically && (!step.input || (_.isArray(step.input) && !step.input.length))) throw new Meteor.Error(500, `MigrationStep "${step.id}" is manual, but no input is provided`)
if (partialMigration) return
if (
semver.gt(step._version, step.chunk._dbVersion) && // step version is larger than database version
semver.lte(step._version, step.chunk._targetVersion) // // step version is less than (or equal) to system version
) {
// Step is in play
if (step.overrideSteps) {
// Override / delete other steps
_.each(step.overrideSteps, (overrideId: string) => {
delete migrationSteps[overrideId]
if (ignoredSteps[overrideId]) {
delete ignoredSteps[overrideId]
ignoredStepCount--
}
})
}
if (migrationSteps[step.id] || ignoredSteps[step.id]) throw new Meteor.Error(500, `Error: MigrationStep.id must be unique: "${step.id}"`)
if (step.dependOnResultFrom) {
if (ignoredSteps[step.dependOnResultFrom]) {
// dependent step was ignored, continue then
} else if (migrationSteps[step.dependOnResultFrom]) {
// we gotta pause here
partialMigration = true
return
}
}
// Check if the step can be applied:
try {
if (step.chunk.sourceType === MigrationStepType.CORE) {
let validate = step.validate as ValidateFunctionCore
step._validateResult = validate(false)
} else if (step.chunk.sourceType === MigrationStepType.STUDIO) {
let validate = step.validate as ValidateFunctionStudio
step._validateResult = validate(getMigrationStudioContext(step.chunk), false)
} else if (step.chunk.sourceType === MigrationStepType.SHOWSTYLE) {
let validate = step.validate as ValidateFunctionShowStyle
step._validateResult = validate(getMigrationShowStyleContext(step.chunk),false)
} else throw new Meteor.Error(500, `Unknown step.chunk.sourceType "${step.chunk.sourceType}"`)
} catch (error) {
throw new Meteor.Error(500, `Error in migration step "${step.id}": ${error.reason || error.toString()}`)
}
if (step._validateResult) {
migrationSteps[step.id] = step
} else {
// No need to run step
ignoredSteps[step.id] = true
ignoredStepCount++
}
} else {
// Step is not applicable
}
})
// check if there are any manual steps:
// (this makes an automatic migration impossible)
let manualInputs: Array<MigrationStepInput> = []
let stepsHash: Array<string> = []
_.each(migrationSteps, (step: MigrationStepInternal, id: string) => {
stepsHash.push(step.id)
step.chunk._steps.push(step.id)
if (!step.canBeRunAutomatically) {
manualStepCount++
if (step.input) {
let input: Array<MigrationStepInput> = []
if (_.isArray(step.input)) {
input = []
_.each(step.input, (i) => {
input.push(_.clone(i))
})
} else if (_.isFunction(step.input)) {
if (step.chunk.sourceType === MigrationStepType.CORE) {
let inputFunction = step.input as InputFunctionCore
input = inputFunction()
} else if (step.chunk.sourceType === MigrationStepType.STUDIO) {
let inputFunction = step.input as InputFunctionStudio
input = inputFunction(getMigrationStudioContext(step.chunk))
} else if (step.chunk.sourceType === MigrationStepType.SHOWSTYLE) {
let inputFunction = step.input as InputFunctionShowStyle
input = inputFunction(getMigrationShowStyleContext(step.chunk))
} else throw new Meteor.Error(500, `Unknown step.chunk.sourceType "${step.chunk.sourceType}"`)
}
if (input.length) {
_.each(input, (i) => {
if (i.label && _.isString(step._validateResult)) {
i.label = (i.label + '').replace(/\$validation/g, step._validateResult)
}
if (i.description && _.isString(step._validateResult)) {
i.description = (i.description + '').replace(/\$validation/g, step._validateResult)
}
manualInputs.push(_.extend({}, i, {
stepId: step.id
}))
})
}
}
} else {
automaticStepCount++
}
})
// Only return the chunks which has steps in them:
const activeChunks = (
returnAllChunks ?
migrationChunks :
_.filter(migrationChunks, (chunk) => {
return chunk._steps.length > 0
})
)
const hash = getHash(stepsHash.join(','))
const steps = _.values(migrationSteps)
if (steps.length > 0) migrationNeeded = true
return {
hash: hash,
chunks: activeChunks,
steps: steps,
migrationNeeded: migrationNeeded,
automaticStepCount: automaticStepCount,
manualStepCount: manualStepCount,
ignoredStepCount: ignoredStepCount,
manualInputs: manualInputs,
partialMigration: partialMigration
}
}
function prefixIdsOnStep (prefix: string, step: MigrationStepInternal): MigrationStepInternal {
step.id = prefix + step.id
if (step.overrideSteps) {
step.overrideSteps = _.map(step.overrideSteps, (override) => {
return prefix + override
})
}
if (step.dependOnResultFrom) {
step.dependOnResultFrom = prefix + step.dependOnResultFrom
}
return step
}
export function runMigration (
chunks: Array<MigrationChunk>,
hash: string,
inputResults: Array<MigrationStepInputResult>,
isFirstOfPartialMigrations: boolean = true,
chunksLeft: number = 20
): RunMigrationResult {
if (chunksLeft < 0) {
logger.error(`Migration: Bailing out, looks like we're in a loop`)
throw new Meteor.Error(500, 'Infinite loop in migrations')
}
logger.info(`Migration: Starting`)
// logger.info(`Migration: Starting, from "${baseVersion}" to "${targetVersion}".`)
// Verify the input:
let migration = prepareMigration(true)
let manualInputsWithUserPrompt = _.filter(migration.manualInputs, (manualInput) => {
return !!(manualInput.stepId && manualInput.attribute)
})
if (migration.hash !== hash) throw new Meteor.Error(500, `Migration input hash differ from expected: "${hash}", "${migration.hash}"`)
if (manualInputsWithUserPrompt.length !== inputResults.length) {
throw new Meteor.Error(500, `Migration manualInput lengths differ from expected: "${inputResults.length}", "${migration.manualInputs.length}"`)
}
// Check that chunks match:
let unmatchedChunk = _.find(migration.chunks, (migrationChunk) => {
return !_.find(chunks, (chunk) => {
return _.isEqual(_.omit(chunk, ['_steps']), _.omit(migrationChunk, ['_steps']))
})
})
if (unmatchedChunk) throw new Meteor.Error(500, `Migration input chunks differ from expected, chunk "${JSON.stringify(unmatchedChunk)}" not found in input`)
unmatchedChunk = _.find(chunks, (chunk) => {
return !_.find(migration.chunks, (migrationChunk) => {
return _.isEqual(_.omit(chunk, ['_steps']), _.omit(migrationChunk, ['_steps']))
})
})
if (unmatchedChunk) throw new Meteor.Error(500, `Migration input chunks differ from expected, chunk in input "${JSON.stringify(unmatchedChunk)}" not found in migration.chunks`)
if (migration.chunks.length !== chunks.length) throw new Meteor.Error(500, `Migration input chunk lengths differ`)
_.each(migration.chunks, (chunk) => {
logger.info(`Migration: Chunk: ${chunk.sourceType}, ${chunk.sourceName}, from ${chunk._dbVersion} to ${chunk._targetVersion}`)
})
let warningMessages: Array<string> = []
let snapshotId: string = ''
if (isFirstOfPartialMigrations) { // First, take a system snapshot:
let system = getCoreSystem()
if (system && system.storePath) {
try {
snapshotId = storeSystemSnapshot(null, `Automatic, taken before migration`)
} catch (e) {
warningMessages.push(`Error when taking snapshot:${e.toString()}`)
logger.error(e)
}
}
}
logger.info(`Migration: ${migration.automaticStepCount} automatic and ${migration.manualStepCount} manual steps (${migration.ignoredStepCount} ignored).`)
logger.debug(inputResults)
_.each(migration.steps, (step: MigrationStepInternal) => {
try {
// Prepare input from user
let stepInput: MigrationStepInputFilteredResult = {}
_.each(inputResults, (ir) => {
if (ir.stepId === step.id) {
stepInput[ir.attribute] = ir.value
}
})
// Run the migration script
if (step.migrate !== undefined) {
logger.info(`Running migration step "${step.id}"`)
if (step.chunk.sourceType === MigrationStepType.CORE) {
let migration = step.migrate as MigrateFunctionCore
migration(stepInput)
} else if (step.chunk.sourceType === MigrationStepType.STUDIO) {
let migration = step.migrate as MigrateFunctionStudio
migration(getMigrationStudioContext(step.chunk), stepInput)
} else if (step.chunk.sourceType === MigrationStepType.SHOWSTYLE) {
let migration = step.migrate as MigrateFunctionShowStyle
migration(getMigrationShowStyleContext(step.chunk), stepInput)
} else throw new Meteor.Error(500, `Unknown step.chunk.sourceType "${step.chunk.sourceType}"`)
}
// After migration, run the validation again
// Since the migration should be done by now, the validate should return true
let validateMessage: string | boolean = false
if (step.chunk.sourceType === MigrationStepType.CORE) {
let validate = step.validate as ValidateFunctionCore
validateMessage = validate(true)
} else if (step.chunk.sourceType === MigrationStepType.STUDIO) {
let validate = step.validate as ValidateFunctionStudio
validateMessage = validate(getMigrationStudioContext(step.chunk), true)
} else if (step.chunk.sourceType === MigrationStepType.SHOWSTYLE) {
let validate = step.validate as ValidateFunctionShowStyle
validateMessage = validate(getMigrationShowStyleContext(step.chunk),true)
} else throw new Meteor.Error(500, `Unknown step.chunk.sourceType "${step.chunk.sourceType}"`)
// let validate = step.validate as ValidateFunctionCore
// let validateMessage: string | boolean = validate(true)
if (validateMessage) {
// Something's not right
let msg = `Step "${step.id}": Something went wrong, validation didn't approve of the changes. The changes have been applied, but might need to be confirmed.`
if (validateMessage !== true && _.isString(validateMessage)) {
msg += ` (Validation error: ${validateMessage})`
}
warningMessages.push(msg)
}
} catch (e) {
logger.error(`Error in Migration step ${step.id}: ${e}`)
logger.error(e.stack ? e.stack : e.toString())
warningMessages.push(`Internal server error in step ${step.id}`)
}
})
let migrationCompleted: boolean = false
if (migration.manualStepCount === 0 && !warningMessages.length) { // continue automatically with the next batch
logger.info('Migration: Automatically continuing with next batch..')
migration.partialMigration = false
const s = getMigrationStatus()
if (s.migration.automaticStepCount > 0 || s.migration.manualStepCount > 0) {
try {
const res = runMigration(s.migration.chunks, s.migration.hash, inputResults, false, chunksLeft - 1)
if (res.migrationCompleted) {
return res
}
_.each(res.warnings, w => warningMessages.push(w))
} catch (e) {
warningMessages.push(`When running next chunk: ${e}`)
migration.partialMigration = true
}
}
}
if (!migration.partialMigration && !warningMessages.length) {
// if there are no warning messages, we can complete the migration right away:
logger.info(`Migration: Completing...`)
completeMigration(migration.chunks)
migrationCompleted = true
}
_.each(warningMessages, (str) => {
logger.warn(`Migration: ${str}`)
})
logger.info(`Migration: End`)
return {
migrationCompleted: migrationCompleted,
partialMigration: migration.partialMigration,
warnings: warningMessages,
snapshot: snapshotId
}
}
function completeMigration (chunks: Array<MigrationChunk>) {
_.each(chunks, (chunk) => {
if (chunk.sourceType === MigrationStepType.CORE) {
setCoreSystemVersion(chunk._targetVersion)
} else if (
chunk.sourceType === MigrationStepType.STUDIO ||
chunk.sourceType === MigrationStepType.SHOWSTYLE
) {
if (!chunk.blueprintId) throw new Meteor.Error(500, `chunk.blueprintId missing!`)
if (!chunk.sourceId) throw new Meteor.Error(500, `chunk.sourceId missing!`)
let blueprint = Blueprints.findOne(chunk.blueprintId)
if (!blueprint) throw new Meteor.Error(404, `Blueprint "${chunk.blueprintId}" not found!`)
let m: any = {}
if (chunk.sourceType === MigrationStepType.STUDIO) {
logger.info(`Updating Blueprint "${chunk.sourceName}" version, from "${blueprint.databaseVersion.studio[chunk.sourceId]}" to "${chunk._targetVersion}".`)
m[`databaseVersion.studio.${chunk.sourceId}`] = chunk._targetVersion
} else if (chunk.sourceType === MigrationStepType.SHOWSTYLE) {
logger.info(`Updating Blueprint "${chunk.sourceName}" version, from "${blueprint.databaseVersion.showStyle[chunk.sourceId]}" to "${chunk._targetVersion}".`)
m[`databaseVersion.showStyle.${chunk.sourceId}`] = chunk._targetVersion
} else throw new Meteor.Error(500, `Bad chunk.sourcetype: "${chunk.sourceType}"`)
Blueprints.update(chunk.blueprintId, { $set: m })
} else throw new Meteor.Error(500, `Unknown chunk.sourcetype: "${chunk.sourceType}"`)
})
}
export function updateDatabaseVersion (targetVersionStr: string) {
let targetVersion = parseVersion(targetVersionStr)
setCoreSystemVersion(targetVersion)
}
export function updateDatabaseVersionToSystem () {
updateDatabaseVersion(CURRENT_SYSTEM_VERSION)
}
export function getMigrationStatus (): GetMigrationStatusResult {
let migration = prepareMigration(true)
return {
// databaseVersion: databaseVersion,
// databasePreviousVersion: system.previousVersion,
// systemVersion: systemVersion,
migrationNeeded: migration.migrationNeeded,
migration: {
canDoAutomaticMigration: migration.manualStepCount === 0,
manualInputs: migration.manualInputs,
hash: migration.hash,
chunks: migration.chunks,
automaticStepCount: migration.automaticStepCount,
manualStepCount: migration.manualStepCount,
ignoredStepCount: migration.ignoredStepCount,
partialMigration: migration.partialMigration
}
}
}
export function forceMigration (chunks: Array<MigrationChunk>) {
logger.info(`Force migration`)
_.each(chunks, (chunk) => {
logger.info(`Force migration: Chunk: ${chunk.sourceType}, ${chunk.sourceName}, from ${chunk._dbVersion} to ${chunk._targetVersion}`)
})
return completeMigration(chunks)
}
export function resetDatabaseVersions () {
updateDatabaseVersion(GENESIS_SYSTEM_VERSION)
Blueprints.find().forEach((blueprint) => {
Blueprints.update(blueprint._id, {$set: {
databaseVersion: {
studio: {},
showStyle: {}
}
}})
})
}
function getMigrationStudioContext (chunk: MigrationChunk): IMigrationContextStudio {
if (chunk.sourceType !== MigrationStepType.STUDIO) throw new Meteor.Error(500, `wrong chunk.sourceType "${chunk.sourceType}", expected STUDIO`)
if (!chunk.sourceId) throw new Meteor.Error(500, `chunk.sourceId missing`)
let studio = Studios.findOne(chunk.sourceId)
if (!studio) throw new Meteor.Error(404, `Studio "${chunk.sourceId}" not found`)
return new MigrationContextStudio(studio)
}
function getMigrationShowStyleContext (chunk: MigrationChunk): IMigrationContextShowStyle {
if (chunk.sourceType !== MigrationStepType.SHOWSTYLE) throw new Meteor.Error(500, `wrong chunk.sourceType "${chunk.sourceType}", expected SHOWSTYLE`)
if (!chunk.sourceId) throw new Meteor.Error(500, `chunk.sourceId missing`)
let showStyleBase = ShowStyleBases.findOne(chunk.sourceId)
if (!showStyleBase) throw new Meteor.Error(404, `ShowStyleBase "${chunk.sourceId}" not found`)
return new MigrationContextShowStyle(showStyleBase)
}