@powersync/service-module-mssql 0.4.0 → 0.6.0
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.
- package/CHANGELOG.md +45 -0
- package/dist/common/CaptureInstance.d.ts +14 -0
- package/dist/common/CaptureInstance.js +2 -0
- package/dist/common/CaptureInstance.js.map +1 -0
- package/dist/common/MSSQLSourceTable.d.ts +16 -14
- package/dist/common/MSSQLSourceTable.js +35 -16
- package/dist/common/MSSQLSourceTable.js.map +1 -1
- package/dist/replication/CDCPoller.d.ts +42 -20
- package/dist/replication/CDCPoller.js +200 -60
- package/dist/replication/CDCPoller.js.map +1 -1
- package/dist/replication/CDCReplicationJob.js +9 -1
- package/dist/replication/CDCReplicationJob.js.map +1 -1
- package/dist/replication/CDCStream.d.ts +35 -4
- package/dist/replication/CDCStream.js +188 -77
- package/dist/replication/CDCStream.js.map +1 -1
- package/dist/replication/MSSQLConnectionManager.js +16 -5
- package/dist/replication/MSSQLConnectionManager.js.map +1 -1
- package/dist/types/types.d.ts +4 -56
- package/dist/types/types.js +5 -24
- package/dist/types/types.js.map +1 -1
- package/dist/utils/deadlock.d.ts +9 -0
- package/dist/utils/deadlock.js +40 -0
- package/dist/utils/deadlock.js.map +1 -0
- package/dist/utils/mssql.d.ts +33 -15
- package/dist/utils/mssql.js +101 -99
- package/dist/utils/mssql.js.map +1 -1
- package/dist/utils/schema.d.ts +9 -0
- package/dist/utils/schema.js +34 -0
- package/dist/utils/schema.js.map +1 -1
- package/package.json +8 -8
- package/src/common/CaptureInstance.ts +15 -0
- package/src/common/MSSQLSourceTable.ts +33 -24
- package/src/replication/CDCPoller.ts +272 -72
- package/src/replication/CDCReplicationJob.ts +8 -1
- package/src/replication/CDCStream.ts +245 -96
- package/src/replication/MSSQLConnectionManager.ts +15 -5
- package/src/types/types.ts +5 -28
- package/src/utils/deadlock.ts +44 -0
- package/src/utils/mssql.ts +159 -124
- package/src/utils/schema.ts +43 -0
- package/test/src/CDCStream.test.ts +3 -1
- package/test/src/CDCStreamTestContext.ts +28 -7
- package/test/src/CDCStream_resumable_snapshot.test.ts +9 -7
- package/test/src/env.ts +1 -1
- package/test/src/mssql-to-sqlite.test.ts +18 -10
- package/test/src/schema-changes.test.ts +470 -0
- package/test/src/util.ts +84 -15
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { logger } from '@powersync/lib-services-framework';
|
|
2
|
+
import timers from 'timers/promises';
|
|
3
|
+
|
|
4
|
+
const MSSQL_DEADLOCK_RETRIES = 5;
|
|
5
|
+
const MSSQL_DEADLOCK_BACKOFF_FACTOR = 2;
|
|
6
|
+
const MSSQL_DEADLOCK_RETRY_DELAY_MS = 200;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Retries the given async function if it fails with a SQL Server deadlock error (1205).
|
|
10
|
+
* Deadlocks, while uncommon, can occur when CDC altering functions are being called whilst actively replicating
|
|
11
|
+
* using CDC functions.
|
|
12
|
+
*
|
|
13
|
+
* If the error is not a deadlock or all retries are exhausted, the error is re-thrown.
|
|
14
|
+
*/
|
|
15
|
+
export async function retryOnDeadlock<T>(fn: () => Promise<T>, operationName: string): Promise<T> {
|
|
16
|
+
let lastError: Error | null = null;
|
|
17
|
+
for (let attempt = 0; attempt <= MSSQL_DEADLOCK_RETRIES; attempt++) {
|
|
18
|
+
try {
|
|
19
|
+
return await fn();
|
|
20
|
+
} catch (error) {
|
|
21
|
+
lastError = error;
|
|
22
|
+
if (!isDeadlockError(error) || attempt === MSSQL_DEADLOCK_RETRIES) {
|
|
23
|
+
throw error;
|
|
24
|
+
}
|
|
25
|
+
const delay = MSSQL_DEADLOCK_RETRY_DELAY_MS * Math.pow(MSSQL_DEADLOCK_BACKOFF_FACTOR, attempt);
|
|
26
|
+
logger.warn(
|
|
27
|
+
`Deadlock detected during ${operationName} (attempt ${attempt + 1}/${MSSQL_DEADLOCK_RETRIES}). Retrying in ${delay}ms...`
|
|
28
|
+
);
|
|
29
|
+
await timers.setTimeout(delay);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
throw lastError;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function isDeadlockError(error: unknown): boolean {
|
|
37
|
+
if (error != null && typeof error === 'object' && 'number' in error) {
|
|
38
|
+
// SQL Server deadlock victim error number.
|
|
39
|
+
// When SQL Server detects a deadlock, it chooses one of the participating transactions
|
|
40
|
+
// as the "deadlock victim" and terminates it with error 1205.
|
|
41
|
+
return (error as { number: unknown }).number === 1205;
|
|
42
|
+
}
|
|
43
|
+
return false;
|
|
44
|
+
}
|
package/src/utils/mssql.ts
CHANGED
|
@@ -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 {
|
|
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 {
|
|
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
|
|
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
|
|
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
|
|
94
|
+
export async function checkPowerSyncCheckpointsTable(connectionManager: MSSQLConnectionManager): Promise<string[]> {
|
|
88
95
|
const errors: string[] = [];
|
|
89
96
|
try {
|
|
90
|
-
//
|
|
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
|
|
101
|
-
|
|
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
|
-
|
|
117
|
-
|
|
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
|
|
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 {
|
|
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
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
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
|
|
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
|
|
337
|
+
const captureInstanceDetails = await getCaptureInstance({
|
|
413
338
|
connectionManager: connectionManager,
|
|
414
|
-
table:
|
|
415
|
-
|
|
339
|
+
table: {
|
|
340
|
+
schema: schema,
|
|
341
|
+
name: table.name
|
|
342
|
+
}
|
|
416
343
|
});
|
|
417
|
-
if (
|
|
344
|
+
if (captureInstanceDetails == null) {
|
|
418
345
|
cdcError = {
|
|
419
|
-
level: '
|
|
420
|
-
message: `CDC is not enabled for table ${toQualifiedTableName(schema, table.name)}.
|
|
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(
|
|
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
|
+
}
|
package/src/utils/schema.ts
CHANGED
|
@@ -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
|
+
}
|
|
@@ -18,7 +18,9 @@ describe('CDCStream tests', () => {
|
|
|
18
18
|
describeWithStorage({ timeout: 20_000 }, defineCDCStreamTests);
|
|
19
19
|
});
|
|
20
20
|
|
|
21
|
-
function defineCDCStreamTests(
|
|
21
|
+
function defineCDCStreamTests(config: storage.TestStorageConfig) {
|
|
22
|
+
const { factory } = config;
|
|
23
|
+
|
|
22
24
|
test('Initial snapshot sync', async () => {
|
|
23
25
|
await using context = await CDCStreamTestContext.open(factory);
|
|
24
26
|
const { connectionManager } = context;
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
SyncRulesBucketStorage,
|
|
10
10
|
updateSyncRulesFromYaml
|
|
11
11
|
} from '@powersync/service-core';
|
|
12
|
-
import { METRICS_HELPER, test_utils } from '@powersync/service-core-tests';
|
|
12
|
+
import { bucketRequest, METRICS_HELPER, test_utils } from '@powersync/service-core-tests';
|
|
13
13
|
import { clearTestDb, getClientCheckpoint, TEST_CONNECTION_OPTIONS } from './util.js';
|
|
14
14
|
import { CDCStream, CDCStreamOptions } from '@module/replication/CDCStream.js';
|
|
15
15
|
import { MSSQLConnectionManager } from '@module/replication/MSSQLConnectionManager.js';
|
|
@@ -26,6 +26,7 @@ export class CDCStreamTestContext implements AsyncDisposable {
|
|
|
26
26
|
private _cdcStream?: CDCStream;
|
|
27
27
|
private abortController = new AbortController();
|
|
28
28
|
private streamPromise?: Promise<void>;
|
|
29
|
+
private syncRulesContent?: storage.PersistedSyncRulesContent;
|
|
29
30
|
public storage?: SyncRulesBucketStorage;
|
|
30
31
|
private snapshotPromise?: Promise<void>;
|
|
31
32
|
private replicationDone = false;
|
|
@@ -77,6 +78,7 @@ export class CDCStreamTestContext implements AsyncDisposable {
|
|
|
77
78
|
const syncRules = await this.factory.updateSyncRules(
|
|
78
79
|
updateSyncRulesFromYaml(content, { validate: true, storageVersion: LEGACY_STORAGE_VERSION })
|
|
79
80
|
);
|
|
81
|
+
this.syncRulesContent = syncRules;
|
|
80
82
|
this.storage = this.factory.getInstance(syncRules);
|
|
81
83
|
return this.storage!;
|
|
82
84
|
}
|
|
@@ -87,6 +89,7 @@ export class CDCStreamTestContext implements AsyncDisposable {
|
|
|
87
89
|
throw new Error(`Next sync rules not available`);
|
|
88
90
|
}
|
|
89
91
|
|
|
92
|
+
this.syncRulesContent = syncRules;
|
|
90
93
|
this.storage = this.factory.getInstance(syncRules);
|
|
91
94
|
return this.storage!;
|
|
92
95
|
}
|
|
@@ -97,10 +100,18 @@ export class CDCStreamTestContext implements AsyncDisposable {
|
|
|
97
100
|
throw new Error(`Active sync rules not available`);
|
|
98
101
|
}
|
|
99
102
|
|
|
103
|
+
this.syncRulesContent = syncRules;
|
|
100
104
|
this.storage = this.factory.getInstance(syncRules);
|
|
101
105
|
return this.storage!;
|
|
102
106
|
}
|
|
103
107
|
|
|
108
|
+
private getSyncRulesContent(): storage.PersistedSyncRulesContent {
|
|
109
|
+
if (this.syncRulesContent == null) {
|
|
110
|
+
throw new Error('Sync rules not configured - call updateSyncRules() first');
|
|
111
|
+
}
|
|
112
|
+
return this.syncRulesContent;
|
|
113
|
+
}
|
|
114
|
+
|
|
104
115
|
get cdcStream() {
|
|
105
116
|
if (this.storage == null) {
|
|
106
117
|
throw new Error('updateSyncRules() first');
|
|
@@ -118,6 +129,7 @@ export class CDCStreamTestContext implements AsyncDisposable {
|
|
|
118
129
|
pollingIntervalMs: 1000,
|
|
119
130
|
trustServerCertificate: true
|
|
120
131
|
},
|
|
132
|
+
schemaCheckIntervalMs: 500,
|
|
121
133
|
...this.cdcStreamOptions
|
|
122
134
|
};
|
|
123
135
|
this._cdcStream = new CDCStream(options);
|
|
@@ -129,7 +141,7 @@ export class CDCStreamTestContext implements AsyncDisposable {
|
|
|
129
141
|
*/
|
|
130
142
|
async initializeReplication() {
|
|
131
143
|
await this.replicateSnapshot();
|
|
132
|
-
|
|
144
|
+
await this.startStreaming();
|
|
133
145
|
// Make sure we're up to date
|
|
134
146
|
await this.getCheckpoint();
|
|
135
147
|
}
|
|
@@ -139,11 +151,12 @@ export class CDCStreamTestContext implements AsyncDisposable {
|
|
|
139
151
|
this.replicationDone = true;
|
|
140
152
|
}
|
|
141
153
|
|
|
142
|
-
// TODO: Enable once streaming is implemented
|
|
143
154
|
startStreaming() {
|
|
144
155
|
if (!this.replicationDone) {
|
|
145
156
|
throw new Error('Call replicateSnapshot() before startStreaming()');
|
|
146
157
|
}
|
|
158
|
+
|
|
159
|
+
this.cdcStream.isStartingReplication = true;
|
|
147
160
|
this.streamPromise = this.cdcStream.streamChanges();
|
|
148
161
|
// Wait for the replication to start before returning.
|
|
149
162
|
// This avoids a bunch of unpredictable race conditions that appear in testing
|
|
@@ -171,7 +184,8 @@ export class CDCStreamTestContext implements AsyncDisposable {
|
|
|
171
184
|
|
|
172
185
|
async getBucketsDataBatch(buckets: Record<string, InternalOpId>, options?: { timeout?: number }) {
|
|
173
186
|
let checkpoint = await this.getCheckpoint(options);
|
|
174
|
-
const
|
|
187
|
+
const syncRules = this.getSyncRulesContent();
|
|
188
|
+
const map = Object.entries(buckets).map(([bucket, start]) => bucketRequest(syncRules, bucket, start));
|
|
175
189
|
return test_utils.fromAsync(this.storage!.getBucketDataBatch(checkpoint, map));
|
|
176
190
|
}
|
|
177
191
|
|
|
@@ -183,8 +197,9 @@ export class CDCStreamTestContext implements AsyncDisposable {
|
|
|
183
197
|
if (typeof start == 'string') {
|
|
184
198
|
start = BigInt(start);
|
|
185
199
|
}
|
|
200
|
+
const syncRules = this.getSyncRulesContent();
|
|
186
201
|
const checkpoint = await this.getCheckpoint(options);
|
|
187
|
-
|
|
202
|
+
let map = [bucketRequest(syncRules, bucket, start)];
|
|
188
203
|
let data: OplogEntry[] = [];
|
|
189
204
|
while (true) {
|
|
190
205
|
const batch = this.storage!.getBucketDataBatch(checkpoint, map);
|
|
@@ -194,11 +209,16 @@ export class CDCStreamTestContext implements AsyncDisposable {
|
|
|
194
209
|
if (batches.length == 0 || !batches[0]!.chunkData.has_more) {
|
|
195
210
|
break;
|
|
196
211
|
}
|
|
197
|
-
map
|
|
212
|
+
map = [bucketRequest(syncRules, bucket, BigInt(batches[0]!.chunkData.next_after))];
|
|
198
213
|
}
|
|
199
214
|
return data;
|
|
200
215
|
}
|
|
201
216
|
|
|
217
|
+
async getFinalBucketState(bucket: string) {
|
|
218
|
+
const data = await this.getBucketData(bucket);
|
|
219
|
+
return test_utils.reduceBucket(data).slice(1);
|
|
220
|
+
}
|
|
221
|
+
|
|
202
222
|
/**
|
|
203
223
|
* This does not wait for a client checkpoint.
|
|
204
224
|
*/
|
|
@@ -208,7 +228,8 @@ export class CDCStreamTestContext implements AsyncDisposable {
|
|
|
208
228
|
start = BigInt(start);
|
|
209
229
|
}
|
|
210
230
|
const { checkpoint } = await this.storage!.getCheckpoint();
|
|
211
|
-
const
|
|
231
|
+
const syncRules = this.getSyncRulesContent();
|
|
232
|
+
const map = [bucketRequest(syncRules, bucket, start)];
|
|
212
233
|
const batch = this.storage!.getBucketDataBatch(checkpoint, map);
|
|
213
234
|
const batches = await test_utils.fromAsync(batch);
|
|
214
235
|
return batches[0]?.chunkData.data ?? [];
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { describe, expect, test } from 'vitest';
|
|
2
2
|
import { env } from './env.js';
|
|
3
3
|
import { createTestTableWithBasicId, describeWithStorage, waitForPendingCDCChanges } from './util.js';
|
|
4
|
-
import { TestStorageFactory } from '@powersync/service-core';
|
|
4
|
+
import { TestStorageConfig, TestStorageFactory } from '@powersync/service-core';
|
|
5
5
|
import { METRICS_HELPER } from '@powersync/service-core-tests';
|
|
6
6
|
import { ReplicationMetric } from '@powersync/service-types';
|
|
7
7
|
import * as timers from 'node:timers/promises';
|
|
@@ -10,19 +10,19 @@ import { CDCStreamTestContext } from './CDCStreamTestContext.js';
|
|
|
10
10
|
import { getLatestLSN } from '@module/utils/mssql.js';
|
|
11
11
|
|
|
12
12
|
describe.skipIf(!(env.CI || env.SLOW_TESTS))('batch replication', function () {
|
|
13
|
-
describeWithStorage({ timeout: 240_000 }, function (
|
|
13
|
+
describeWithStorage({ timeout: 240_000 }, function (config) {
|
|
14
14
|
test('resuming initial replication (1)', async () => {
|
|
15
15
|
// Stop early - likely to not include deleted row in first replication attempt.
|
|
16
|
-
await testResumingReplication(
|
|
16
|
+
await testResumingReplication(config, 2000);
|
|
17
17
|
});
|
|
18
18
|
test('resuming initial replication (2)', async () => {
|
|
19
19
|
// Stop late - likely to include deleted row in first replication attempt.
|
|
20
|
-
await testResumingReplication(
|
|
20
|
+
await testResumingReplication(config, 8000);
|
|
21
21
|
});
|
|
22
22
|
});
|
|
23
23
|
});
|
|
24
24
|
|
|
25
|
-
async function testResumingReplication(
|
|
25
|
+
async function testResumingReplication(config: TestStorageConfig, stopAfter: number) {
|
|
26
26
|
// This tests interrupting and then resuming initial replication.
|
|
27
27
|
// We interrupt replication after test_data1 has fully replicated, and
|
|
28
28
|
// test_data2 has partially replicated.
|
|
@@ -34,7 +34,9 @@ async function testResumingReplication(factory: TestStorageFactory, stopAfter: n
|
|
|
34
34
|
// have been / have not been replicated at that point is not deterministic.
|
|
35
35
|
// We do allow for some variation in the test results to account for this.
|
|
36
36
|
|
|
37
|
-
await using context = await CDCStreamTestContext.open(factory, {
|
|
37
|
+
await using context = await CDCStreamTestContext.open(config.factory, {
|
|
38
|
+
cdcStreamOptions: { snapshotBatchSize: 1000 }
|
|
39
|
+
});
|
|
38
40
|
|
|
39
41
|
await context.updateSyncRules(`bucket_definitions:
|
|
40
42
|
global:
|
|
@@ -84,7 +86,7 @@ async function testResumingReplication(factory: TestStorageFactory, stopAfter: n
|
|
|
84
86
|
}
|
|
85
87
|
|
|
86
88
|
// Bypass the usual "clear db on factory open" step.
|
|
87
|
-
await using context2 = await CDCStreamTestContext.open(factory, {
|
|
89
|
+
await using context2 = await CDCStreamTestContext.open(config.factory, {
|
|
88
90
|
doNotClear: true,
|
|
89
91
|
cdcStreamOptions: { snapshotBatchSize: 1000 }
|
|
90
92
|
});
|
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:
|
|
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
|
});
|