1
1
import { addMigrationSteps } from './databaseMigration'
2
2
import { CURRENT_SYSTEM_VERSION } from './currentSystemVersion'
3
- import { AdLibActions , AdLibPieces , ExpectedPackages , Pieces } from '../collections'
4
- import { ExpectedPackageDBType } from '@sofie-automation/corelib/dist/dataModel/ExpectedPackages'
5
- import _ from 'underscore'
6
- import {
7
- AdLibActionId ,
8
- PartId ,
9
- PieceId ,
10
- RundownBaselineAdLibActionId ,
11
- } from '@sofie-automation/corelib/dist/dataModel/Ids'
12
- import { Piece } from '@sofie-automation/corelib/dist/dataModel/Piece'
13
- import { AdLibPiece } from '@sofie-automation/corelib/dist/dataModel/AdLibPiece'
14
- import { AdLibAction } from '@sofie-automation/corelib/dist/dataModel/AdlibAction'
15
- import { protectString } from '@sofie-automation/corelib/dist/protectedString'
16
3
17
4
/*
18
5
* **************************************************************************************
@@ -24,94 +11,6 @@ import { protectString } from '@sofie-automation/corelib/dist/protectedString'
24
11
* **************************************************************************************
25
12
*/
26
13
27
- const EXPECTED_PACKAGE_TYPES_ADDED_PART_ID = [
28
- ExpectedPackageDBType . PIECE ,
29
- ExpectedPackageDBType . ADLIB_PIECE ,
30
- ExpectedPackageDBType . ADLIB_ACTION ,
31
- ]
32
-
33
14
export const addSteps = addMigrationSteps ( CURRENT_SYSTEM_VERSION , [
34
15
// Add your migration here
35
-
36
- {
37
- id : `ExpectedPackageDBFromAdLibAction and ExpectedPackageDBFromPiece add partId` ,
38
- canBeRunAutomatically : true ,
39
- validate : async ( ) => {
40
- const objectCount = await ExpectedPackages . countDocuments ( {
41
- fromPieceType : { $in : EXPECTED_PACKAGE_TYPES_ADDED_PART_ID } ,
42
- partId : { $exists : false } ,
43
- } )
44
-
45
- if ( objectCount ) {
46
- return `object needs to be updated`
47
- }
48
- return false
49
- } ,
50
- migrate : async ( ) => {
51
- const objects = await ExpectedPackages . findFetchAsync ( {
52
- fromPieceType : { $in : EXPECTED_PACKAGE_TYPES_ADDED_PART_ID } ,
53
- partId : { $exists : false } ,
54
- } )
55
-
56
- const neededPieceIds : Array < PieceId | AdLibActionId | RundownBaselineAdLibActionId > = _ . compact (
57
- objects . map ( ( obj ) => obj . pieceId )
58
- )
59
- const [ pieces , adlibPieces , adlibActions ] = await Promise . all ( [
60
- Pieces . findFetchAsync (
61
- {
62
- _id : { $in : neededPieceIds as PieceId [ ] } ,
63
- } ,
64
- {
65
- projection : {
66
- _id : 1 ,
67
- startPartId : 1 ,
68
- } ,
69
- }
70
- ) as Promise < Pick < Piece , '_id' | 'startPartId' > [ ] > ,
71
- AdLibPieces . findFetchAsync (
72
- {
73
- _id : { $in : neededPieceIds as PieceId [ ] } ,
74
- } ,
75
- {
76
- projection : {
77
- _id : 1 ,
78
- partId : 1 ,
79
- } ,
80
- }
81
- ) as Promise < Pick < AdLibPiece , '_id' | 'partId' > [ ] > ,
82
- AdLibActions . findFetchAsync (
83
- {
84
- _id : { $in : neededPieceIds as AdLibActionId [ ] } ,
85
- } ,
86
- {
87
- projection : {
88
- _id : 1 ,
89
- partId : 1 ,
90
- } ,
91
- }
92
- ) as Promise < Pick < AdLibAction , '_id' | 'partId' > [ ] > ,
93
- ] )
94
-
95
- const partIdLookup = new Map < PieceId | AdLibActionId | RundownBaselineAdLibActionId , PartId > ( )
96
- for ( const piece of pieces ) {
97
- partIdLookup . set ( piece . _id , piece . startPartId )
98
- }
99
- for ( const adlib of adlibPieces ) {
100
- if ( adlib . partId ) partIdLookup . set ( adlib . _id , adlib . partId )
101
- }
102
- for ( const action of adlibActions ) {
103
- partIdLookup . set ( action . _id , action . partId )
104
- }
105
-
106
- for ( const expectedPackage of objects ) {
107
- if ( ! expectedPackage . pieceId ) continue
108
-
109
- await ExpectedPackages . mutableCollection . updateAsync ( expectedPackage . _id , {
110
- $set : {
111
- partId : partIdLookup . get ( expectedPackage . pieceId ) ?? protectString ( '' ) ,
112
- } ,
113
- } )
114
- }
115
- } ,
116
- } ,
117
16
] )
0 commit comments