1+ const groupingTimestampIndexName = 'groupingTimestamp' ;
2+ const groupingTimestampAndGroupHashIndexName = 'groupingTimestampAndGroupHash' ;
3+
4+ module . exports = {
5+ async up ( db ) {
6+ const collections = await db . listCollections ( { } , {
7+ authorizedCollections : true ,
8+ nameOnly : true ,
9+ } ) . toArray ( ) ;
10+
11+ const targetCollections = [ ] ;
12+
13+ collections . forEach ( ( collection ) => {
14+ if ( / d a i l y E v e n t s / . test ( collection . name ) ) {
15+ targetCollections . push ( collection . name ) ;
16+ }
17+ } ) ;
18+
19+ console . log ( `${ targetCollections . length } collections will be updated.` ) ;
20+
21+ let currentCollectionNumber = 1 ;
22+
23+ for ( const collectionName of targetCollections ) {
24+ console . log ( `${ collectionName } in process.` ) ;
25+ console . log ( `${ currentCollectionNumber } of ${ targetCollections . length } in process.` ) ;
26+ try {
27+ const hasGroupingTimestampIndexAlready = await db . collection ( collectionName ) . indexExists ( groupingTimestampIndexName ) ;
28+
29+ if ( ! hasGroupingTimestampIndexAlready ) {
30+ await db . collection ( collectionName ) . createIndex ( {
31+ groupingTimestamp : 1 ,
32+ } , {
33+ name : groupingTimestampIndexName ,
34+ sparse : true ,
35+ background : true ,
36+ } ) ;
37+ console . log ( `Index ${ groupingTimestampIndexName } created for ${ collectionName } ` ) ;
38+ } else {
39+ console . log ( `Index ${ groupingTimestampIndexName } already exists for ${ collectionName } ` ) ;
40+ }
41+
42+ const hasGroupingTimestampAndGroupHashIndexAlready = await db . collection ( collectionName ) . indexExists ( groupingTimestampAndGroupHashIndexName ) ;
43+
44+ if ( ! hasGroupingTimestampAndGroupHashIndexAlready ) {
45+ await db . collection ( collectionName ) . createIndex ( {
46+ groupingTimestamp : 1 ,
47+ groupHash : 1 ,
48+ } , {
49+ name : groupingTimestampAndGroupHashIndexName ,
50+ sparse : true ,
51+ background : true ,
52+ } ) ;
53+ console . log ( `Index ${ groupingTimestampAndGroupHashIndexName } created for ${ collectionName } ` ) ;
54+ } else {
55+ console . log ( `Index ${ groupingTimestampAndGroupHashIndexName } already exists for ${ collectionName } ` ) ;
56+ }
57+ } catch ( error ) {
58+ console . error ( `Error adding indexes to ${ collectionName } :` , error ) ;
59+ }
60+ currentCollectionNumber ++ ;
61+ }
62+ } ,
63+ async down ( db ) {
64+ const collections = await db . listCollections ( { } , {
65+ authorizedCollections : true ,
66+ nameOnly : true ,
67+ } ) . toArray ( ) ;
68+
69+ const targetCollections = [ ] ;
70+
71+ collections . forEach ( ( collection ) => {
72+ if ( / d a i l y E v e n t s / . test ( collection . name ) ) {
73+ targetCollections . push ( collection . name ) ;
74+ }
75+ } ) ;
76+
77+ console . log ( `${ targetCollections . length } collections will be updated.` ) ;
78+
79+ let currentCollectionNumber = 1 ;
80+
81+ for ( const collectionName of targetCollections ) {
82+ console . log ( `${ collectionName } in process.` ) ;
83+ console . log ( `${ currentCollectionNumber } of ${ targetCollections . length } in process.` ) ;
84+
85+ try {
86+ const hasGroupingTimestampIndexAlready = await db . collection ( collectionName ) . indexExists ( groupingTimestampIndexName ) ;
87+ if ( hasGroupingTimestampIndexAlready ) {
88+ await db . collection ( collectionName ) . dropIndex ( groupingTimestampIndexName ) ;
89+ console . log ( `Index ${ groupingTimestampIndexName } dropped for ${ collectionName } ` ) ;
90+ } else {
91+ console . log ( `Index ${ groupingTimestampIndexName } does not exist for ${ collectionName } , skipping drop.` ) ;
92+ }
93+
94+ const hasGroupingTimestampAndGroupHashIndexAlready = await db . collection ( collectionName ) . indexExists ( groupingTimestampAndGroupHashIndexName ) ;
95+ if ( hasGroupingTimestampAndGroupHashIndexAlready ) {
96+ await db . collection ( collectionName ) . dropIndex ( groupingTimestampAndGroupHashIndexName ) ;
97+ console . log ( `Index ${ groupingTimestampAndGroupHashIndexName } dropped for ${ collectionName } ` ) ;
98+ } else {
99+ console . log ( `Index ${ groupingTimestampAndGroupHashIndexName } does not exist for ${ collectionName } , skipping drop.` ) ;
100+ }
101+ } catch ( error ) {
102+ console . error ( `Error dropping indexes from ${ collectionName } :` , error ) ;
103+ }
104+ currentCollectionNumber ++ ;
105+ }
106+ }
107+ }
0 commit comments