From 0da15753c07bb66a4f387af864a0ff6dbf7022cb Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 20 Nov 2026 21:53:35 +0300 Subject: [PATCH] fix(events): replace repetitions groupHash_hashed index with groupHashAndId (#692) (#693) * fix(events): replace repetitions groupHash_hashed index with groupHashAndId * Bump version up to 1.7.1 * fix(migrations): simplify IndexNotFound error check * chore(migrations): add progress logging for repetitions index migration --------- Co-authored-by: Peter Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> --- ...000-replace-repetition-group-hash-index.js | 222 ++++++++++++++++++ package.json | 2 +- src/resolvers/project.js | 13 +- test/resolvers/project.test.ts | 10 + 4 files changed, 241 insertions(+), 6 deletions(-) create mode 100644 migrations/20261119000000-replace-repetition-group-hash-index.js diff --git a/migrations/20261119000000-replace-repetition-group-hash-index.js b/migrations/20261119000000-replace-repetition-group-hash-index.js new file mode 100644 index 00000000..d8abf323 --- /dev/null +++ b/migrations/20261119000000-replace-repetition-group-hash-index.js @@ -0,0 +1,222 @@ +const { MongoClient } = require('mongodb'); + +const NEW_REPETITIONS_INDEX_NAME = 'groupHashAndId'; +const OLD_REPETITIONS_INDEX_NAME = 'groupHash_hashed'; + +/** + * Load ANSI formatting helpers from compiled JS when available (prod / built repo), + * falling back to plain pass-through in unbuilt dev environments. + * + * @returns {{ Effect: object, sgr: Function }} + */ +function loadAnsi() { + try { + return require('../src/utils/ansi'); + } catch (_srcError) { + try { + return require('../build/src/utils/ansi'); + } catch (_buildError) { + return { + Effect: {}, + sgr: (message) => message, + }; + } + } +} + +const { Effect, sgr } = loadAnsi(); + +const LOG_PREFIX = sgr('[migration:repetitions-index]', [Effect.ForegroundCyan, Effect.Bold]); + +/** + * Format duration in milliseconds into a human-readable string. + * + * @param {number} durationMs - elapsed time in milliseconds + * @returns {string} formatted duration + */ +function formatDuration(durationMs) { + if (durationMs < 1000) { + return `${durationMs}ms`; + } + + const seconds = durationMs / 1000; + + if (seconds < 60) { + return `${seconds.toFixed(2)}s`; + } + + const minutes = Math.floor(seconds / 60); + const remainingSeconds = (seconds % 60).toFixed(1); + + return `${minutes}m ${remainingSeconds}s`; +} + +/** + * Format collection progress prefix, e.g. "[12/450 2.7%]". + * + * @param {number} index - zero-based collection index + * @param {number} total - total collections count + * @returns {string} formatted progress badge + */ +function formatProgress(index, total) { + const current = index + 1; + const percent = total > 0 ? ((current / total) * 100).toFixed(1) : '100.0'; + + return sgr(`[${current}/${total} ${percent}%]`, Effect.ForegroundGray); +} + +/** + * 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(); + } +} + +/** + * Drop an index if it exists, ignoring IndexNotFound errors. + * + * @param {object} collection - MongoDB collection + * @param {string} indexName - name of the index to drop + * @returns {Promise} true if dropped, false if index did not exist + */ +async function dropIndexIfExists(collection, indexName) { + try { + await collection.dropIndex(indexName); + + return true; + } catch (error) { + if (error.codeName !== 'IndexNotFound') { + throw error; + } + + return false; + } +} + +module.exports = { + async up() { + await withEventsDatabase(async (db) => { + const startedAt = Date.now(); + const collections = await db.listCollections({ name: /^repetitions:/ }, { nameOnly: true }).toArray(); + const total = collections.length; + let droppedCount = 0; + + console.log( + `${LOG_PREFIX} Starting ${sgr('UP', [Effect.ForegroundGreen, Effect.Bold])}: ` + + `found ${sgr(String(total), Effect.ForegroundYellow)} repetitions collections` + ); + + for (let i = 0; i < total; i++) { + const { name } = collections[i]; + const collection = db.collection(name); + const progress = formatProgress(i, total); + const collectionLabel = sgr(name, Effect.ForegroundGreen); + const collectionStartedAt = Date.now(); + + console.log( + `${LOG_PREFIX} ${progress} ${collectionLabel} — creating ${sgr(NEW_REPETITIONS_INDEX_NAME, Effect.ForegroundCyan)}...` + ); + + /** + * Create the compound B-tree index before dropping the hashed one so groupHash queries stay indexed. + */ + await collection.createIndex({ + groupHash: 1, + _id: -1, + }, { + name: NEW_REPETITIONS_INDEX_NAME, + }); + + const wasDropped = await dropIndexIfExists(collection, OLD_REPETITIONS_INDEX_NAME); + + if (wasDropped) { + droppedCount += 1; + } + + const dropStatus = wasDropped + ? `dropped ${sgr(OLD_REPETITIONS_INDEX_NAME, Effect.ForegroundYellow)}` + : sgr(`${OLD_REPETITIONS_INDEX_NAME} not found`, Effect.ForegroundGray); + const elapsed = sgr(formatDuration(Date.now() - collectionStartedAt), Effect.ForegroundGray); + + console.log(`${LOG_PREFIX} ${progress} ${collectionLabel} — done (${dropStatus}) in ${elapsed}`); + } + + const totalElapsed = sgr(formatDuration(Date.now() - startedAt), Effect.ForegroundCyan); + + console.log( + `${LOG_PREFIX} ${sgr('Finished UP', [Effect.ForegroundGreen, Effect.Bold])}: ` + + `processed ${sgr(String(total), Effect.ForegroundYellow)} collections, ` + + `dropped ${sgr(String(droppedCount), Effect.ForegroundYellow)} old indexes in ${totalElapsed}` + ); + }); + }, + + async down() { + await withEventsDatabase(async (db) => { + const startedAt = Date.now(); + const collections = await db.listCollections({ name: /^repetitions:/ }, { nameOnly: true }).toArray(); + const total = collections.length; + let droppedCount = 0; + + console.log( + `${LOG_PREFIX} Starting ${sgr('DOWN', [Effect.ForegroundYellow, Effect.Bold])}: ` + + `found ${sgr(String(total), Effect.ForegroundYellow)} repetitions collections` + ); + + for (let i = 0; i < total; i++) { + const { name } = collections[i]; + const collection = db.collection(name); + const progress = formatProgress(i, total); + const collectionLabel = sgr(name, Effect.ForegroundGreen); + const collectionStartedAt = Date.now(); + + console.log( + `${LOG_PREFIX} ${progress} ${collectionLabel} — restoring ${sgr(OLD_REPETITIONS_INDEX_NAME, Effect.ForegroundCyan)}...` + ); + + await collection.createIndex({ + groupHash: 'hashed', + }, { + name: OLD_REPETITIONS_INDEX_NAME, + }); + + const wasDropped = await dropIndexIfExists(collection, NEW_REPETITIONS_INDEX_NAME); + + if (wasDropped) { + droppedCount += 1; + } + + const dropStatus = wasDropped + ? `dropped ${sgr(NEW_REPETITIONS_INDEX_NAME, Effect.ForegroundYellow)}` + : sgr(`${NEW_REPETITIONS_INDEX_NAME} not found`, Effect.ForegroundGray); + const elapsed = sgr(formatDuration(Date.now() - collectionStartedAt), Effect.ForegroundGray); + + console.log(`${LOG_PREFIX} ${progress} ${collectionLabel} — done (${dropStatus}) in ${elapsed}`); + } + + const totalElapsed = sgr(formatDuration(Date.now() - startedAt), Effect.ForegroundCyan); + + console.log( + `${LOG_PREFIX} ${sgr('Finished DOWN', [Effect.ForegroundYellow, Effect.Bold])}: ` + + `processed ${sgr(String(total), Effect.ForegroundYellow)} collections, ` + + `dropped ${sgr(String(droppedCount), Effect.ForegroundYellow)} indexes in ${totalElapsed}` + ); + }); + }, +}; diff --git a/package.json b/package.json index d5b59fc4..f267e78f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hawk.api", - "version": "1.7.0", + "version": "1.7.1", "main": "index.ts", "license": "BUSL-1.1", "scripts": { diff --git a/src/resolvers/project.js b/src/resolvers/project.js index 2e82f6c0..fc67665b 100644 --- a/src/resolvers/project.js +++ b/src/resolvers/project.js @@ -12,7 +12,7 @@ const ProjectModel = require('../models/project').default; 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_ID_INDEX_NAME = 'groupHashAndId'; const REPETITIONS_GROUP_HASH_AND_RELEASE_INDEX_NAME = 'groupHashAndRelease'; const REPETITIONS_USER_ID_INDEX_NAME = 'userId'; const REPETITIONS_TIMESTAMP_INDEX_NAME = 'timestamp'; @@ -403,11 +403,14 @@ module.exports = { name: EVENTS_GROUP_HASH_INDEX_NAME, }); + /** + * Used to fetch repetitions by groupHash ordered by newest _id (including cursor pagination). + */ await projectRepetitionsEventsCollection.createIndex({ - groupHash: 'hashed', - }, - { - name: REPETITIONS_GROUP_HASH_INDEX_NAME, + groupHash: 1, + _id: -1, + }, { + name: REPETITIONS_GROUP_HASH_AND_ID_INDEX_NAME, }); /** diff --git a/test/resolvers/project.test.ts b/test/resolvers/project.test.ts index ea894eb0..c8dbed0c 100644 --- a/test/resolvers/project.test.ts +++ b/test/resolvers/project.test.ts @@ -596,6 +596,16 @@ describe('Project Resolver - createProject', () => { context ); + expect(repetitionsCollectionMock.createIndex).toHaveBeenCalledWith( + { groupHash: 1, _id: -1 }, + { name: 'groupHashAndId' } + ); + + expect(repetitionsCollectionMock.createIndex).not.toHaveBeenCalledWith( + { groupHash: 'hashed' }, + expect.anything() + ); + expect(repetitionsCollectionMock.createIndex).toHaveBeenCalledWith( { groupHash: 1, release: 1 }, { name: 'groupHashAndRelease' }