Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(infra): refuse an empty restore before stopping orphan engines
Yudhi Armyndharis3657903fc490
diff --git a/src/modules/infra/infra-data.controller.spec.ts b/src/modules/infra/infra-data.controller.spec.tsindex 6518e9970..54babbc14 100644--- a/src/modules/infra/infra-data.controller.spec.ts+++ b/src/modules/infra/infra-data.controller.spec.ts@@ -1399,7 +1399,10 @@ describe('InfraDataController.importData round-trips export-data (no silent mess});});- await expect(controller.importData({ tables: { messages: [] } })).rejects.toThrow(/database is locked/);+ // One row, so the archive is not refused as empty before the transaction opens.+ await expect(controller.importData({ tables: { messages: [{ id: 'm2' }] } as never })).rejects.toThrow(+ /database is locked/,+ );expect(rolledBack).toBe(true);jest.restoreAllMocks();@@ -2446,6 +2449,24 @@ describe('InfraDataController.importData status_updates + runtime reconciliationexpect(res.orphanedEngines).toEqual(['ghost']);});+ it('refuses an archive with no rows before stopOrphans tears down any engine', async () => {+ // With no sessions in the archive every running engine looks orphaned, and the teardown cannot be+ // undone, yet a backup without rows is always refused. Nothing may be stopped for it.+ await seedSession('s1');+ const stopOrphanEngines = jest.fn().mockResolvedValue({ stopped: ['s1'], notRunning: [], failed: [] });+ const controller = build({ sessionService: { getActiveSessionIds: () => ['s1'], stopOrphanEngines } });++ const res = await controller.importData({ tables: { sessions: [], messages: [] }, stopOrphans: true });++ expect(res.imported).toBe(false);+ expect(res.warnings).toEqual([+ 'Backup contained no rows to restore; refused to replace existing data. Check the file.',+ ]);+ expect(stopOrphanEngines).not.toHaveBeenCalled();+ expect(res.stoppedOrphanEngines).toEqual([]);+ expect(await ds.getRepository(Session).count()).toBe(1);+ });+it('still reports the engines it already stopped when the import rolls back', async () => {// The pre-flight teardown runs BEFORE the transaction opens and cannot be rolled back with it.// An operator reading imported:false plus empty orphan arrays would conclude nothing happened,@@ -2743,7 +2764,7 @@ describe('InfraDataController.importData rejects a malformed table value', () =>// archive) and an empty one (a table that legitimately has no rows).it.each([['omits a table', { sessions: [{ id: 's1' }] }],- ['carries an empty table', { sessions: [], messages: [] }],+ ['carries an empty table', { sessions: [{ id: 's1' }], messages: [] }],])('does not reject an archive that %s', async (_label, tables) => {await expect(controller().importData({ tables } as never)).rejects.not.toThrow(/must be an array/);});diff --git a/src/modules/infra/infra-data.service.ts b/src/modules/infra/infra-data.service.tsindex 37006af50..3ce39efa3 100644--- a/src/modules/infra/infra-data.service.ts+++ b/src/modules/infra/infra-data.service.ts@@ -247,6 +247,8 @@ export interface InfraExportDataResult {omittedInlineMedia: { messages: number; messageBatches: number };}+const EMPTY_ARCHIVE_WARNING = 'Backup contained no rows to restore; refused to replace existing data. Check the file.';+/** Result of POST /infra/import-data; InfraImportDataResponseDto is the published contract. */export interface InfraImportDataResult {imported: boolean;@@ -581,6 +583,22 @@ export class InfraDataService {}}+ // An archive with no rows is always refused (see the totalRestored check below), so refuse it here,+ // before the orphan pre-flight: with no sessions in it every running engine reads as an orphan, and+ // a stopOrphans retry would tear all of them down for a restore that was never going to happen.+ if (TABLE_IMPORTERS.every(importer => !data.tables[importer.key]?.length)) {+ return {+ imported: false,+ counts: Object.fromEntries(TABLE_IMPORTERS.map(importer => [importer.key, 0] as const)) as TableCounts,+ warnings: [EMPTY_ARCHIVE_WARNING],+ notices: [],+ restartRequired: false,+ orphanedEngines: [],+ stoppedOrphanEngines: [],+ failedOrphanEngines: [],+ };+ }+const importedSessionIds = new Set((data.tables.sessions ?? []).map(s => s.id));const orphanedEngines = (this.sessionService?.getActiveSessionIds() ?? []).filter(id => !importedSessionIds.has(id),@@ -958,7 +976,7 @@ export class InfraDataService {return {imported: false,counts,- warnings: ['Backup contained no rows to restore; refused to replace existing data. Check the file.'],+ warnings: [EMPTY_ARCHIVE_WARNING],notices,...engineStateAfterRollback,};