Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
perf(db): drop the redundant single-column messages(sessionId) index (#509)
Yudhi Armyndharisaf635481ac91
diff --git a/src/database/migrations/1781600000000-DropRedundantMessagesSessionIdIndex.ts b/src/database/migrations/1781600000000-DropRedundantMessagesSessionIdIndex.tsnew file mode 100644index 000000000..c0ca817a4--- /dev/null+++ b/src/database/migrations/1781600000000-DropRedundantMessagesSessionIdIndex.ts@@ -0,0 +1,23 @@+import { MigrationInterface, QueryRunner } from 'typeorm';++/**+ * Drop the redundant single-column index on messages(sessionId). Every lookup it can serve is+ * already covered by a composite index that leads with sessionId — IDX_399833…(sessionId,+ * createdAt) and the unique (sessionId, waMessageId) — so the standalone index adds only+ * write-time maintenance on a hot, high-volume table.+ *+ * Runs on the `data` connection. `IF EXISTS` keeps it idempotent and portable across SQLite and+ * Postgres (and safe on a synchronize-built schema). The name is the one TypeORM generated for the+ * `@Index()` on the sessionId column — hardcoded in the baseline migration (1770108659848).+ */+export class DropRedundantMessagesSessionIdIndex1781600000000 implements MigrationInterface {+ name = 'DropRedundantMessagesSessionIdIndex1781600000000';++ public async up(queryRunner: QueryRunner): Promise<void> {+ await queryRunner.query(`DROP INDEX IF EXISTS "IDX_066163c46cda7e8187f96bc87a"`);+ }++ public async down(queryRunner: QueryRunner): Promise<void> {+ await queryRunner.query(`CREATE INDEX IF NOT EXISTS "IDX_066163c46cda7e8187f96bc87a" ON "messages" ("sessionId")`);+ }+}diff --git a/src/database/migrations/__tests__/1781600000000-DropRedundantMessagesSessionIdIndex.spec.ts b/src/database/migrations/__tests__/1781600000000-DropRedundantMessagesSessionIdIndex.spec.tsnew file mode 100644index 000000000..a66756341--- /dev/null+++ b/src/database/migrations/__tests__/1781600000000-DropRedundantMessagesSessionIdIndex.spec.ts@@ -0,0 +1,47 @@+import { DataSource } from 'typeorm';+import { DropRedundantMessagesSessionIdIndex1781600000000 } from '../1781600000000-DropRedundantMessagesSessionIdIndex';++describe('DropRedundantMessagesSessionIdIndex migration', () => {+ let ds: DataSource;++ beforeEach(async () => {+ ds = new DataSource({ type: 'sqlite', database: ':memory:' });+ await ds.initialize();+ await ds.query(+ `CREATE TABLE "messages" ("id" varchar PRIMARY KEY NOT NULL, "sessionId" varchar NOT NULL, ` ++ `"createdAt" datetime NOT NULL DEFAULT (datetime('now')))`,+ );+ // The redundant single-column index plus the composite that subsumes it.+ await ds.query(`CREATE INDEX "IDX_066163c46cda7e8187f96bc87a" ON "messages" ("sessionId")`);+ await ds.query(`CREATE INDEX "IDX_399833392126349ef0b04b9bed" ON "messages" ("sessionId", "createdAt")`);+ });++ afterEach(async () => {+ await ds.destroy();+ });++ const indexNames = async (): Promise<string[]> => {+ const rows = await ds.query<{ name: string }[]>(`PRAGMA index_list("messages")`);+ return rows.map(r => r.name).sort();+ };++ it('drops the redundant single-column sessionId index but keeps the composite', async () => {+ const runner = ds.createQueryRunner();+ await new DropRedundantMessagesSessionIdIndex1781600000000().up(runner);++ const idx = await indexNames();+ expect(idx).not.toContain('IDX_066163c46cda7e8187f96bc87a');+ expect(idx).toContain('IDX_399833392126349ef0b04b9bed'); // composite (sessionId, createdAt) retained+ });++ it('is idempotent (re-running up is a no-op) and down() restores the index', async () => {+ const runner = ds.createQueryRunner();+ const migration = new DropRedundantMessagesSessionIdIndex1781600000000();++ await migration.up(runner);+ await expect(migration.up(runner)).resolves.toBeUndefined(); // IF EXISTS — safe to re-run++ await migration.down(runner);+ expect(await indexNames()).toContain('IDX_066163c46cda7e8187f96bc87a');+ });+});diff --git a/src/modules/message/entities/message.entity.ts b/src/modules/message/entities/message.entity.tsindex de344b959..073a6ffb5 100644--- a/src/modules/message/entities/message.entity.ts+++ b/src/modules/message/entities/message.entity.ts@@ -41,8 +41,9 @@ export class Message {@PrimaryGeneratedColumn('uuid')id: string;+ // No standalone @Index here: sessionId-only lookups are already served by the composite indexes+ // that lead with sessionId — (sessionId, createdAt) above and the unique (sessionId, waMessageId).@Column()- @Index()sessionId: string;@Column({ nullable: true })