Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
58 changes: 58 additions & 0 deletions migrations/20261014000000-add-repetition-group-release-index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const { MongoClient } = require('mongodb');

const REPETITIONS_INDEX_NAME = 'groupHashAndRelease';

/**
* Run an operation against the events database.
*
* @param {Function} callback - operation to run
*/
async function withEventsDatabase(callback) {
const url = process.env.MONGO_EVENTS_DB_URL;

if (!url) {
throw new Error('MONGO_EVENTS_DB_URL must be set to migrate repetition indexes');
}

const client = new MongoClient(url);

try {
await client.connect();

return await callback(client.db());
} finally {
await client.close();
}
}

module.exports = {
async up() {
await withEventsDatabase(async db => {
const collections = await db.listCollections({ name: /^repetitions:/ }, { nameOnly: true }).toArray();

for (const { name } of collections) {
await db.collection(name).createIndex({
groupHash: 1,
release: 1,
}, {
name: REPETITIONS_INDEX_NAME,
});
}
});
},

async down() {
await withEventsDatabase(async db => {
const collections = await db.listCollections({ name: /^repetitions:/ }, { nameOnly: true }).toArray();

for (const { name } of collections) {
await db.collection(name).dropIndex(REPETITIONS_INDEX_NAME)
.catch(error => {
if (error.codeName !== 'IndexNotFound' && error.code !== 27) {
throw error;
}
});
}
});
},
};
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.5.16",
"version": "1.6.0",
"main": "index.ts",
"license": "BUSL-1.1",
"scripts": {
Expand Down
11 changes: 11 additions & 0 deletions src/resolvers/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const { GitHubService } = require('../integrations/github/service');

const EVENTS_GROUP_HASH_INDEX_NAME = 'groupHashUnique';
const REPETITIONS_GROUP_HASH_INDEX_NAME = 'groupHash_hashed';
const REPETITIONS_GROUP_HASH_AND_RELEASE_INDEX_NAME = 'groupHashAndRelease';
const REPETITIONS_USER_ID_INDEX_NAME = 'userId';
const REPETITIONS_TIMESTAMP_INDEX_NAME = 'timestamp';
const EVENTS_TIMESTAMP_INDEX_NAME = 'timestamp';
Expand Down Expand Up @@ -409,6 +410,16 @@ module.exports = {
name: REPETITIONS_GROUP_HASH_INDEX_NAME,
});

/**
* Used by the release validator to find the releases in which each event occurred.
*/
await projectRepetitionsEventsCollection.createIndex({
groupHash: 1,
release: 1,
}, {
name: REPETITIONS_GROUP_HASH_AND_RELEASE_INDEX_NAME,
});

await projectRepetitionsEventsCollection.createIndex({
'payload.user.id': 1,
}, {
Expand Down
5 changes: 5 additions & 0 deletions test/resolvers/project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,11 @@ describe('Project Resolver - createProject', () => {
context
);

expect(repetitionsCollectionMock.createIndex).toHaveBeenCalledWith(
{ groupHash: 1, release: 1 },
{ name: 'groupHashAndRelease' }
);

expect(repetitionsCollectionMock.createIndex).toHaveBeenCalledWith(
{ timestamp: 1 },
{ name: 'timestamp', sparse: true }
Expand Down
Loading