Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(infra): store archived datetime columns in SQLite form on import

Yudhi Armyndharise7243d24956d
diff --git a/src/modules/infra/infra-data.controller.spec.ts b/src/modules/infra/infra-data.controller.spec.ts
index 5272d0026..3cc12f5b5 100644
--- a/src/modules/infra/infra-data.controller.spec.ts
+++ b/src/modules/infra/infra-data.controller.spec.ts
@@ -25,7 +25,7 @@ jest.mock('fs', () => {
import { DataSource, IsNull, QueryFailedError } from 'typeorm';
import { ConflictException } from '@nestjs/common';
import { InfraDataController } from './infra-data.controller';
-import { InfraDataService, restoreSessionOwnership } from './infra-data.service';
+import { InfraDataService, restoreSessionOwnership, toSqliteDatetime } from './infra-data.service';
import { EXPORT_TABLES } from './export-tables';
import { Session, SessionStatus } from '../session/entities/session.entity';
import { Webhook } from '../webhook/entities/webhook.entity';
@@ -40,6 +40,7 @@ import { ConversationMapping } from '../integration/entities/conversation-mappin
import { IngressEvent } from '../integration/entities/ingress-event.entity';
import { WebhookDeliveryFailure } from '../webhook/entities/webhook-delivery-failure.entity';
import { WebhookOutboxEvent } from '../webhook/entities/webhook-outbox-event.entity';
+import { WebhookOutboxService } from '../webhook/webhook-outbox.service';
import { IntegrationDeliveryFailure } from '../integration/entities/integration-delivery-failure.entity';
import { StatusUpdate } from '../status-store/entities/status-update.entity';
import { AutomationRule } from '../automation/entities/automation-rule.entity';
@@ -1303,6 +1304,94 @@ describe('InfraDataController.import/export preserves every data-DB table', () =
expect((await outboxRepo.findOneByOrFail({ idempotencyKey: 'key-1' })).state).toBe('pending');
});
+ // A PostgreSQL export serializes CreateDateColumn/UpdateDateColumn values as ISO `...T...Z`, while
+ // TypeORM writes and compares them on SQLite as `YYYY-MM-DD HH:MM:SS.SSS`. Stored verbatim, a
+ // restored pending row never matched `createdAt < cutoff` on its own calendar day, so the replay
+ // sweep skipped the whole restored backlog until the next UTC day.
+ it('stores PostgreSQL-archived datetime columns in the SQLite form so same-day comparisons match', async () => {
+ const res = await controller.importData({
+ tables: {
+ sessions: [
+ {
+ id: 's1',
+ name: 'session-s1',
+ status: 'disconnected',
+ phone: null,
+ pushName: null,
+ config: {},
+ proxyUrl: null,
+ proxyType: null,
+ connectedAt: '2026-09-14T01:00:00.000Z',
+ lastActiveAt: null,
+ createdAt: '2026-09-14T01:00:00.000Z',
+ // Already in the SQLite form (a SQLite-made archive): carries no zone and stays untouched.
+ updatedAt: '2026-09-14 02:00:00.000',
+ },
+ ],
+ messageBatches: [
+ {
+ id: 'b1',
+ batch_id: 'batch-1',
+ session_id: 's1',
+ status: 'pending',
+ messages: [],
+ options: null,
+ progress: null,
+ results: null,
+ current_index: 0,
+ created_at: '2026-09-14T03:00:00.000Z',
+ updated_at: '2026-09-14T03:30:00.000+07:00',
+ started_at: null,
+ completed_at: null,
+ },
+ ],
+ webhookOutboxEvents: [
+ {
+ id: 'o1',
+ webhookId: 'wh-1',
+ sessionId: 's1',
+ event: 'message.received',
+ idempotencyKey: 'key-1',
+ deliveryId: 'del-1',
+ payload: JSON.stringify({ from: '[email protected]' }),
+ state: 'pending',
+ attempts: 1,
+ lastAttemptAt: '2026-09-14T03:05:00.000Z',
+ createdAt: '2026-09-14T03:00:00.000Z',
+ },
+ ],
+ },
+ });
+
+ expect(res.warnings).toEqual([]);
+ expect(res.imported).toBe(true);
+
+ const [outbox] = await ds.query<unknown[]>('SELECT "createdAt", "lastAttemptAt" FROM webhook_outbox_events');
+ expect(outbox).toEqual({ createdAt: '2026-09-14 03:00:00.000', lastAttemptAt: '2026-09-14T03:05:00.000Z' });
+ const stale = await new WebhookOutboxService(ds.getRepository(WebhookOutboxEvent)).findStale(
+ new Date('2026-09-14T10:00:00.000Z'),
+ 10,
+ );
+ expect(stale.map(row => row.idempotencyKey)).toEqual(['key-1']);
+
+ const [session] = await ds.query<unknown[]>('SELECT "connectedAt", "createdAt", "updatedAt" FROM sessions');
+ expect(session).toEqual({
+ connectedAt: '2026-09-14T01:00:00.000Z',
+ createdAt: '2026-09-14 01:00:00.000',
+ updatedAt: '2026-09-14 02:00:00.000',
+ });
+ const [batch] = await ds.query<unknown[]>('SELECT created_at, updated_at FROM message_batches');
+ expect(batch).toEqual({ created_at: '2026-09-14 03:00:00.000', updated_at: '2026-09-13 20:30:00.000' });
+ });
+
+ // A zoneless value must never be parsed: `new Date()` reads it as host-local time and shifts it. The
+ // millisecond-less forms change text when reformatted even on a UTC host, so this holds in any TZ.
+ it('leaves datetime values without a zone untouched', () => {
+ expect(toSqliteDatetime('2026-09-14 02:00:00')).toBe('2026-09-14 02:00:00');
+ expect(toSqliteDatetime('2026-09-14T02:00:00')).toBe('2026-09-14T02:00:00');
+ expect(toSqliteDatetime('2026-09-14T02:00:00Z')).toBe('2026-09-14 02:00:00.000');
+ });
+
// The messages import column list must carry every later-added column; `author` (the group
// sender identity) was the one that drifted — a restored backup silently lost all attribution.
it('restores the group-sender author column on a backup→restore', async () => {
diff --git a/src/modules/infra/infra-data.service.ts b/src/modules/infra/infra-data.service.ts
index 021eb3f94..0b8a088c2 100644
--- a/src/modules/infra/infra-data.service.ts
+++ b/src/modules/infra/infra-data.service.ts
@@ -11,6 +11,7 @@ import { LidMappingStoreService } from '../../engine/identity/lid-mapping-store.
import { SessionOwnershipService } from '../session/session-ownership.service';
import { Session as SessionEntity, SessionStatus } from '../session/entities/session.entity';
import { In } from 'typeorm';
+import { DateUtils } from 'typeorm/util/DateUtils';
import type { MigrationTables, TableCounts } from './migration-tables.types';
import { EXPORT_TABLES, EXPORT_TABLE_EXCLUSIONS } from './export-tables';
import { TABLE_IMPORTERS } from './table-importers';
@@ -107,6 +108,37 @@ export async function restoreSessionOwnership(
}
}
+/**
+ * The `datetime` columns of each imported table on a SQLite data connection, keyed by backup table key
+ * and read from the entity metadata: CreateDateColumn/UpdateDateColumn and any other column TypeORM
+ * binds through its SQLite datetime path. DateTransformer columns are `text` there and are not listed,
+ * because the app itself writes those in ISO form.
+ */
+export function sqliteDatetimeColumns(dataSource: DataSource): Map<keyof MigrationTables, string[]> {
+ const byTable = new Map(
+ dataSource.entityMetadatas.map(metadata => [
+ metadata.tableName,
+ metadata.columns
+ .filter(column => dataSource.driver.normalizeType(column) === 'datetime')
+ .map(column => column.databaseName),
+ ]),
+ );
+ return new Map(EXPORT_TABLES.map(entry => [entry.key, byTable.get(entry.table) ?? []]));
+}
+
+/**
+ * Rewrite one archived `datetime` value into the text TypeORM writes on SQLite (`YYYY-MM-DD HH:MM:SS.SSS`,
+ * UTC). A PostgreSQL export serializes these columns as ISO `...T...Z`, and SQLite compares them as
+ * text: `'T'` sorts after `' '`, so a restored row never matches `LessThan(date)` on its own calendar
+ * day. Only ISO text with an explicit zone is converted. A value already in SQLite form carries no
+ * zone, and parsing it would read it as host-local time and shift it, so it is left as it is.
+ */
+export function toSqliteDatetime(value: unknown): unknown {
+ if (typeof value !== 'string' || !/T.*(Z|[+-]\d{2}:?\d{2})$/i.test(value)) return value;
+ const date = new Date(value);
+ return Number.isNaN(date.getTime()) ? value : DateUtils.mixedDateToUtcDatetimeString(date);
+}
+
/**
* Dialects where every TypeORM query runner shares ONE connection, so an open transaction is visible
* to anything else querying in the same process. A positive list on purpose: a dialect nobody has
@@ -640,10 +672,23 @@ export class InfraDataService {
// carry each table's INSERT text, param mapping, and per-row skip guard; a missing or empty
// table keeps its 0 count and contributes no warnings.
const counts = Object.fromEntries(TABLE_IMPORTERS.map(importer => [importer.key, 0] as const)) as TableCounts;
+ // SQLite only: archived datetime values are normalized to the form TypeORM writes there, so a
+ // PostgreSQL-made backup compares and sorts like rows the app wrote itself.
+ const datetimeColumns = isPostgres ? undefined : sqliteDatetimeColumns(this.dataDataSource);
for (const importer of TABLE_IMPORTERS) {
const rows = data.tables[importer.key];
if (!rows?.length) continue;
- for (const untypedRow of rows) {
+ const dateColumns = datetimeColumns?.get(importer.key) ?? [];
+ for (const archivedRow of rows) {
+ const source = archivedRow as unknown as Record<string, unknown>;
+ const untypedRow = {
+ ...source,
+ ...Object.fromEntries(
+ dateColumns
+ .filter(column => column in source)
+ .map(column => [column, toSqliteDatetime(source[column])]),
+ ),
+ };
// `rows` was read from `data.tables[importer.key]`, so it holds exactly the row type this
// descriptor's id/map/skip declare. That correlation is what the erased importer type
// cannot carry, and this loop is the one place it is known — so the cast lives here rather