11/**
2- * This migration updates date from format `dd-mm-YYYY` to midnight unixtime
3- * so that each client with different timezone could convert it to local time
2+ * This migration sets timestamp for event repetitions if it was omitted because it's the same as original event one
43 */
54module . exports = {
6- async up ( db ) {
5+ async up ( db , client ) {
76 const collections = await db . listCollections ( { } , {
87 authorizedCollections : true ,
98 nameOnly : true ,
@@ -19,25 +18,33 @@ module.exports = {
1918 }
2019 } ) ;
2120
22- for ( const projectId of projectIds ) {
23- const originalEvents = await db . collection ( `${ EVENTS } :${ projectId } ` ) . find ( { } ) . toArray ( ) ;
24- const repetitions = await db . collection ( `${ REPETITIONS } :${ projectId } ` ) . find ( {
25- 'payload.timestamp' : { $eq : null }
26- } ) . toArray ( ) ;
21+ const session = client . startSession ( ) ;
2722
28- for ( const event of originalEvents ) {
29- const eventRepetitions = repetitions . filter ( rep => rep . groupHash === event . groupHash ) ;
23+ try {
24+ session . withTransaction ( async ( ) => {
25+ for ( const projectId of projectIds ) {
26+ const originalEvents = await db . collection ( `${ EVENTS } :${ projectId } ` ) . find ( { } ) . toArray ( ) ;
27+ const repetitions = await db . collection ( `${ REPETITIONS } :${ projectId } ` ) . find ( {
28+ 'payload.timestamp' : { $eq : null }
29+ } ) . toArray ( ) ;
3030
31- for ( const repetition of eventRepetitions ) {
32- await db . collection ( `${ REPETITIONS } :${ projectId } ` ) . updateOne ( {
33- _id : repetition . _id ,
34- } , {
35- $set : {
36- 'payload.timestamp' : event . payload . timestamp ,
37- } ,
38- } ) ;
31+ for ( const event of originalEvents ) {
32+ const eventRepetitions = repetitions . filter ( rep => rep . groupHash === event . groupHash ) ;
33+
34+ for ( const repetition of eventRepetitions ) {
35+ await db . collection ( `${ REPETITIONS } :${ projectId } ` ) . updateOne ( {
36+ _id : repetition . _id ,
37+ } , {
38+ $set : {
39+ 'payload.timestamp' : event . payload . timestamp ,
40+ } ,
41+ } ) ;
42+ }
43+ }
3944 }
40- }
45+ } )
46+ } finally {
47+ session . endSession ( ) ;
4148 }
4249 } ,
4350} ;
0 commit comments