From 922cb106c2fe22ee41b3add729bdc7603a3680db Mon Sep 17 00:00:00 2001 From: Joao-vi Date: Wed, 16 Oct 2024 14:15:16 -0400 Subject: [PATCH] add migrations to execute reindex on elasticsearch --- .../index.ts | 17 +++++++++ ...sist_filename_with_fullText_object.spec.ts | 37 +++++++++++++++++++ .../specs/fixtures.ts | 13 +++++++ .../types.ts | 13 +++++++ 4 files changed, 80 insertions(+) create mode 100644 app/api/migrations/migrations/169-reindex_persist_filename_with_fullText_object/index.ts create mode 100644 app/api/migrations/migrations/169-reindex_persist_filename_with_fullText_object/specs/169-reindex_persist_filename_with_fullText_object.spec.ts create mode 100644 app/api/migrations/migrations/169-reindex_persist_filename_with_fullText_object/specs/fixtures.ts create mode 100644 app/api/migrations/migrations/169-reindex_persist_filename_with_fullText_object/types.ts diff --git a/app/api/migrations/migrations/169-reindex_persist_filename_with_fullText_object/index.ts b/app/api/migrations/migrations/169-reindex_persist_filename_with_fullText_object/index.ts new file mode 100644 index 0000000000..2d46a71aac --- /dev/null +++ b/app/api/migrations/migrations/169-reindex_persist_filename_with_fullText_object/index.ts @@ -0,0 +1,17 @@ +import { Db } from 'mongodb'; + +export default { + delta: 169, + + name: 'reindex_persist_filename_with_fullText_object', + + description: + "We're now indexing document.filename within the fullText object on elasticsearch, this is usefull because on search/v2 endpoint we need to return which filename each text snippet belongs to.", + + reindex: true, + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + async up(db: Db) { + process.stdout.write(`${this.name}...\r\n`); + }, +}; diff --git a/app/api/migrations/migrations/169-reindex_persist_filename_with_fullText_object/specs/169-reindex_persist_filename_with_fullText_object.spec.ts b/app/api/migrations/migrations/169-reindex_persist_filename_with_fullText_object/specs/169-reindex_persist_filename_with_fullText_object.spec.ts new file mode 100644 index 0000000000..30a56fadf0 --- /dev/null +++ b/app/api/migrations/migrations/169-reindex_persist_filename_with_fullText_object/specs/169-reindex_persist_filename_with_fullText_object.spec.ts @@ -0,0 +1,37 @@ +import { Db } from 'mongodb'; + +import testingDB from 'api/utils/testing_db'; +import migration from '../index'; +import { Fixture } from '../types'; +import { fixtures } from './fixtures'; + +let db: Db | null; + +const initTest = async (fixture: Fixture) => { + await testingDB.setupFixturesAndContext(fixture); + db = testingDB.mongodb!; + await migration.up(db); +}; + +beforeAll(async () => { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + jest.spyOn(process.stdout, 'write').mockImplementation((str: string | Uint8Array) => true); +}); + +afterAll(async () => { + await testingDB.tearDown(); +}); + +describe('migration test', () => { + beforeAll(async () => { + await initTest(fixtures); + }); + + it('should have a delta number', () => { + expect(migration.delta).toBe(169); + }); + + it('should check if a reindex is needed', async () => { + expect(migration.reindex).toBe(true); + }); +}); diff --git a/app/api/migrations/migrations/169-reindex_persist_filename_with_fullText_object/specs/fixtures.ts b/app/api/migrations/migrations/169-reindex_persist_filename_with_fullText_object/specs/fixtures.ts new file mode 100644 index 0000000000..e0e2c34cd0 --- /dev/null +++ b/app/api/migrations/migrations/169-reindex_persist_filename_with_fullText_object/specs/fixtures.ts @@ -0,0 +1,13 @@ +import { ObjectId } from 'mongodb'; +import { Fixture } from '../types'; + +const fixtures: Fixture = { + entities: [ + { + _id: new ObjectId(), + title: 'test_doc', + }, + ], +}; + +export { fixtures }; diff --git a/app/api/migrations/migrations/169-reindex_persist_filename_with_fullText_object/types.ts b/app/api/migrations/migrations/169-reindex_persist_filename_with_fullText_object/types.ts new file mode 100644 index 0000000000..f98b5fcc36 --- /dev/null +++ b/app/api/migrations/migrations/169-reindex_persist_filename_with_fullText_object/types.ts @@ -0,0 +1,13 @@ +import { ObjectId } from 'mongodb'; + +interface Entity { + _id: ObjectId; + title: string; + [k: string]: unknown | undefined; +} + +interface Fixture { + entities: Entity[]; +} + +export type { Entity, Fixture };