@powersync/service-module-postgres 0.11.3 → 0.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  import * as bson from 'bson';
2
- import { afterEach, describe, expect, test } from 'vitest';
2
+ import { afterEach, beforeAll, describe, expect, test } from 'vitest';
3
3
  import { WalStream, WalStreamOptions } from '../../src/replication/WalStream.js';
4
4
  import { env } from './env.js';
5
5
  import {
@@ -15,8 +15,8 @@ import * as pgwire from '@powersync/service-jpgwire';
15
15
  import { SqliteRow } from '@powersync/service-sync-rules';
16
16
 
17
17
  import { PgManager } from '@module/replication/PgManager.js';
18
- import { storage } from '@powersync/service-core';
19
- import { test_utils } from '@powersync/service-core-tests';
18
+ import { createCoreReplicationMetrics, initializeCoreReplicationMetrics, storage } from '@powersync/service-core';
19
+ import { METRICS_HELPER, test_utils } from '@powersync/service-core-tests';
20
20
  import * as mongo_storage from '@powersync/service-module-mongodb-storage';
21
21
  import * as postgres_storage from '@powersync/service-module-postgres-storage';
22
22
  import * as timers from 'node:timers/promises';
@@ -49,6 +49,11 @@ function defineSlowTests(factory: storage.TestStorageFactory) {
49
49
  let abortController: AbortController | undefined;
50
50
  let streamPromise: Promise<void> | undefined;
51
51
 
52
+ beforeAll(async () => {
53
+ createCoreReplicationMetrics(METRICS_HELPER.metricsEngine);
54
+ initializeCoreReplicationMetrics(METRICS_HELPER.metricsEngine);
55
+ });
56
+
52
57
  afterEach(async () => {
53
58
  // This cleans up, similar to WalStreamTestContext.dispose().
54
59
  // These tests are a little more complex than what is supported by WalStreamTestContext.
@@ -98,7 +103,8 @@ bucket_definitions:
98
103
  const options: WalStreamOptions = {
99
104
  abort_signal: abortController.signal,
100
105
  connections,
101
- storage: storage
106
+ storage: storage,
107
+ metrics: METRICS_HELPER.metricsEngine
102
108
  };
103
109
  walStream = new WalStream(options);
104
110
 
@@ -344,13 +350,14 @@ bucket_definitions:
344
350
  const connections = new PgManager(TEST_CONNECTION_OPTIONS, {});
345
351
  const replicationConnection = await connections.replicationConnection();
346
352
 
347
- abortController = new AbortController();
348
- const options: WalStreamOptions = {
349
- abort_signal: abortController.signal,
350
- connections,
351
- storage: storage
352
- };
353
- walStream = new WalStream(options);
353
+ abortController = new AbortController();
354
+ const options: WalStreamOptions = {
355
+ abort_signal: abortController.signal,
356
+ connections,
357
+ storage: storage,
358
+ metrics: METRICS_HELPER.metricsEngine
359
+ };
360
+ walStream = new WalStream(options);
354
361
 
355
362
  await storage.clear();
356
363
 
@@ -1,12 +1,13 @@
1
1
  import { MissingReplicationSlotError } from '@module/replication/WalStream.js';
2
- import { Metrics, storage } from '@powersync/service-core';
3
- import { putOp, removeOp } from '@powersync/service-core-tests';
2
+ import { storage } from '@powersync/service-core';
3
+ import { METRICS_HELPER, putOp, removeOp } from '@powersync/service-core-tests';
4
4
  import { pgwireRows } from '@powersync/service-jpgwire';
5
5
  import * as crypto from 'crypto';
6
6
  import { describe, expect, test } from 'vitest';
7
7
  import { env } from './env.js';
8
8
  import { INITIALIZED_MONGO_STORAGE_FACTORY, INITIALIZED_POSTGRES_STORAGE_FACTORY } from './util.js';
9
9
  import { WalStreamTestContext } from './wal_stream_utils.js';
10
+ import { ReplicationMetric } from '@powersync/service-types';
10
11
 
11
12
  const BASIC_SYNC_RULES = `
12
13
  bucket_definitions:
@@ -40,9 +41,8 @@ bucket_definitions:
40
41
 
41
42
  await context.replicateSnapshot();
42
43
 
43
- const startRowCount = (await Metrics.getInstance().getMetricValueForTests('powersync_rows_replicated_total')) ?? 0;
44
- const startTxCount =
45
- (await Metrics.getInstance().getMetricValueForTests('powersync_transactions_replicated_total')) ?? 0;
44
+ const startRowCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.ROWS_REPLICATED)) ?? 0;
45
+ const startTxCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.TRANSACTIONS_REPLICATED)) ?? 0;
46
46
 
47
47
  context.startStreaming();
48
48
 
@@ -55,9 +55,8 @@ bucket_definitions:
55
55
  const data = await context.getBucketData('global[]');
56
56
 
57
57
  expect(data).toMatchObject([putOp('test_data', { id: test_id, description: 'test1', num: 1152921504606846976n })]);
58
- const endRowCount = (await Metrics.getInstance().getMetricValueForTests('powersync_rows_replicated_total')) ?? 0;
59
- const endTxCount =
60
- (await Metrics.getInstance().getMetricValueForTests('powersync_transactions_replicated_total')) ?? 0;
58
+ const endRowCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.ROWS_REPLICATED)) ?? 0;
59
+ const endTxCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.TRANSACTIONS_REPLICATED)) ?? 0;
61
60
  expect(endRowCount - startRowCount).toEqual(1);
62
61
  expect(endTxCount - startTxCount).toEqual(1);
63
62
  });
@@ -77,9 +76,8 @@ bucket_definitions:
77
76
 
78
77
  await context.replicateSnapshot();
79
78
 
80
- const startRowCount = (await Metrics.getInstance().getMetricValueForTests('powersync_rows_replicated_total')) ?? 0;
81
- const startTxCount =
82
- (await Metrics.getInstance().getMetricValueForTests('powersync_transactions_replicated_total')) ?? 0;
79
+ const startRowCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.ROWS_REPLICATED)) ?? 0;
80
+ const startTxCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.TRANSACTIONS_REPLICATED)) ?? 0;
83
81
 
84
82
  context.startStreaming();
85
83
 
@@ -90,9 +88,8 @@ bucket_definitions:
90
88
  const data = await context.getBucketData('global[]');
91
89
 
92
90
  expect(data).toMatchObject([putOp('test_DATA', { id: test_id, description: 'test1' })]);
93
- const endRowCount = (await Metrics.getInstance().getMetricValueForTests('powersync_rows_replicated_total')) ?? 0;
94
- const endTxCount =
95
- (await Metrics.getInstance().getMetricValueForTests('powersync_transactions_replicated_total')) ?? 0;
91
+ const endRowCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.ROWS_REPLICATED)) ?? 0;
92
+ const endTxCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.TRANSACTIONS_REPLICATED)) ?? 0;
96
93
  expect(endRowCount - startRowCount).toEqual(1);
97
94
  expect(endTxCount - startTxCount).toEqual(1);
98
95
  });
@@ -274,9 +271,8 @@ bucket_definitions:
274
271
 
275
272
  await context.replicateSnapshot();
276
273
 
277
- const startRowCount = (await Metrics.getInstance().getMetricValueForTests('powersync_rows_replicated_total')) ?? 0;
278
- const startTxCount =
279
- (await Metrics.getInstance().getMetricValueForTests('powersync_transactions_replicated_total')) ?? 0;
274
+ const startRowCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.ROWS_REPLICATED)) ?? 0;
275
+ const startTxCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.TRANSACTIONS_REPLICATED)) ?? 0;
280
276
 
281
277
  context.startStreaming();
282
278
 
@@ -287,9 +283,8 @@ bucket_definitions:
287
283
  const data = await context.getBucketData('global[]');
288
284
 
289
285
  expect(data).toMatchObject([]);
290
- const endRowCount = (await Metrics.getInstance().getMetricValueForTests('powersync_rows_replicated_total')) ?? 0;
291
- const endTxCount =
292
- (await Metrics.getInstance().getMetricValueForTests('powersync_transactions_replicated_total')) ?? 0;
286
+ const endRowCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.ROWS_REPLICATED)) ?? 0;
287
+ const endTxCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.TRANSACTIONS_REPLICATED)) ?? 0;
293
288
 
294
289
  // There was a transaction, but we should not replicate any actual data
295
290
  expect(endRowCount - startRowCount).toEqual(0);
@@ -2,12 +2,14 @@ import { PgManager } from '@module/replication/PgManager.js';
2
2
  import { PUBLICATION_NAME, WalStream, WalStreamOptions } from '@module/replication/WalStream.js';
3
3
  import {
4
4
  BucketStorageFactory,
5
+ createCoreReplicationMetrics,
6
+ initializeCoreReplicationMetrics,
5
7
  InternalOpId,
6
8
  OplogEntry,
7
9
  storage,
8
10
  SyncRulesBucketStorage
9
11
  } from '@powersync/service-core';
10
- import { test_utils } from '@powersync/service-core-tests';
12
+ import { METRICS_HELPER, test_utils } from '@powersync/service-core-tests';
11
13
  import * as pgwire from '@powersync/service-jpgwire';
12
14
  import { clearTestDb, getClientCheckpoint, TEST_CONNECTION_OPTIONS } from './util.js';
13
15
 
@@ -41,7 +43,10 @@ export class WalStreamTestContext implements AsyncDisposable {
41
43
  constructor(
42
44
  public factory: BucketStorageFactory,
43
45
  public connectionManager: PgManager
44
- ) {}
46
+ ) {
47
+ createCoreReplicationMetrics(METRICS_HELPER.metricsEngine);
48
+ initializeCoreReplicationMetrics(METRICS_HELPER.metricsEngine);
49
+ }
45
50
 
46
51
  async [Symbol.asyncDispose]() {
47
52
  await this.dispose();
@@ -101,6 +106,7 @@ export class WalStreamTestContext implements AsyncDisposable {
101
106
  }
102
107
  const options: WalStreamOptions = {
103
108
  storage: this.storage,
109
+ metrics: METRICS_HELPER.metricsEngine,
104
110
  connections: this.connectionManager,
105
111
  abort_signal: this.abortController.signal
106
112
  };