Skip to content

Commit 8591a37

Browse files
authored
feat(opt): added index for dailyEvent for groupingTimestamp, lastRepetitionTime и _id for sorting for dailyEventPortion (#473)
1 parent 1fef139 commit 8591a37

1 file changed

Lines changed: 94 additions & 0 deletions

File tree

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* Create compound index for all collections dailyEvents:projectId on
3+
* (groupingTimestamp, lastRepetitionTime, _id desc)
4+
*/
5+
const indexName = 'groupingTimestampAndLastRepetitionTimeAndId';
6+
7+
module.exports = {
8+
async up(db) {
9+
const collections = await db.listCollections({}, {
10+
authorizedCollections: true,
11+
nameOnly: true,
12+
}).toArray();
13+
14+
const targetCollections = [];
15+
16+
collections.forEach((collection) => {
17+
if (/dailyEvents:/.test(collection.name)) {
18+
targetCollections.push(collection.name);
19+
}
20+
});
21+
22+
console.log(`${targetCollections.length} dailyEvents collections will be updated.`);
23+
24+
let currentCollectionNumber = 1;
25+
26+
for (const collectionName of targetCollections) {
27+
console.log(`${collectionName} in process.`);
28+
console.log(`${currentCollectionNumber} of ${targetCollections.length} in process.`);
29+
30+
try {
31+
const hasIndexAlready = await db.collection(collectionName).indexExists(indexName);
32+
33+
if (!hasIndexAlready) {
34+
await db.collection(collectionName).createIndex({
35+
groupingTimestamp: -1,
36+
lastRepetitionTime: -1,
37+
_id: -1,
38+
}, {
39+
name: indexName,
40+
background: true,
41+
});
42+
console.log(`Index ${indexName} created for ${collectionName}`);
43+
} else {
44+
console.log(`Index ${indexName} already exists for ${collectionName}`);
45+
}
46+
} catch (error) {
47+
console.error(`Error adding index to ${collectionName}:`, error);
48+
}
49+
50+
currentCollectionNumber++;
51+
}
52+
},
53+
54+
async down(db) {
55+
const collections = await db.listCollections({}, {
56+
authorizedCollections: true,
57+
nameOnly: true,
58+
}).toArray();
59+
60+
const targetCollections = [];
61+
62+
collections.forEach((collection) => {
63+
if (/dailyEvents:/.test(collection.name)) {
64+
targetCollections.push(collection.name);
65+
}
66+
});
67+
68+
console.log(`${targetCollections.length} dailyEvents collections will be updated.`);
69+
70+
let currentCollectionNumber = 1;
71+
72+
for (const collectionName of targetCollections) {
73+
console.log(`${collectionName} in process.`);
74+
console.log(`${currentCollectionNumber} of ${targetCollections.length} in process.`);
75+
76+
try {
77+
const hasIndexAlready = await db.collection(collectionName).indexExists(indexName);
78+
79+
if (hasIndexAlready) {
80+
await db.collection(collectionName).dropIndex(indexName);
81+
console.log(`Index ${indexName} dropped for ${collectionName}`);
82+
} else {
83+
console.log(`Index ${indexName} does not exist for ${collectionName}, skipping drop.`);
84+
}
85+
} catch (error) {
86+
console.error(`Error dropping index from ${collectionName}:`, error);
87+
}
88+
89+
currentCollectionNumber++;
90+
}
91+
}
92+
};
93+
94+

0 commit comments

Comments
 (0)