@powersync/service-module-mssql 0.5.0 → 0.6.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.
Files changed (46) hide show
  1. package/CHANGELOG.md +32 -0
  2. package/dist/common/CaptureInstance.d.ts +14 -0
  3. package/dist/common/CaptureInstance.js +2 -0
  4. package/dist/common/CaptureInstance.js.map +1 -0
  5. package/dist/common/MSSQLSourceTable.d.ts +16 -14
  6. package/dist/common/MSSQLSourceTable.js +35 -16
  7. package/dist/common/MSSQLSourceTable.js.map +1 -1
  8. package/dist/replication/CDCPoller.d.ts +42 -20
  9. package/dist/replication/CDCPoller.js +200 -60
  10. package/dist/replication/CDCPoller.js.map +1 -1
  11. package/dist/replication/CDCReplicationJob.js +9 -1
  12. package/dist/replication/CDCReplicationJob.js.map +1 -1
  13. package/dist/replication/CDCStream.d.ts +35 -4
  14. package/dist/replication/CDCStream.js +181 -74
  15. package/dist/replication/CDCStream.js.map +1 -1
  16. package/dist/replication/MSSQLConnectionManager.js +16 -5
  17. package/dist/replication/MSSQLConnectionManager.js.map +1 -1
  18. package/dist/types/types.d.ts +4 -56
  19. package/dist/types/types.js +5 -24
  20. package/dist/types/types.js.map +1 -1
  21. package/dist/utils/deadlock.d.ts +9 -0
  22. package/dist/utils/deadlock.js +40 -0
  23. package/dist/utils/deadlock.js.map +1 -0
  24. package/dist/utils/mssql.d.ts +33 -15
  25. package/dist/utils/mssql.js +101 -99
  26. package/dist/utils/mssql.js.map +1 -1
  27. package/dist/utils/schema.d.ts +9 -0
  28. package/dist/utils/schema.js +34 -0
  29. package/dist/utils/schema.js.map +1 -1
  30. package/package.json +8 -8
  31. package/src/common/CaptureInstance.ts +15 -0
  32. package/src/common/MSSQLSourceTable.ts +33 -24
  33. package/src/replication/CDCPoller.ts +272 -72
  34. package/src/replication/CDCReplicationJob.ts +8 -1
  35. package/src/replication/CDCStream.ts +237 -90
  36. package/src/replication/MSSQLConnectionManager.ts +15 -5
  37. package/src/types/types.ts +5 -28
  38. package/src/utils/deadlock.ts +44 -0
  39. package/src/utils/mssql.ts +159 -124
  40. package/src/utils/schema.ts +43 -0
  41. package/test/src/CDCStreamTestContext.ts +9 -2
  42. package/test/src/env.ts +1 -1
  43. package/test/src/mssql-to-sqlite.test.ts +18 -10
  44. package/test/src/schema-changes.test.ts +470 -0
  45. package/test/src/util.ts +75 -12
  46. package/tsconfig.tsbuildinfo +1 -1
@@ -1,14 +1,21 @@
1
1
  import sql from 'mssql';
2
2
  import { coerce, gte } from 'semver';
3
3
  import { logger } from '@powersync/lib-services-framework';
4
+ import { retryOnDeadlock } from './deadlock.js';
4
5
  import { MSSQLConnectionManager } from '../replication/MSSQLConnectionManager.js';
5
6
  import { LSN } from '../common/LSN.js';
6
- import { CaptureInstance, MSSQLSourceTable } from '../common/MSSQLSourceTable.js';
7
+ import { MSSQLSourceTable } from '../common/MSSQLSourceTable.js';
7
8
  import { MSSQLParameter } from '../types/mssql-data-types.js';
9
+ import * as sync_rules from '@powersync/service-sync-rules';
8
10
  import { SqlSyncRules, TablePattern } from '@powersync/service-sync-rules';
9
- import { getReplicationIdentityColumns, ReplicationIdentityColumnsResult, ResolvedTable } from './schema.js';
11
+ import {
12
+ getPendingSchemaChanges,
13
+ getReplicationIdentityColumns,
14
+ ReplicationIdentityColumnsResult,
15
+ ResolvedTable
16
+ } from './schema.js';
10
17
  import * as service_types from '@powersync/service-types';
11
- import * as sync_rules from '@powersync/service-sync-rules';
18
+ import { CaptureInstance } from '../common/CaptureInstance.js';
12
19
 
13
20
  export const POWERSYNC_CHECKPOINTS_TABLE = '_powersync_checkpoints';
14
21
 
@@ -78,16 +85,16 @@ export async function checkSourceConfiguration(connectionManager: MSSQLConnectio
78
85
  }
79
86
 
80
87
  // 4) Check if the _powersync_checkpoints table is correctly configured
81
- const checkpointTableErrors = await ensurePowerSyncCheckpointsTable(connectionManager);
88
+ const checkpointTableErrors = await checkPowerSyncCheckpointsTable(connectionManager);
82
89
  errors.push(...checkpointTableErrors);
83
90
 
84
91
  return errors;
85
92
  }
86
93
 
87
- export async function ensurePowerSyncCheckpointsTable(connectionManager: MSSQLConnectionManager): Promise<string[]> {
94
+ export async function checkPowerSyncCheckpointsTable(connectionManager: MSSQLConnectionManager): Promise<string[]> {
88
95
  const errors: string[] = [];
89
96
  try {
90
- // check if the dbo_powersync_checkpoints table exists
97
+ // Check if the table exists
91
98
  const { recordset: checkpointsResult } = await connectionManager.query(
92
99
  `
93
100
  SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @schema AND TABLE_NAME = @tableName;
@@ -97,45 +104,22 @@ export async function ensurePowerSyncCheckpointsTable(connectionManager: MSSQLCo
97
104
  { name: 'tableName', type: sql.VarChar(sql.MAX), value: POWERSYNC_CHECKPOINTS_TABLE }
98
105
  ]
99
106
  );
100
- if (checkpointsResult.length > 0) {
101
- // Table already exists, check if CDC is enabled
102
- const isEnabled = await isTableEnabledForCDC({
103
- connectionManager,
104
- table: POWERSYNC_CHECKPOINTS_TABLE,
105
- schema: connectionManager.schema
106
- });
107
- if (!isEnabled) {
108
- // Enable CDC on the table
109
- await enableCDCForTable({
110
- connectionManager,
111
- table: POWERSYNC_CHECKPOINTS_TABLE
112
- });
113
- }
114
- return errors;
107
+ if (checkpointsResult.length === 0) {
108
+ throw new Error(`The ${POWERSYNC_CHECKPOINTS_TABLE} table does not exist. Please create it.`);
115
109
  }
116
- } catch (error) {
117
- errors.push(`Failed ensure ${POWERSYNC_CHECKPOINTS_TABLE} table is correctly configured: ${error}`);
118
- }
119
-
120
- // Try to create the table
121
- try {
122
- await connectionManager.query(`
123
- CREATE TABLE ${toQualifiedTableName(connectionManager.schema, POWERSYNC_CHECKPOINTS_TABLE)} (
124
- id INT IDENTITY PRIMARY KEY,
125
- last_updated DATETIME NOT NULL DEFAULT (GETDATE())
126
- )`);
127
- } catch (error) {
128
- errors.push(`Failed to create ${POWERSYNC_CHECKPOINTS_TABLE} table: ${error}`);
129
- }
130
-
131
- try {
132
- // Enable CDC on the table if not already enabled
133
- await enableCDCForTable({
110
+ // Check if CDC is enabled
111
+ const isEnabled = await isTableEnabledForCDC({
134
112
  connectionManager,
135
- table: POWERSYNC_CHECKPOINTS_TABLE
113
+ table: POWERSYNC_CHECKPOINTS_TABLE,
114
+ schema: connectionManager.schema
136
115
  });
116
+ if (!isEnabled) {
117
+ throw new Error(
118
+ `The ${POWERSYNC_CHECKPOINTS_TABLE} table exists but is not enabled for CDC. Please enable CDC on this table.`
119
+ );
120
+ }
137
121
  } catch (error) {
138
- errors.push(`Failed to enable CDC on ${POWERSYNC_CHECKPOINTS_TABLE} table: ${error}`);
122
+ errors.push(`Failed ensure ${POWERSYNC_CHECKPOINTS_TABLE} table is correctly configured: ${error}`);
139
123
  }
140
124
 
141
125
  return errors;
@@ -165,36 +149,9 @@ export interface IsTableEnabledForCDCOptions {
165
149
  export async function isTableEnabledForCDC(options: IsTableEnabledForCDCOptions): Promise<boolean> {
166
150
  const { connectionManager, table, schema } = options;
167
151
 
168
- const { recordset: checkResult } = await connectionManager.query(
169
- `
170
- SELECT 1 FROM cdc.change_tables ct
171
- JOIN sys.tables AS tbl ON tbl.object_id = ct.source_object_id
172
- JOIN sys.schemas AS sch ON sch.schema_id = tbl.schema_id
173
- WHERE sch.name = @schema
174
- AND tbl.name = @tableName
175
- `,
176
- [
177
- { name: 'schema', type: sql.VarChar(sql.MAX), value: schema },
178
- { name: 'tableName', type: sql.VarChar(sql.MAX), value: table }
179
- ]
180
- );
181
- return checkResult.length > 0;
182
- }
183
-
184
- export interface EnableCDCForTableOptions {
185
- connectionManager: MSSQLConnectionManager;
186
- table: string;
187
- }
188
-
189
- export async function enableCDCForTable(options: EnableCDCForTableOptions): Promise<void> {
190
- const { connectionManager, table } = options;
152
+ const captureInstance = await getCaptureInstance({ connectionManager, table: { schema, name: table } });
191
153
 
192
- await connectionManager.execute('sys.sp_cdc_enable_table', [
193
- { name: 'source_schema', value: connectionManager.schema },
194
- { name: 'source_name', value: table },
195
- { name: 'role_name', value: 'NULL' },
196
- { name: 'supports_net_changes', value: 1 }
197
- ]);
154
+ return captureInstance != null;
198
155
  }
199
156
 
200
157
  /**
@@ -216,23 +173,28 @@ export interface IsWithinRetentionThresholdOptions {
216
173
  }
217
174
 
218
175
  /**
219
- * Checks that CDC the specified checkpoint LSN is within the retention threshold for all specified tables.
176
+ * Checks that the given checkpoint LSN is still within the retention threshold of the source table capture instances.
220
177
  * CDC periodically cleans up old data up to the retention threshold. If replication has been stopped for too long it is
221
178
  * possible for the checkpoint LSN to be older than the minimum LSN in the CDC tables. In such a case we need to perform a new snapshot.
222
179
  * @param options
223
180
  */
224
- export async function isWithinRetentionThreshold(options: IsWithinRetentionThresholdOptions): Promise<boolean> {
181
+ export async function checkRetentionThresholds(
182
+ options: IsWithinRetentionThresholdOptions
183
+ ): Promise<MSSQLSourceTable[]> {
225
184
  const { checkpointLSN, tables, connectionManager } = options;
185
+ const tablesOutsideRetentionThreshold: MSSQLSourceTable[] = [];
226
186
  for (const table of tables) {
227
- const minLSN = await getMinLSN(connectionManager, table.captureInstance);
228
- if (minLSN > checkpointLSN) {
229
- logger.warn(
230
- `The checkpoint LSN:[${checkpointLSN}] is older than the minimum LSN:[${minLSN}] for table ${table.sourceTable.qualifiedName}. This indicates that the checkpoint LSN is outside of the retention window.`
231
- );
232
- return false;
187
+ if (table.enabledForCDC()) {
188
+ const minLSN = await getMinLSN(connectionManager, table.captureInstance!.name);
189
+ if (minLSN > checkpointLSN) {
190
+ logger.warn(
191
+ `The checkpoint LSN:[${checkpointLSN}] is older than the minimum LSN:[${minLSN}] for table ${table.toQualifiedName()}. This indicates that the checkpoint LSN is outside of the retention window.`
192
+ );
193
+ tablesOutsideRetentionThreshold.push(table);
194
+ }
233
195
  }
234
196
  }
235
- return true;
197
+ return tablesOutsideRetentionThreshold;
236
198
  }
237
199
 
238
200
  export async function getMinLSN(connectionManager: MSSQLConnectionManager, captureInstance: string): Promise<LSN> {
@@ -252,43 +214,6 @@ export async function incrementLSN(lsn: LSN, connectionManager: MSSQLConnectionM
252
214
  return LSN.fromBinary(result[0].incremented_lsn);
253
215
  }
254
216
 
255
- export interface GetCaptureInstanceOptions {
256
- connectionManager: MSSQLConnectionManager;
257
- tableName: string;
258
- schema: string;
259
- }
260
-
261
- export async function getCaptureInstance(options: GetCaptureInstanceOptions): Promise<CaptureInstance | null> {
262
- const { connectionManager, tableName, schema } = options;
263
- const { recordset: result } = await connectionManager.query(
264
- `
265
- SELECT
266
- ct.capture_instance,
267
- OBJECT_SCHEMA_NAME(ct.[object_id]) AS cdc_schema
268
- FROM
269
- sys.tables tbl
270
- INNER JOIN sys.schemas sch ON tbl.schema_id = sch.schema_id
271
- INNER JOIN cdc.change_tables ct ON ct.source_object_id = tbl.object_id
272
- WHERE sch.name = @schema
273
- AND tbl.name = @tableName
274
- AND ct.end_lsn IS NULL;
275
- `,
276
- [
277
- { name: 'schema', type: sql.VarChar(sql.MAX), value: schema },
278
- { name: 'tableName', type: sql.VarChar(sql.MAX), value: tableName }
279
- ]
280
- );
281
-
282
- if (result.length === 0) {
283
- return null;
284
- }
285
-
286
- return {
287
- name: result[0].capture_instance,
288
- schema: result[0].cdc_schema
289
- };
290
- }
291
-
292
217
  /**
293
218
  * Return the LSN of the latest transaction recorded in the transaction log
294
219
  * @param connectionManager
@@ -406,18 +331,28 @@ export async function getDebugTableInfo(options: GetDebugTableInfoOptions): Prom
406
331
  selectError = { level: 'fatal', message: e.message };
407
332
  }
408
333
 
409
- // Check if CDC is enabled for the table
410
334
  let cdcError: service_types.ReplicationError | null = null;
335
+ let schemaDriftError: service_types.ReplicationError | null = null;
411
336
  try {
412
- const isEnabled = await isTableEnabledForCDC({
337
+ const captureInstanceDetails = await getCaptureInstance({
413
338
  connectionManager: connectionManager,
414
- table: table.name,
415
- schema: schema
339
+ table: {
340
+ schema: schema,
341
+ name: table.name
342
+ }
416
343
  });
417
- if (!isEnabled) {
344
+ if (captureInstanceDetails == null) {
418
345
  cdcError = {
419
- level: 'fatal',
420
- message: `CDC is not enabled for table ${toQualifiedTableName(schema, table.name)}. Enable CDC with: sys.sp_cdc_enable_table @source_schema = '${schema}', @source_name = '${table.name}', @role_name = NULL, @supports_net_changes = 1`
346
+ level: 'warning',
347
+ message: `CDC is not enabled for table ${toQualifiedTableName(schema, table.name)}. Please enable CDC on the table to capture changes.`
348
+ };
349
+ }
350
+
351
+ if (captureInstanceDetails && captureInstanceDetails.instances[0].pendingSchemaChanges.length > 0) {
352
+ schemaDriftError = {
353
+ level: 'warning',
354
+ message: `Source table ${toQualifiedTableName(schema, table.name)} has schema changes not reflected in the CDC capture instance. Please disable and re-enable CDC on the source table to update the capture instance schema.
355
+ Pending schema changes: ${captureInstanceDetails.instances[0].pendingSchemaChanges.join(', \n')}`
421
356
  };
422
357
  }
423
358
  } catch (e) {
@@ -433,6 +368,106 @@ export async function getDebugTableInfo(options: GetDebugTableInfoOptions): Prom
433
368
  replication_id: idColumns.map((c) => c.name),
434
369
  data_queries: syncData,
435
370
  parameter_queries: syncParameters,
436
- errors: [idColumnsError, selectError, cdcError].filter((error) => error != null) as service_types.ReplicationError[]
371
+ errors: [idColumnsError, selectError, cdcError, schemaDriftError].filter(
372
+ (error) => error != null
373
+ ) as service_types.ReplicationError[]
374
+ };
375
+ }
376
+
377
+ // Describes the capture instances linked to a source table.
378
+ export interface CaptureInstanceDetails {
379
+ sourceTable: {
380
+ schema: string;
381
+ name: string;
382
+ objectId: number;
383
+ };
384
+
385
+ /**
386
+ * The capture instances for the source table.
387
+ * The instances are sorted by create date in descending order.
388
+ */
389
+ instances: CaptureInstance[];
390
+ }
391
+
392
+ export interface GetCaptureInstancesOptions {
393
+ connectionManager: MSSQLConnectionManager;
394
+ table?: {
395
+ schema: string;
396
+ name: string;
397
+ };
398
+ }
399
+
400
+ export async function getCaptureInstances(
401
+ options: GetCaptureInstancesOptions
402
+ ): Promise<Map<number, CaptureInstanceDetails>> {
403
+ return retryOnDeadlock(async () => {
404
+ const { connectionManager, table } = options;
405
+ const instances = new Map<number, CaptureInstanceDetails>();
406
+
407
+ const { recordset: results } = table
408
+ ? await connectionManager.execute('sys.sp_cdc_help_change_data_capture', [
409
+ { name: 'source_schema', value: table.schema },
410
+ { name: 'source_name', value: table.name }
411
+ ])
412
+ : await connectionManager.execute('sys.sp_cdc_help_change_data_capture', []);
413
+
414
+ if (results.length === 0) {
415
+ return new Map<number, CaptureInstanceDetails>();
416
+ }
417
+
418
+ for (const row of results) {
419
+ const instance: CaptureInstance = {
420
+ name: row.capture_instance,
421
+ objectId: row.object_id,
422
+ minLSN: LSN.fromBinary(row.start_lsn),
423
+ createDate: new Date(row.create_date),
424
+ pendingSchemaChanges: []
425
+ };
426
+
427
+ instance.pendingSchemaChanges = await getPendingSchemaChanges({
428
+ connectionManager: connectionManager,
429
+ captureInstanceName: instance.name
430
+ });
431
+
432
+ const sourceTable = {
433
+ schema: row.source_schema,
434
+ name: row.source_table,
435
+ objectId: row.source_object_id
436
+ };
437
+
438
+ // There can only ever be 2 capture instances active at any given time for a source table.
439
+ if (instances.has(row.source_object_id)) {
440
+ if (instance.createDate > instances.get(row.source_object_id)!.instances[0].createDate) {
441
+ instances.get(row.source_object_id)!.instances.unshift(instance);
442
+ } else {
443
+ instances.get(row.source_object_id)!.instances.push(instance);
444
+ }
445
+ } else {
446
+ instances.set(row.source_object_id, {
447
+ instances: [instance],
448
+ sourceTable
449
+ });
450
+ }
451
+ }
452
+
453
+ return instances;
454
+ }, 'getCaptureInstances');
455
+ }
456
+
457
+ export interface GetCaptureInstanceOptions {
458
+ connectionManager: MSSQLConnectionManager;
459
+ table: {
460
+ schema: string;
461
+ name: string;
437
462
  };
438
463
  }
464
+ export async function getCaptureInstance(options: GetCaptureInstanceOptions): Promise<CaptureInstanceDetails | null> {
465
+ const { connectionManager, table } = options;
466
+ const instances = await getCaptureInstances({ connectionManager, table });
467
+
468
+ if (instances.size === 0) {
469
+ return null;
470
+ }
471
+
472
+ return instances.values().next().value!;
473
+ }
@@ -3,6 +3,7 @@ import { TablePattern } from '@powersync/service-sync-rules';
3
3
  import { MSSQLConnectionManager } from '../replication/MSSQLConnectionManager.js';
4
4
  import { MSSQLColumnDescriptor } from '../types/mssql-data-types.js';
5
5
  import sql from 'mssql';
6
+ import { logger } from '@powersync/lib-services-framework';
6
7
 
7
8
  export interface GetColumnsOptions {
8
9
  connectionManager: MSSQLConnectionManager;
@@ -198,3 +199,45 @@ export async function getTablesFromPattern(
198
199
  });
199
200
  }
200
201
  }
202
+
203
+ export interface GetPendingSchemaChangesOptions {
204
+ connectionManager: MSSQLConnectionManager;
205
+ captureInstanceName: string;
206
+ }
207
+
208
+ /**
209
+ * Returns the DDL commands that have been applied to the source table since the capture instance was created.
210
+ */
211
+ export async function getPendingSchemaChanges(options: GetPendingSchemaChangesOptions): Promise<string[]> {
212
+ const { connectionManager, captureInstanceName } = options;
213
+
214
+ try {
215
+ const { recordset: results } = await connectionManager.execute('sys.sp_cdc_get_ddl_history', [
216
+ { name: 'capture_instance', type: sql.VarChar(sql.MAX), value: captureInstanceName }
217
+ ]);
218
+ return results.map((row) => row.ddl_command);
219
+ } catch (e) {
220
+ if (isObjectNotExistError(e)) {
221
+ // Defensive check to cover the case where the capture instance metadata is temporarily unavailable.
222
+ logger.warn(`Unable to retrieve schema changes for capture instance: [${captureInstanceName}].`);
223
+ return [];
224
+ }
225
+ throw e;
226
+ }
227
+ }
228
+
229
+ function isObjectNotExistError(error: unknown): boolean {
230
+ if (error != null && typeof error === 'object' && 'number' in error) {
231
+ // SQL Server Object does not exist or access is denied error number.
232
+ return (error as { number: unknown }).number === 22981;
233
+ }
234
+ return false;
235
+ }
236
+
237
+ export async function tableExists(tableId: number, connectionManager: MSSQLConnectionManager): Promise<boolean> {
238
+ const { recordset: results } = await connectionManager.query(`SELECT 1 FROM sys.tables WHERE object_id = @tableId`, [
239
+ { name: 'tableId', type: sql.Int, value: tableId }
240
+ ]);
241
+
242
+ return results.length > 0;
243
+ }
@@ -129,6 +129,7 @@ export class CDCStreamTestContext implements AsyncDisposable {
129
129
  pollingIntervalMs: 1000,
130
130
  trustServerCertificate: true
131
131
  },
132
+ schemaCheckIntervalMs: 500,
132
133
  ...this.cdcStreamOptions
133
134
  };
134
135
  this._cdcStream = new CDCStream(options);
@@ -140,7 +141,7 @@ export class CDCStreamTestContext implements AsyncDisposable {
140
141
  */
141
142
  async initializeReplication() {
142
143
  await this.replicateSnapshot();
143
- // TODO: renable this.startStreaming();
144
+ await this.startStreaming();
144
145
  // Make sure we're up to date
145
146
  await this.getCheckpoint();
146
147
  }
@@ -150,11 +151,12 @@ export class CDCStreamTestContext implements AsyncDisposable {
150
151
  this.replicationDone = true;
151
152
  }
152
153
 
153
- // TODO: Enable once streaming is implemented
154
154
  startStreaming() {
155
155
  if (!this.replicationDone) {
156
156
  throw new Error('Call replicateSnapshot() before startStreaming()');
157
157
  }
158
+
159
+ this.cdcStream.isStartingReplication = true;
158
160
  this.streamPromise = this.cdcStream.streamChanges();
159
161
  // Wait for the replication to start before returning.
160
162
  // This avoids a bunch of unpredictable race conditions that appear in testing
@@ -212,6 +214,11 @@ export class CDCStreamTestContext implements AsyncDisposable {
212
214
  return data;
213
215
  }
214
216
 
217
+ async getFinalBucketState(bucket: string) {
218
+ const data = await this.getBucketData(bucket);
219
+ return test_utils.reduceBucket(data).slice(1);
220
+ }
221
+
215
222
  /**
216
223
  * This does not wait for a client checkpoint.
217
224
  */
package/test/src/env.ts CHANGED
@@ -5,7 +5,7 @@ export const env = utils.collectEnvironmentVariables({
5
5
  MONGO_TEST_URL: utils.type.string.default('mongodb://localhost:27017/powersync_test'),
6
6
  CI: utils.type.boolean.default('false'),
7
7
  SLOW_TESTS: utils.type.boolean.default('false'),
8
- PG_STORAGE_TEST_URL: utils.type.string.default('postgres://postgres:postgres@localhost:5431/powersync_storage_test'),
8
+ PG_STORAGE_TEST_URL: utils.type.string.default('postgres://postgres:postgres@localhost:5432/powersync_storage_test'),
9
9
  TEST_MONGO_STORAGE: utils.type.boolean.default('true'),
10
10
  TEST_POSTGRES_STORAGE: utils.type.boolean.default('true')
11
11
  });
@@ -6,19 +6,24 @@ import {
6
6
  TimeValuePrecision
7
7
  } from '@powersync/service-sync-rules';
8
8
  import { afterAll, beforeEach, describe, expect, test } from 'vitest';
9
- import { clearTestDb, createUpperCaseUUID, TEST_CONNECTION_OPTIONS, waitForPendingCDCChanges } from './util.js';
9
+ import {
10
+ clearTestDb,
11
+ createUpperCaseUUID,
12
+ enableCDCForTable,
13
+ TEST_CONNECTION_OPTIONS,
14
+ waitForPendingCDCChanges
15
+ } from './util.js';
10
16
  import { CDCToSqliteRow, toSqliteInputRow } from '@module/common/mssqls-to-sqlite.js';
11
17
  import { MSSQLConnectionManager } from '@module/replication/MSSQLConnectionManager.js';
12
18
  import {
13
- enableCDCForTable,
14
19
  escapeIdentifier,
15
- getCaptureInstance,
20
+ getCaptureInstances,
16
21
  getLatestLSN,
17
22
  getLatestReplicatedLSN,
18
- getMinLSN,
19
23
  toQualifiedTableName
20
24
  } from '@module/utils/mssql.js';
21
25
  import sql from 'mssql';
26
+ import { CDC_SCHEMA } from '@module/common/MSSQLSourceTable.js';
22
27
 
23
28
  describe('MSSQL Data Types Tests', () => {
24
29
  const connectionManager = new MSSQLConnectionManager(TEST_CONNECTION_OPTIONS, {});
@@ -482,20 +487,23 @@ async function getReplicatedRows(
482
487
  ): Promise<SqliteInputRow[]> {
483
488
  const endLSN = await getLatestReplicatedLSN(connectionManager);
484
489
 
485
- const captureInstance = await getCaptureInstance({
490
+ const captureInstances = await getCaptureInstances({
486
491
  connectionManager,
487
- schema: connectionManager.schema,
488
- tableName
492
+ table: {
493
+ schema: connectionManager.schema,
494
+ name: tableName
495
+ }
489
496
  });
490
- if (!captureInstance) {
497
+ if (captureInstances.size === 0) {
491
498
  throw new Error(`No CDC capture instance found for table ${tableName}`);
492
499
  }
493
500
 
494
- const startLSN = await getMinLSN(connectionManager, captureInstance.name);
501
+ const captureInstance = Array.from(captureInstances.values())[0].instances[0];
502
+ const startLSN = captureInstance.minLSN;
495
503
  // Query CDC changes
496
504
  const { recordset: results } = await connectionManager.query(
497
505
  `
498
- SELECT * FROM ${captureInstance.schema}.fn_cdc_get_all_changes_${captureInstance.name}(@from_lsn, @to_lsn, 'all update old') ORDER BY __$start_lsn, __$seqval
506
+ SELECT * FROM ${CDC_SCHEMA}.fn_cdc_get_all_changes_${captureInstance.name}(@from_lsn, @to_lsn, 'all update old') ORDER BY __$start_lsn, __$seqval
499
507
  `,
500
508
  [
501
509
  { name: 'from_lsn', type: sql.VarBinary, value: startLSN.toBinary() },