Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(infra): drop orphaned session children from a data export
Yudhi Armyndharis59e0c0aff6d9
diff --git a/src/modules/infra/export-tables.parity.spec.ts b/src/modules/infra/export-tables.parity.spec.tsindex 0450a4583..51a716d9c 100644--- a/src/modules/infra/export-tables.parity.spec.ts+++ b/src/modules/infra/export-tables.parity.spec.ts@@ -48,6 +48,8 @@ interface EntityFile {file: string;connection: Connection;tables: string[];+ /** Of those, the tables carrying an FK to sessions. */+ sessionFkTables: string[];}function findEntityFiles(dir: string, found: string[] = []): string[] {@@ -92,12 +94,23 @@ async function loadEntityFiles(): Promise<EntityFile[]> {});await ds.initialize();const tableOf = new Map<unknown, string>(ds.entityMetadatas.map(metadata => [metadata.target, metadata.tableName]));+ const sessionFkTables = new Set(+ ds.entityMetadatas+ .filter(metadata => metadata.foreignKeys.some(fk => fk.referencedTablePath === 'sessions'))+ .map(metadata => metadata.tableName),+ );await ds.destroy();- return discovered.map(entry => ({- file: entry.file,- connection: classify(entry.file),- tables: entry.classes.map(cls => tableOf.get(cls)).filter((table): table is string => typeof table === 'string'),- }));+ return discovered.map(entry => {+ const tables = entry.classes+ .map(cls => tableOf.get(cls))+ .filter((table): table is string => typeof table === 'string');+ return {+ file: entry.file,+ connection: classify(entry.file),+ tables,+ sessionFkTables: tables.filter(table => sessionFkTables.has(table)),+ };+ });}/** Top-level property names of a published schema, read from the committed OpenAPI contract. */@@ -153,6 +166,13 @@ describe('export-tables registry: every data-DB entity table has a backup decisiexpect(stale).toEqual([]);});+ it('flags exactly the tables with an FK to sessions, so the export drops their orphaned rows', () => {+ const flagged = EXPORT_TABLES.filter(entry => entry.sessionFk).map(entry => entry.table);+ const withFk = entityFiles.filter(f => f.connection === 'data').flatMap(f => f.sessionFkTables);+ expect(flagged.sort()).toEqual(withFk.sort());+ expect(flagged.length).toBeGreaterThan(0);+ });+it('exports and imports the same tables in the same FK-safe order', () => {expect(EXPORT_TABLES.map(entry => entry.key)).toEqual(TABLE_IMPORTERS.map(importer => importer.key));});diff --git a/src/modules/infra/export-tables.ts b/src/modules/infra/export-tables.tsindex c81567603..42a0e55a9 100644--- a/src/modules/infra/export-tables.ts+++ b/src/modules/infra/export-tables.ts@@ -159,6 +159,12 @@ export interface ExportTable<K extends keyof MigrationTables = keyof MigrationTa* export, because a backup that silently omits them is worse than no backup.*/optional?: boolean;+ /**+ * Rows carry an FK `sessionId` to sessions. The reads share no snapshot, so a session created after+ * `sessions` was read can leave child rows here that would fail the restore's FK check and roll the+ * whole import back; the export drops rows whose session is not in the archive.+ */+ sessionFk?: boolean;/** In-place row mutation applied right after the read (redaction, artifact stripping). */afterRead?: (rows: MigrationTables[K]) => void;/**@@ -217,7 +223,7 @@ function defineExportTable<K extends keyof MigrationTables>(table: ExportTable<Kexport const EXPORT_TABLES: AnyExportTable[] = [// sessions first: webhooks/messages/templates/etc. all reference it (some via FK, all by sessionId).defineExportTable({ key: 'sessions', table: 'sessions', afterRead: redactSessionProxyCredentials }),- defineExportTable({ key: 'webhooks', table: 'webhooks', afterRead: redactWebhookCredentials }),+ defineExportTable({ key: 'webhooks', table: 'webhooks', sessionFk: true, afterRead: redactWebhookCredentials }),// Both carry a full inline base64 payload, so they share ONE budget: messages are served first// (newest media kept), batches spend what is left. Optional — an older DB may predate them.@@ -250,8 +256,13 @@ export const EXPORT_TABLES: AnyExportTable[] = [// templates + baileys_stored_messages both FK sessions ON DELETE CASCADE, so the import's// `DELETE FROM sessions` wipes them; they must be exported and re-inserted or the documented// backup flow loses them permanently.- defineExportTable({ key: 'templates', table: 'templates', optional: true }),- defineExportTable({ key: 'baileysStoredMessages', table: 'baileys_stored_messages', optional: true }),+ defineExportTable({ key: 'templates', table: 'templates', optional: true, sessionFk: true }),+ defineExportTable({+ key: 'baileysStoredMessages',+ table: 'baileys_stored_messages',+ optional: true,+ sessionFk: true,+ }),// The persisted lid->phone resolution cache. Not a FK to sessions (provenance only), so the// import's `DELETE FROM sessions` never clears it — it must be exported + re-inserted explicitly@@ -284,7 +295,7 @@ export const EXPORT_TABLES: AnyExportTable[] = [// automation_rules has an ON DELETE CASCADE FK to sessions, so the sessions DELETE takes every// rule with it — exporting and re-inserting it is not optional, or a restore silently destroys// every autoreply rule.- defineExportTable({ key: 'automationRules', table: 'automation_rules', optional: true }),+ defineExportTable({ key: 'automationRules', table: 'automation_rules', optional: true, sessionFk: true }),];/**diff --git a/src/modules/infra/infra-data.controller.spec.ts b/src/modules/infra/infra-data.controller.spec.tsindex 7790b1fa6..f096dfa73 100644--- a/src/modules/infra/infra-data.controller.spec.ts+++ b/src/modules/infra/infra-data.controller.spec.ts@@ -381,6 +381,33 @@ describe('InfraDataController.importData round-trips export-data (no silent messawait expect(controller.importData({ tables: dump.tables })).resolves.toMatchObject({ imported: true });});+ it('leaves out child rows of a session created after the sessions table was read', async () => {+ await seedSession('s1');+ await ds+ .getRepository(Template)+ .save(ds.getRepository(Template).create({ id: 't1', sessionId: 's1', name: 'greet', body: 'Hi' }));+ // The export reads each table separately, so a session paired mid-export is missing from the+ // archive while a child row written for it before its table is read is not.+ const query = ds.query.bind(ds);+ jest.spyOn(ds, 'query').mockImplementation(async (sql: string, params?: unknown[]) => {+ const rows: unknown = await query(sql, params);+ if (sql === 'SELECT * FROM sessions') {+ await seedSession('s2');+ await ds+ .getRepository(Template)+ .save(ds.getRepository(Template).create({ id: 't2', sessionId: 's2', name: 'late', body: 'Hi' }));+ }+ return rows;+ });++ const dump = await controller.exportData();+ jest.restoreAllMocks();++ expect(dump.counts).toMatchObject({ sessions: 1, templates: 1 });+ expect((dump.tables.templates as Array<{ id: string }>).map(t => t.id)).toEqual(['t1']);+ await expect(controller.importData({ tables: dump.tables })).resolves.toMatchObject({ imported: true });+ });+it('releases the loss-detection token even when the transaction never opens', async () => {await seedSession('s1');const dump = await controller.exportData();diff --git a/src/modules/infra/infra-data.service.ts b/src/modules/infra/infra-data.service.tsindex 4ed192b05..79578842e 100644--- a/src/modules/infra/infra-data.service.ts+++ b/src/modules/infra/infra-data.service.ts@@ -425,6 +425,17 @@ export class InfraDataService {counts[entry.key] = rows.length;}+ // See ExportTable.sessionFk: a child row of a session missing from the archive cannot be restored.+ const exportedSessions = new Set(tables.sessions.map(session => session.id));+ for (const entry of EXPORT_TABLES) {+ if (!entry.sessionFk) continue;+ const kept = (tables[entry.key] as Array<{ sessionId: string }>).filter(row =>+ exportedSessions.has(row.sessionId),+ );+ tables[entry.key] = kept as never;+ counts[entry.key] = kept.length;+ }+// Audit the full-DB export: this payload carries plugin-instance secrets, so WHO pulled// a dump (and the per-table row counts) must land in the audit log. Data itself is never// logged — only counts.