@powersync/service-module-mysql 0.0.0-dev-20250317122913 → 0.0.0-dev-20250326092547

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,10 +1,11 @@
1
- import { Metrics, storage } from '@powersync/service-core';
2
- import { putOp, removeOp } from '@powersync/service-core-tests';
1
+ import { storage } from '@powersync/service-core';
2
+ import { METRICS_HELPER, putOp, removeOp } from '@powersync/service-core-tests';
3
3
  import { v4 as uuid } from 'uuid';
4
4
  import { describe, expect, test } from 'vitest';
5
5
  import { BinlogStreamTestContext } from './BinlogStreamUtils.js';
6
6
  import { env } from './env.js';
7
7
  import { INITIALIZED_MONGO_STORAGE_FACTORY, INITIALIZED_POSTGRES_STORAGE_FACTORY } from './util.js';
8
+ import { ReplicationMetric } from '@powersync/service-types';
8
9
 
9
10
  const BASIC_SYNC_RULES = `
10
11
  bucket_definitions:
@@ -35,9 +36,8 @@ function defineBinlogStreamTests(factory: storage.TestStorageFactory) {
35
36
 
36
37
  await context.replicateSnapshot();
37
38
 
38
- const startRowCount = (await Metrics.getInstance().getMetricValueForTests('powersync_rows_replicated_total')) ?? 0;
39
- const startTxCount =
40
- (await Metrics.getInstance().getMetricValueForTests('powersync_transactions_replicated_total')) ?? 0;
39
+ const startRowCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.ROWS_REPLICATED)) ?? 0;
40
+ const startTxCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.TRANSACTIONS_REPLICATED)) ?? 0;
41
41
 
42
42
  context.startStreaming();
43
43
  const testId = uuid();
@@ -47,9 +47,8 @@ function defineBinlogStreamTests(factory: storage.TestStorageFactory) {
47
47
  const data = await context.getBucketData('global[]');
48
48
 
49
49
  expect(data).toMatchObject([putOp('test_data', { id: testId, description: 'test1', num: 1152921504606846976n })]);
50
- const endRowCount = (await Metrics.getInstance().getMetricValueForTests('powersync_rows_replicated_total')) ?? 0;
51
- const endTxCount =
52
- (await Metrics.getInstance().getMetricValueForTests('powersync_transactions_replicated_total')) ?? 0;
50
+ const endRowCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.ROWS_REPLICATED)) ?? 0;
51
+ const endTxCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.TRANSACTIONS_REPLICATED)) ?? 0;
53
52
  expect(endRowCount - startRowCount).toEqual(1);
54
53
  expect(endTxCount - startTxCount).toEqual(1);
55
54
  });
@@ -68,9 +67,8 @@ function defineBinlogStreamTests(factory: storage.TestStorageFactory) {
68
67
 
69
68
  await context.replicateSnapshot();
70
69
 
71
- const startRowCount = (await Metrics.getInstance().getMetricValueForTests('powersync_rows_replicated_total')) ?? 0;
72
- const startTxCount =
73
- (await Metrics.getInstance().getMetricValueForTests('powersync_transactions_replicated_total')) ?? 0;
70
+ const startRowCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.ROWS_REPLICATED)) ?? 0;
71
+ const startTxCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.TRANSACTIONS_REPLICATED)) ?? 0;
74
72
 
75
73
  context.startStreaming();
76
74
 
@@ -80,9 +78,8 @@ function defineBinlogStreamTests(factory: storage.TestStorageFactory) {
80
78
  const data = await context.getBucketData('global[]');
81
79
 
82
80
  expect(data).toMatchObject([putOp('test_DATA', { id: testId, description: 'test1' })]);
83
- const endRowCount = (await Metrics.getInstance().getMetricValueForTests('powersync_rows_replicated_total')) ?? 0;
84
- const endTxCount =
85
- (await Metrics.getInstance().getMetricValueForTests('powersync_transactions_replicated_total')) ?? 0;
81
+ const endRowCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.ROWS_REPLICATED)) ?? 0;
82
+ const endTxCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.TRANSACTIONS_REPLICATED)) ?? 0;
86
83
  expect(endRowCount - startRowCount).toEqual(1);
87
84
  expect(endTxCount - startTxCount).toEqual(1);
88
85
  });
@@ -172,11 +169,15 @@ function defineBinlogStreamTests(factory: storage.TestStorageFactory) {
172
169
  const testId = uuid();
173
170
  await connectionManager.query(`INSERT INTO test_data(id, description) VALUES('${testId}','test1')`);
174
171
 
172
+ const startRowCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.ROWS_REPLICATED)) ?? 0;
173
+
175
174
  await context.replicateSnapshot();
176
175
  context.startStreaming();
177
176
 
177
+ const endRowCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.ROWS_REPLICATED)) ?? 0;
178
178
  const data = await context.getBucketData('global[]');
179
179
  expect(data).toMatchObject([putOp('test_data', { id: testId, description: 'test1' })]);
180
+ expect(endRowCount - startRowCount).toEqual(1);
180
181
  });
181
182
 
182
183
  test('snapshot with date values', async () => {
@@ -229,9 +230,8 @@ function defineBinlogStreamTests(factory: storage.TestStorageFactory) {
229
230
 
230
231
  await context.replicateSnapshot();
231
232
 
232
- const startRowCount = (await Metrics.getInstance().getMetricValueForTests('powersync_rows_replicated_total')) ?? 0;
233
- const startTxCount =
234
- (await Metrics.getInstance().getMetricValueForTests('powersync_transactions_replicated_total')) ?? 0;
233
+ const startRowCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.ROWS_REPLICATED)) ?? 0;
234
+ const startTxCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.TRANSACTIONS_REPLICATED)) ?? 0;
235
235
 
236
236
  context.startStreaming();
237
237
 
@@ -258,9 +258,8 @@ function defineBinlogStreamTests(factory: storage.TestStorageFactory) {
258
258
  timestamp: '2023-03-06T15:47:00.000Z'
259
259
  })
260
260
  ]);
261
- const endRowCount = (await Metrics.getInstance().getMetricValueForTests('powersync_rows_replicated_total')) ?? 0;
262
- const endTxCount =
263
- (await Metrics.getInstance().getMetricValueForTests('powersync_transactions_replicated_total')) ?? 0;
261
+ const endRowCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.ROWS_REPLICATED)) ?? 0;
262
+ const endTxCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.TRANSACTIONS_REPLICATED)) ?? 0;
264
263
  expect(endRowCount - startRowCount).toEqual(2);
265
264
  expect(endTxCount - startTxCount).toEqual(2);
266
265
  });
@@ -274,9 +273,8 @@ function defineBinlogStreamTests(factory: storage.TestStorageFactory) {
274
273
 
275
274
  await context.replicateSnapshot();
276
275
 
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;
276
+ const startRowCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.ROWS_REPLICATED)) ?? 0;
277
+ const startTxCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.TRANSACTIONS_REPLICATED)) ?? 0;
280
278
 
281
279
  context.startStreaming();
282
280
 
@@ -284,9 +282,8 @@ function defineBinlogStreamTests(factory: storage.TestStorageFactory) {
284
282
  const data = await context.getBucketData('global[]');
285
283
 
286
284
  expect(data).toMatchObject([]);
287
- const endRowCount = (await Metrics.getInstance().getMetricValueForTests('powersync_rows_replicated_total')) ?? 0;
288
- const endTxCount =
289
- (await Metrics.getInstance().getMetricValueForTests('powersync_transactions_replicated_total')) ?? 0;
285
+ const endRowCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.ROWS_REPLICATED)) ?? 0;
286
+ const endTxCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetric.TRANSACTIONS_REPLICATED)) ?? 0;
290
287
 
291
288
  // There was a transaction, but we should not replicate any actual data
292
289
  expect(endRowCount - startRowCount).toEqual(0);
@@ -4,6 +4,8 @@ import { MySQLConnectionManager } from '@module/replication/MySQLConnectionManag
4
4
  import { logger } from '@powersync/lib-services-framework';
5
5
  import {
6
6
  BucketStorageFactory,
7
+ createCoreReplicationMetrics,
8
+ initializeCoreReplicationMetrics,
7
9
  InternalOpId,
8
10
  OplogEntry,
9
11
  ProtocolOpId,
@@ -11,7 +13,7 @@ import {
11
13
  storage,
12
14
  SyncRulesBucketStorage
13
15
  } from '@powersync/service-core';
14
- import { test_utils } from '@powersync/service-core-tests';
16
+ import { METRICS_HELPER, test_utils } from '@powersync/service-core-tests';
15
17
  import mysqlPromise from 'mysql2/promise';
16
18
  import { clearTestDb, TEST_CONNECTION_OPTIONS } from './util.js';
17
19
 
@@ -44,7 +46,10 @@ export class BinlogStreamTestContext {
44
46
  constructor(
45
47
  public factory: BucketStorageFactory,
46
48
  public connectionManager: MySQLConnectionManager
47
- ) {}
49
+ ) {
50
+ createCoreReplicationMetrics(METRICS_HELPER.metricsEngine);
51
+ initializeCoreReplicationMetrics(METRICS_HELPER.metricsEngine);
52
+ }
48
53
 
49
54
  async dispose() {
50
55
  this.abortController.abort();
@@ -97,6 +102,7 @@ export class BinlogStreamTestContext {
97
102
  }
98
103
  const options: BinLogStreamOptions = {
99
104
  storage: this.storage,
105
+ metrics: METRICS_HELPER.metricsEngine,
100
106
  connections: this.connectionManager,
101
107
  abortSignal: this.abortController.signal
102
108
  };
@@ -40,6 +40,9 @@ describe('MySQL Data Types', () => {
40
40
 
41
41
  char_col CHAR(10),
42
42
  varchar_col VARCHAR(255),
43
+ varchar_binary_encoding_col VARCHAR(255) CHARACTER SET binary,
44
+ varchar_with_bin_collation_col VARCHAR(255) COLLATE utf8mb4_bin,
45
+
43
46
  binary_col BINARY(16),
44
47
  varbinary_col VARBINARY(256),
45
48
  tinyblob_col TINYBLOB,
@@ -145,6 +148,8 @@ INSERT INTO test_data (
145
148
  INSERT INTO test_data (
146
149
  char_col,
147
150
  varchar_col,
151
+ varchar_binary_encoding_col,
152
+ varchar_with_bin_collation_col,
148
153
  binary_col,
149
154
  varbinary_col,
150
155
  tinyblob_col,
@@ -159,7 +164,9 @@ INSERT INTO test_data (
159
164
  ) VALUES (
160
165
  'CharData', -- CHAR(10) with padding spaces
161
166
  'Variable character data',-- VARCHAR(255)
162
- 'ShortBin', -- BINARY(16)
167
+ 'Varchar with binary encoding', -- VARCHAR(255) with binary encoding
168
+ 'Variable character data with bin collation', -- VARCHAR(255) with bin collation
169
+ 'ShortBin', -- BINARY(16)
163
170
  'VariableBinaryData', -- VARBINARY(256)
164
171
  'TinyBlobData', -- TINYBLOB
165
172
  'BlobData', -- BLOB
@@ -177,6 +184,11 @@ INSERT INTO test_data (
177
184
  const expectedResult = {
178
185
  char_col: 'CharData',
179
186
  varchar_col: 'Variable character data',
187
+ varchar_binary_encoding_col: new Uint8Array([
188
+ 86, 97, 114, 99, 104, 97, 114, 32, 119, 105, 116, 104, 32, 98, 105, 110, 97, 114, 121, 32, 101, 110, 99, 111,
189
+ 100, 105, 110, 103
190
+ ]),
191
+ varchar_with_bin_collation_col: 'Variable character data with bin collation',
180
192
  binary_col: new Uint8Array([83, 104, 111, 114, 116, 66, 105, 110, 0, 0, 0, 0, 0, 0, 0, 0]), // Pad with 0
181
193
  varbinary_col: new Uint8Array([
182
194
  0x56, 0x61, 0x72, 0x69, 0x61, 0x62, 0x6c, 0x65, 0x42, 0x69, 0x6e, 0x61, 0x72, 0x79, 0x44, 0x61, 0x74, 0x61
package/test/src/setup.ts CHANGED
@@ -1,13 +1,12 @@
1
1
  import { container } from '@powersync/lib-services-framework';
2
- import { test_utils } from '@powersync/service-core-tests';
2
+ import { METRICS_HELPER } from '@powersync/service-core-tests';
3
3
  import { beforeAll, beforeEach } from 'vitest';
4
4
 
5
5
  beforeAll(async () => {
6
6
  // Executes for every test file
7
7
  container.registerDefaults();
8
- await test_utils.initMetrics();
9
8
  });
10
9
 
11
10
  beforeEach(async () => {
12
- await test_utils.resetMetrics();
11
+ METRICS_HELPER.resetMetrics();
13
12
  });