Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions migrations/2025101022010-add-daily-events-indexes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
const groupingTimestampIndexName = 'groupingTimestamp';
const groupingTimestampAndGroupHashIndexName = 'groupingTimestampAndGroupHash';

module.exports = {
async up(db) {
const collections = await db.listCollections({}, {
authorizedCollections: true,
nameOnly: true,
}).toArray();

const targetCollections = [];

collections.forEach((collection) => {
if (/dailyEvents/.test(collection.name)) {
targetCollections.push(collection.name);
}
});

console.log(`${targetCollections.length} collections will be updated.`);

let currentCollectionNumber = 1;

for (const collectionName of targetCollections) {
console.log(`${collectionName} in process.`);
console.log(`${currentCollectionNumber} of ${targetCollections.length} in process.`);
try {
const hasGroupingTimestampIndexAlready = await db.collection(collectionName).indexExists(groupingTimestampIndexName);

if (!hasGroupingTimestampIndexAlready) {
await db.collection(collectionName).createIndex({
groupingTimestamp: 1,
}, {
name: groupingTimestampIndexName,
sparse: true,
background: true,
});
console.log(`Index ${groupingTimestampIndexName} created for ${collectionName}`);
} else {
console.log(`Index ${groupingTimestampIndexName} already exists for ${collectionName}`);
}

const hasGroupingTimestampAndGroupHashIndexAlready = await db.collection(collectionName).indexExists(groupingTimestampAndGroupHashIndexName);

if (!hasGroupingTimestampAndGroupHashIndexAlready) {
await db.collection(collectionName).createIndex({
groupingTimestamp: 1,
groupHash: 1,
}, {
name: groupingTimestampAndGroupHashIndexName,
sparse: true,
background: true,
});
console.log(`Index ${groupingTimestampAndGroupHashIndexName} created for ${collectionName}`);
} else {
console.log(`Index ${groupingTimestampAndGroupHashIndexName} already exists for ${collectionName}`);
}
} catch (error) {
console.error(`Error adding indexes to ${collectionName}:`, error);
}
currentCollectionNumber++;
}
},
async down(db) {
const collections = await db.listCollections({}, {
authorizedCollections: true,
nameOnly: true,
}).toArray();

const targetCollections = [];

collections.forEach((collection) => {
if (/dailyEvents/.test(collection.name)) {
targetCollections.push(collection.name);
}
});

console.log(`${targetCollections.length} collections will be updated.`);

let currentCollectionNumber = 1;

for (const collectionName of targetCollections) {
console.log(`${collectionName} in process.`);
console.log(`${currentCollectionNumber} of ${targetCollections.length} in process.`);

try {
const hasGroupingTimestampIndexAlready = await db.collection(collectionName).indexExists(groupingTimestampIndexName);
if (hasGroupingTimestampIndexAlready) {
await db.collection(collectionName).dropIndex(groupingTimestampIndexName);
console.log(`Index ${groupingTimestampIndexName} dropped for ${collectionName}`);
} else {
console.log(`Index ${groupingTimestampIndexName} does not exist for ${collectionName}, skipping drop.`);
}

const hasGroupingTimestampAndGroupHashIndexAlready = await db.collection(collectionName).indexExists(groupingTimestampAndGroupHashIndexName);
if (hasGroupingTimestampAndGroupHashIndexAlready) {
await db.collection(collectionName).dropIndex(groupingTimestampAndGroupHashIndexName);
console.log(`Index ${groupingTimestampAndGroupHashIndexName} dropped for ${collectionName}`);
} else {
console.log(`Index ${groupingTimestampAndGroupHashIndexName} does not exist for ${collectionName}, skipping drop.`);
}
} catch (error) {
console.error(`Error dropping indexes from ${collectionName}:`, error);
}
currentCollectionNumber++;
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hawk.api",
"version": "1.1.42",
"version": "1.1.43",
"main": "index.ts",
"license": "BUSL-1.1",
"scripts": {
Expand Down
15 changes: 14 additions & 1 deletion src/resolvers/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,20 @@ module.exports = {

const projectRepetitionsEventsCollection = await mongo.databases.events.createCollection('repetitions:' + project._id);

await mongo.databases.events.createCollection('dailyEvents:' + project._id);
const projectDailyEventsCollection = await mongo.databases.events.createCollection('dailyEvents:' + project._id);

await projectDailyEventsCollection.createIndex({
groupingTimestamp: 1,
}, {
name: 'groupingTimestamp',
});

await projectDailyEventsCollection.createIndex({
groupingTimestamp: 1,
groupHash: 1,
}, {
name: 'groupingTimestampAndGroupHash',
});

await projectEventsCollection.createIndex({
groupHash: 1,
Expand Down
Loading