Skip to content

Commit 8b5b5b2

Browse files
committed
feat(perf): added index for events timestamo
1 parent a26b106 commit 8b5b5b2

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

0 commit comments

Comments
 (0)