Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(database): normalize terminal failures before sync
Yudhi Armyndharisd510cdee0825
diff --git a/src/database/migrations/1787200000000-DeduplicateTerminalWebhookFailures.ts b/src/database/migrations/1787200000000-DeduplicateTerminalWebhookFailures.tsindex 84af974aa..247d5bc3a 100644--- a/src/database/migrations/1787200000000-DeduplicateTerminalWebhookFailures.ts+++ b/src/database/migrations/1787200000000-DeduplicateTerminalWebhookFailures.ts@@ -6,13 +6,16 @@ export class DeduplicateTerminalWebhookFailures1787200000000 implements Migratioasync up(queryRunner: QueryRunner): Promise<void> {if (queryRunner.dataSource.options.type === 'postgres') await queryRunner.query('SET LOCAL statement_timeout = 0');+ const payloadOrder = (await queryRunner.hasColumn('webhook_delivery_failures', 'payload'))+ ? 'CASE WHEN "payload" IS NOT NULL THEN 0 ELSE 1 END,'+ : '';// Prefer a replay copy, then the largest attempt count and most recent record.// Unkeyed failures have no shared identity and remain separate.await queryRunner.query(`DELETE FROM "webhook_delivery_failures" WHERE "id" IN (SELECT "id" FROM (SELECT "id", ROW_NUMBER() OVER (PARTITION BY "webhookId", "idempotencyKey"- ORDER BY CASE WHEN "payload" IS NOT NULL THEN 0 ELSE 1 END,+ ORDER BY ${payloadOrder}"attempts" DESC, "createdAt" DESC, "id" DESC) AS ordinal FROM "webhook_delivery_failures"WHERE "attempts" > 0 AND "idempotencyKey" IS NOT NULLdiff --git a/src/database/pg-boot-migrations.ts b/src/database/pg-boot-migrations.tsindex db4486aa0..fdf783022 100644--- a/src/database/pg-boot-migrations.ts+++ b/src/database/pg-boot-migrations.ts@@ -2,6 +2,7 @@ import { Client, ClientConfig } from 'pg';import { DataSource, DataSourceOptions } from 'typeorm';import { createLogger } from '../common/services/logger.service';import { assertDataConnectionUtc, postgresUtcExtra } from './postgres-utc';+import { DeduplicateTerminalWebhookFailures1787200000000 } from './migrations/1787200000000-DeduplicateTerminalWebhookFailures';const logger = createLogger('PgBootMigrations');@@ -51,6 +52,26 @@ export async function createBootDataSource(const createDataSource = deps.createDataSource ?? (opts => new DataSource(opts));const createLockClient = deps.createLockClient ?? createPgLockClient;+ if (options?.type === 'better-sqlite3' && options.synchronize) {+ const dataSource = createDataSource({ ...options, synchronize: false });+ try {+ await dataSource.initialize();+ const runner = dataSource.createQueryRunner();+ try {+ if (await runner.hasTable('webhook_delivery_failures')) {+ await new DeduplicateTerminalWebhookFailures1787200000000().up(runner);+ }+ } finally {+ await runner.release();+ }+ await dataSource.synchronize();+ return dataSource;+ } catch (error) {+ if (dataSource.isInitialized) await dataSource.destroy().catch(() => undefined);+ throw error;+ }+ }+if (options?.type !== 'postgres') {// useFactory always resolves a full options object; the optional parameter is the library's// defensive typing, not a state this connection can actually boot in.diff --git a/src/database/sqlite-boot-failures.spec.ts b/src/database/sqlite-boot-failures.spec.tsnew file mode 100644index 000000000..64f1a7425--- /dev/null+++ b/src/database/sqlite-boot-failures.spec.ts@@ -0,0 +1,58 @@+import { mkdtemp, rm } from 'node:fs/promises';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { DataSource } from 'typeorm';+import { WebhookDeliveryFailure } from '../modules/webhook/entities/webhook-delivery-failure.entity';+import { createBootDataSource } from './pg-boot-migrations';++describe('SQLite synchronize upgrades with legacy terminal failures', () => {+ it.each([false, true])(+ 'normalizes duplicates before creating the unique index (payload column=%s)',+ async hasPayload => {+ const directory = await mkdtemp(join(tmpdir(), 'openwa-failure-upgrade-'));+ const options = {+ type: 'better-sqlite3' as const,+ database: join(directory, 'data.sqlite'),+ entities: [WebhookDeliveryFailure],+ synchronize: true,+ };+ let ds: DataSource | undefined;+ try {+ ds = await new DataSource(options).initialize();+ await ds.query('DROP INDEX "UQ_webhook_delivery_failures_terminal"');+ const row = {+ webhookId: 'w',+ sessionId: 's1',+ event: 'message.received',+ url: 'https://receiver.example',+ idempotencyKey: 'k',+ attempts: 5,+ lastError: 'HTTP 503',+ createdAt: new Date('2026-10-05'),+ };+ await ds.getRepository(WebhookDeliveryFailure).save([+ { ...row, id: 'a' },+ { ...row, id: 'b', attempts: 3, payload: { body: 'retained' } },+ { ...row, id: 'c', attempts: 0 },+ { ...row, id: 'd', idempotencyKey: undefined },+ { ...row, id: 'e', idempotencyKey: undefined },+ ]);+ if (!hasPayload) await ds.query('ALTER TABLE webhook_delivery_failures DROP COLUMN payload');+ await ds.destroy();+ ds = await createBootDataSource(options);+ if (!ds.isInitialized) await ds.initialize();+ const repository = ds.getRepository(WebhookDeliveryFailure);+ expect((await repository.find({ order: { id: 'ASC' } })).map(failure => failure.id)).toEqual([+ hasPayload ? 'b' : 'a',+ 'c',+ 'd',+ 'e',+ ]);+ await expect(repository.insert({ ...row, id: 'f' })).rejects.toThrow(/UNIQUE constraint failed/);+ } finally {+ if (ds?.isInitialized) await ds.destroy();+ await rm(directory, { recursive: true, force: true });+ }+ },+ );+});