From 8a4224c1164234ade89d5f424f5d5549cadebd1b Mon Sep 17 00:00:00 2001 From: Tyagiquamar Date: Wed, 9 Sep 2026 23:59:36 +0530 Subject: [PATCH] Skip the Postgres replication-byte recorder in test-connection mode. TEST_CONNECTION does not register DATA_REPLICATED_BYTES, so installing the jpgwire recorder made the CLI fail before the connection check ran. --- ...kip-test-connection-replication-metrics.md | 5 +++ .../src/module/PostgresModule.ts | 2 +- .../test/src/metrics_recorder.test.ts | 40 +++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 .changeset/skip-test-connection-replication-metrics.md create mode 100644 modules/module-postgres/test/src/metrics_recorder.test.ts diff --git a/.changeset/skip-test-connection-replication-metrics.md b/.changeset/skip-test-connection-replication-metrics.md new file mode 100644 index 000000000..4406c5884 --- /dev/null +++ b/.changeset/skip-test-connection-replication-metrics.md @@ -0,0 +1,5 @@ +--- +'@powersync/service-module-postgres': patch +--- + +Skip the PostgreSQL replication-byte metrics recorder in `test-connection` mode so the command can run without `DATA_REPLICATED_BYTES`. diff --git a/modules/module-postgres/src/module/PostgresModule.ts b/modules/module-postgres/src/module/PostgresModule.ts index 3a3bfb2ec..101415230 100644 --- a/modules/module-postgres/src/module/PostgresModule.ts +++ b/modules/module-postgres/src/module/PostgresModule.ts @@ -31,7 +31,7 @@ export class PostgresModule extends replication.ReplicationModule { // Record replicated bytes using global jpgwire metrics. Only registered if this module is replicating - if (context.replicationEngine) { + if (context.replicationEngine && context.serviceMode !== system.ServiceContextMode.TEST_CONNECTION) { jpgwire.setMetricsRecorder({ addBytesRead(bytes) { context.metricsEngine.getCounter(ReplicationMetric.DATA_REPLICATED_BYTES).add(bytes); diff --git a/modules/module-postgres/test/src/metrics_recorder.test.ts b/modules/module-postgres/test/src/metrics_recorder.test.ts new file mode 100644 index 000000000..22f75eccc --- /dev/null +++ b/modules/module-postgres/test/src/metrics_recorder.test.ts @@ -0,0 +1,40 @@ +import { system } from '@powersync/service-core'; +import * as jpgwire from '@powersync/service-jpgwire'; +import { afterEach, describe, expect, it, vi } from 'vitest'; +import { PostgresModule } from '../../src/module/PostgresModule.js'; + +describe('PostgresModule metrics recorder', () => { + afterEach(() => { + vi.restoreAllMocks(); + }); + + it('does not install a jpgwire recorder in TEST_CONNECTION mode', async () => { + const spy = vi.spyOn(jpgwire, 'setMetricsRecorder'); + const mod = new PostgresModule(); + await mod.onInitialized({ + replicationEngine: {}, + serviceMode: system.ServiceContextMode.TEST_CONNECTION, + metricsEngine: { + getCounter() { + throw new Error('DATA_REPLICATED_BYTES should not be read in test-connection'); + } + } + } as unknown as system.ServiceContextContainer); + + expect(spy).not.toHaveBeenCalled(); + }); + + it('installs a jpgwire recorder when replication metrics are available', async () => { + const spy = vi.spyOn(jpgwire, 'setMetricsRecorder'); + const mod = new PostgresModule(); + await mod.onInitialized({ + replicationEngine: {}, + serviceMode: system.ServiceContextMode.UNIFIED, + metricsEngine: { + getCounter: () => ({ add() {} }) + } + } as unknown as system.ServiceContextContainer); + + expect(spy).toHaveBeenCalledTimes(1); + }); +});