@powersync/service-module-mysql 0.12.5 → 0.14.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 +47 -0
- package/dist/api/MySQLRouteAPIAdapter.d.ts +2 -2
- package/dist/api/MySQLRouteAPIAdapter.js +8 -13
- package/dist/api/MySQLRouteAPIAdapter.js.map +1 -1
- package/dist/replication/BinLogReplicationJob.d.ts +1 -1
- package/dist/replication/BinLogReplicationJob.js +3 -3
- package/dist/replication/BinLogReplicationJob.js.map +1 -1
- package/dist/replication/BinLogReplicator.js +1 -1
- package/dist/replication/BinLogReplicator.js.map +1 -1
- package/dist/replication/BinLogStream.d.ts +3 -3
- package/dist/replication/BinLogStream.js +57 -52
- package/dist/replication/BinLogStream.js.map +1 -1
- package/dist/utils/mysql-utils.d.ts +2 -2
- package/dist/utils/mysql-utils.js.map +1 -1
- package/package.json +10 -20
- package/src/api/MySQLRouteAPIAdapter.ts +10 -14
- package/src/replication/BinLogReplicationJob.ts +3 -3
- package/src/replication/BinLogReplicator.ts +1 -1
- package/src/replication/BinLogStream.ts +64 -56
- package/src/utils/mysql-utils.ts +3 -3
- package/test/src/BinlogStreamUtils.ts +18 -18
- package/tsconfig.json +0 -3
- package/tsconfig.tsbuildinfo +1 -1
package/package.json
CHANGED
|
@@ -1,25 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@powersync/service-module-mysql",
|
|
3
3
|
"repository": "https://github.com/powersync-ja/powersync-service",
|
|
4
|
-
"
|
|
5
|
-
"version": "0.12.5",
|
|
4
|
+
"version": "0.14.0",
|
|
6
5
|
"license": "FSL-1.1-ALv2",
|
|
7
|
-
"main": "dist/index.js",
|
|
8
6
|
"type": "module",
|
|
9
7
|
"publishConfig": {
|
|
10
8
|
"access": "public"
|
|
11
9
|
},
|
|
12
10
|
"exports": {
|
|
13
|
-
".":
|
|
14
|
-
|
|
15
|
-
"require": "./dist/index.js",
|
|
16
|
-
"default": "./dist/index.js"
|
|
17
|
-
},
|
|
18
|
-
"./types": {
|
|
19
|
-
"import": "./dist/types/types.js",
|
|
20
|
-
"require": "./dist/types/types.js",
|
|
21
|
-
"default": "./dist/types/types.js"
|
|
22
|
-
}
|
|
11
|
+
".": "./dist/index.js",
|
|
12
|
+
"./types": "./dist/types/types.js"
|
|
23
13
|
},
|
|
24
14
|
"dependencies": {
|
|
25
15
|
"@powersync/mysql-zongji": "^0.6.0",
|
|
@@ -30,18 +20,18 @@
|
|
|
30
20
|
"ts-codec": "^1.3.0",
|
|
31
21
|
"uri-js": "^4.4.1",
|
|
32
22
|
"uuid": "^14.0.0",
|
|
33
|
-
"@powersync/lib-services-framework": "0.9.
|
|
34
|
-
"@powersync/service-core": "1.
|
|
35
|
-
"@powersync/service-sync-rules": "0.
|
|
36
|
-
"@powersync/service-types": "0.
|
|
23
|
+
"@powersync/lib-services-framework": "0.9.6",
|
|
24
|
+
"@powersync/service-core": "1.23.0",
|
|
25
|
+
"@powersync/service-sync-rules": "0.38.0",
|
|
26
|
+
"@powersync/service-types": "0.16.0",
|
|
37
27
|
"@powersync/service-jsonbig": "0.17.13"
|
|
38
28
|
},
|
|
39
29
|
"devDependencies": {
|
|
40
30
|
"@types/async": "^3.2.24",
|
|
41
31
|
"@types/semver": "^7.7.1",
|
|
42
|
-
"@powersync/service-core-tests": "0.
|
|
43
|
-
"@powersync/service-module-mongodb-storage": "0.
|
|
44
|
-
"@powersync/service-module-postgres-storage": "0.
|
|
32
|
+
"@powersync/service-core-tests": "0.17.0",
|
|
33
|
+
"@powersync/service-module-mongodb-storage": "0.18.0",
|
|
34
|
+
"@powersync/service-module-postgres-storage": "0.16.0"
|
|
45
35
|
},
|
|
46
36
|
"scripts": {
|
|
47
37
|
"build": "tsc -b",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { api,
|
|
1
|
+
import { api, ParseSyncConfigOptions, ReplicationHeadCallback } from '@powersync/service-core';
|
|
2
2
|
|
|
3
3
|
import * as sync_rules from '@powersync/service-sync-rules';
|
|
4
4
|
import * as service_types from '@powersync/service-types';
|
|
@@ -29,7 +29,7 @@ export class MySQLRouteAPIAdapter implements api.RouteAPI {
|
|
|
29
29
|
return this.config;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
getParseSyncRulesOptions():
|
|
32
|
+
getParseSyncRulesOptions(): ParseSyncConfigOptions {
|
|
33
33
|
return {
|
|
34
34
|
// In MySQL Schema and Database are the same thing. There is no default database
|
|
35
35
|
defaultSchema: this.config.database
|
|
@@ -220,20 +220,16 @@ export class MySQLRouteAPIAdapter implements api.RouteAPI {
|
|
|
220
220
|
}
|
|
221
221
|
|
|
222
222
|
const idColumns = idColumnsResult?.columns ?? [];
|
|
223
|
-
const
|
|
224
|
-
id: '', // not used
|
|
223
|
+
const ref: sync_rules.SourceTableRef = {
|
|
225
224
|
connectionTag: this.config.tag,
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
});
|
|
232
|
-
const syncData = syncRules.tableSyncsData(sourceTable);
|
|
233
|
-
const syncParameters = syncRules.tableSyncsParameters(sourceTable);
|
|
225
|
+
schema,
|
|
226
|
+
name: tableName
|
|
227
|
+
};
|
|
228
|
+
const syncData = syncRules.tableSyncsData(ref);
|
|
229
|
+
const syncParameters = syncRules.tableSyncsParameters(ref);
|
|
234
230
|
|
|
235
231
|
if (idColumns.length == 0 && idColumnsError == null) {
|
|
236
|
-
let message = `No replication id found for ${
|
|
232
|
+
let message = `No replication id found for ${mysql_utils.qualifiedMySQLTable(ref)}. Replica identity: ${idColumnsResult?.identity}.`;
|
|
237
233
|
if (idColumnsResult?.identity == 'default') {
|
|
238
234
|
message += ' Configure a primary key on the table.';
|
|
239
235
|
}
|
|
@@ -243,7 +239,7 @@ export class MySQLRouteAPIAdapter implements api.RouteAPI {
|
|
|
243
239
|
let selectError: service_types.ReplicationError | null = null;
|
|
244
240
|
try {
|
|
245
241
|
await this.retriedQuery({
|
|
246
|
-
query: `SELECT * FROM ${
|
|
242
|
+
query: `SELECT * FROM ${mysql_utils.qualifiedMySQLTable(ref)} LIMIT 1`
|
|
247
243
|
});
|
|
248
244
|
} catch (e) {
|
|
249
245
|
selectError = { level: 'fatal', message: e.message };
|
|
@@ -17,8 +17,8 @@ export class BinLogReplicationJob extends replication.AbstractReplicationJob {
|
|
|
17
17
|
this.connectionFactory = options.connectionFactory;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
get
|
|
21
|
-
return this.options.storage.
|
|
20
|
+
get replicationStreamName() {
|
|
21
|
+
return this.options.storage.replicationStreamName;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
async keepAlive() {
|
|
@@ -37,7 +37,7 @@ export class BinLogReplicationJob extends replication.AbstractReplicationJob {
|
|
|
37
37
|
// Report the error if relevant, before retrying
|
|
38
38
|
container.reporter.captureException(e, {
|
|
39
39
|
metadata: {
|
|
40
|
-
replication_slot: this.
|
|
40
|
+
replication_slot: this.replicationStreamName
|
|
41
41
|
}
|
|
42
42
|
});
|
|
43
43
|
// This sets the retry delay
|
|
@@ -17,7 +17,7 @@ export class BinLogReplicator extends replication.AbstractReplicator<BinLogRepli
|
|
|
17
17
|
|
|
18
18
|
createJob(options: replication.CreateJobOptions): BinLogReplicationJob {
|
|
19
19
|
return new BinLogReplicationJob({
|
|
20
|
-
id: this.createJobId(options.storage.
|
|
20
|
+
id: this.createJobId(options.storage.replicationStreamId),
|
|
21
21
|
storage: options.storage,
|
|
22
22
|
metrics: this.metrics,
|
|
23
23
|
lock: options.lock,
|
|
@@ -62,7 +62,7 @@ function createTableId(schema: string, tableName: string): string {
|
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
export class BinLogStream {
|
|
65
|
-
private readonly syncRules: sync_rules.
|
|
65
|
+
private readonly syncRules: sync_rules.HydratedSyncConfig;
|
|
66
66
|
private readonly groupId: number;
|
|
67
67
|
|
|
68
68
|
private readonly storage: storage.SyncRulesBucketStorage;
|
|
@@ -73,7 +73,7 @@ export class BinLogStream {
|
|
|
73
73
|
|
|
74
74
|
private readonly logger: Logger;
|
|
75
75
|
|
|
76
|
-
private tableCache = new Map<string | number, storage.SourceTable>();
|
|
76
|
+
private tableCache = new Map<string | number, storage.SourceTable[]>();
|
|
77
77
|
|
|
78
78
|
private replicationLag = new ReplicationLagTracker();
|
|
79
79
|
|
|
@@ -82,7 +82,7 @@ export class BinLogStream {
|
|
|
82
82
|
this.storage = options.storage;
|
|
83
83
|
this.connections = options.connections;
|
|
84
84
|
this.syncRules = options.storage.getParsedSyncRules({ defaultSchema: this.defaultSchema });
|
|
85
|
-
this.groupId = options.storage.
|
|
85
|
+
this.groupId = options.storage.replicationStreamId;
|
|
86
86
|
this.abortSignal = options.abortSignal;
|
|
87
87
|
}
|
|
88
88
|
|
|
@@ -122,16 +122,17 @@ export class BinLogStream {
|
|
|
122
122
|
return this.connections.databaseName;
|
|
123
123
|
}
|
|
124
124
|
|
|
125
|
-
async handleRelation(
|
|
126
|
-
|
|
127
|
-
|
|
125
|
+
async handleRelation(
|
|
126
|
+
batch: storage.BucketStorageBatch,
|
|
127
|
+
source: storage.SourceEntityDescriptor,
|
|
128
|
+
snapshot: boolean
|
|
129
|
+
): Promise<storage.SourceTable[]> {
|
|
130
|
+
const result = await batch.resolveTables({
|
|
128
131
|
connection_id: this.connectionId,
|
|
129
|
-
|
|
130
|
-
entity_descriptor: entity,
|
|
131
|
-
sync_rules: this.syncRules
|
|
132
|
+
source
|
|
132
133
|
});
|
|
133
|
-
// Since we create the objectId ourselves, this is always defined
|
|
134
|
-
this.tableCache.set(
|
|
134
|
+
// Since we create the objectId ourselves, this is always defined.
|
|
135
|
+
this.tableCache.set(source.objectId!, result.tables);
|
|
135
136
|
|
|
136
137
|
// Drop conflicting tables. In the MySQL case with ObjectIds created from the table name, renames cannot be detected by the storage.
|
|
137
138
|
await batch.drop(result.dropTables);
|
|
@@ -140,11 +141,9 @@ export class BinLogStream {
|
|
|
140
141
|
// 1. Snapshot is requested (false for initial snapshot, since that process handles it elsewhere)
|
|
141
142
|
// 2. Snapshot is not done yet, AND:
|
|
142
143
|
// 3. The table is used in sync config.
|
|
143
|
-
const
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
// Truncate this table in case a previous snapshot was interrupted.
|
|
147
|
-
await batch.truncate([result.table]);
|
|
144
|
+
const snapshotCandidates = result.tables.filter((table) => snapshot && !table.snapshotComplete && table.syncAny);
|
|
145
|
+
if (snapshotCandidates.length > 0) {
|
|
146
|
+
await batch.truncate(snapshotCandidates);
|
|
148
147
|
|
|
149
148
|
let gtid: common.ReplicatedGTID;
|
|
150
149
|
// Start the snapshot inside a transaction.
|
|
@@ -157,7 +156,9 @@ export class BinLogStream {
|
|
|
157
156
|
await promiseConnection.query('START TRANSACTION');
|
|
158
157
|
try {
|
|
159
158
|
gtid = await common.readExecutedGtid(promiseConnection);
|
|
160
|
-
|
|
159
|
+
for (const table of snapshotCandidates) {
|
|
160
|
+
await this.snapshotTable(connection as mysql.Connection, batch, table);
|
|
161
|
+
}
|
|
161
162
|
await promiseConnection.query('COMMIT');
|
|
162
163
|
} catch (e) {
|
|
163
164
|
await this.tryRollback(promiseConnection);
|
|
@@ -166,11 +167,14 @@ export class BinLogStream {
|
|
|
166
167
|
} finally {
|
|
167
168
|
connection.release();
|
|
168
169
|
}
|
|
169
|
-
const
|
|
170
|
-
|
|
170
|
+
const doneTables = await batch.markTableSnapshotDone(snapshotCandidates, gtid.comparable);
|
|
171
|
+
const doneTablesById = new Map(doneTables.map((table) => [table.id, table]));
|
|
172
|
+
const tables = result.tables.map((table) => doneTablesById.get(table.id) ?? table);
|
|
173
|
+
this.tableCache.set(source.objectId!, tables);
|
|
174
|
+
return tables;
|
|
171
175
|
}
|
|
172
176
|
|
|
173
|
-
return result.
|
|
177
|
+
return result.tables;
|
|
174
178
|
}
|
|
175
179
|
|
|
176
180
|
async getQualifiedTableNames(
|
|
@@ -189,18 +193,19 @@ export class BinLogStream {
|
|
|
189
193
|
for (const matchedTable of matchedTables) {
|
|
190
194
|
const replicaIdColumns = await this.getReplicaIdColumns(matchedTable, tablePattern.schema);
|
|
191
195
|
|
|
192
|
-
const
|
|
196
|
+
const resolvedTables = await this.handleRelation(
|
|
193
197
|
batch,
|
|
194
198
|
{
|
|
195
199
|
name: matchedTable,
|
|
196
200
|
schema: tablePattern.schema,
|
|
201
|
+
connectionTag: this.connectionTag,
|
|
197
202
|
objectId: createTableId(tablePattern.schema, matchedTable),
|
|
198
203
|
replicaIdColumns: replicaIdColumns
|
|
199
204
|
},
|
|
200
205
|
false
|
|
201
206
|
);
|
|
202
207
|
|
|
203
|
-
tables.push(
|
|
208
|
+
tables.push(...resolvedTables);
|
|
204
209
|
}
|
|
205
210
|
return tables;
|
|
206
211
|
}
|
|
@@ -210,8 +215,8 @@ export class BinLogStream {
|
|
|
210
215
|
*/
|
|
211
216
|
protected async checkInitialReplicated(): Promise<boolean> {
|
|
212
217
|
const status = await this.storage.getStatus();
|
|
213
|
-
const lastKnowGTID = status.
|
|
214
|
-
if (status.
|
|
218
|
+
const lastKnowGTID = status.resumeLsn ? common.ReplicatedGTID.fromSerialized(status.resumeLsn) : null;
|
|
219
|
+
if (status.snapshotDone) {
|
|
215
220
|
this.logger.info(`Initial replication already done.`);
|
|
216
221
|
|
|
217
222
|
if (lastKnowGTID) {
|
|
@@ -276,7 +281,7 @@ export class BinLogStream {
|
|
|
276
281
|
}
|
|
277
282
|
}
|
|
278
283
|
const snapshotDoneGtid = await common.readExecutedGtid(promiseConnection);
|
|
279
|
-
await batch.
|
|
284
|
+
await batch.markSnapshotDone(snapshotDoneGtid.comparable);
|
|
280
285
|
await batch.commit(headGTID.comparable);
|
|
281
286
|
}
|
|
282
287
|
);
|
|
@@ -305,11 +310,11 @@ export class BinLogStream {
|
|
|
305
310
|
batch: storage.BucketStorageBatch,
|
|
306
311
|
table: storage.SourceTable
|
|
307
312
|
) {
|
|
308
|
-
this.logger.info(`Replicating ${qualifiedMySQLTable(table)}`);
|
|
313
|
+
this.logger.info(`Replicating ${qualifiedMySQLTable(table.ref)}`);
|
|
309
314
|
// TODO count rows and log progress at certain batch sizes
|
|
310
315
|
|
|
311
316
|
// MAX_EXECUTION_TIME(0) hint disables execution timeout for this query
|
|
312
|
-
const query = connection.query(`SELECT /*+ MAX_EXECUTION_TIME(0) */ * FROM ${qualifiedMySQLTable(table)}`);
|
|
317
|
+
const query = connection.query(`SELECT /*+ MAX_EXECUTION_TIME(0) */ * FROM ${qualifiedMySQLTable(table.ref)}`);
|
|
313
318
|
const stream = query.stream();
|
|
314
319
|
|
|
315
320
|
let columns: Map<string, ColumnDescriptor> | undefined = undefined;
|
|
@@ -390,26 +395,26 @@ export class BinLogStream {
|
|
|
390
395
|
}
|
|
391
396
|
}
|
|
392
397
|
|
|
393
|
-
private
|
|
394
|
-
const
|
|
395
|
-
if (
|
|
398
|
+
private getTables(tableId: string): storage.SourceTable[] {
|
|
399
|
+
const tables = this.tableCache.get(tableId);
|
|
400
|
+
if (tables == null) {
|
|
396
401
|
// We should always receive a replication message before the relation is used.
|
|
397
402
|
// If we can't find it, it's a bug.
|
|
398
403
|
throw new ReplicationAssertionError(`Missing relation cache for ${tableId}`);
|
|
399
404
|
}
|
|
400
|
-
return
|
|
405
|
+
return tables;
|
|
401
406
|
}
|
|
402
407
|
|
|
403
408
|
async streamChanges() {
|
|
404
|
-
const serverId = createRandomServerId(this.storage.
|
|
409
|
+
const serverId = createRandomServerId(this.storage.replicationStreamId);
|
|
405
410
|
|
|
406
411
|
const connection = await this.connections.getConnection();
|
|
407
|
-
const {
|
|
408
|
-
if (
|
|
409
|
-
this.logger.info(`Existing
|
|
412
|
+
const { resumeLsn: resume_lsn } = await this.storage.getStatus();
|
|
413
|
+
if (resume_lsn) {
|
|
414
|
+
this.logger.info(`Existing resume LSN found: ${resume_lsn}`);
|
|
410
415
|
}
|
|
411
|
-
const fromGTID =
|
|
412
|
-
? common.ReplicatedGTID.fromSerialized(
|
|
416
|
+
const fromGTID = resume_lsn
|
|
417
|
+
? common.ReplicatedGTID.fromSerialized(resume_lsn)
|
|
413
418
|
: await common.readExecutedGtid(connection);
|
|
414
419
|
connection.release();
|
|
415
420
|
|
|
@@ -498,10 +503,10 @@ export class BinLogStream {
|
|
|
498
503
|
if (change.type === SchemaChangeType.RENAME_TABLE) {
|
|
499
504
|
const fromTableId = createTableId(change.schema, change.table);
|
|
500
505
|
|
|
501
|
-
const
|
|
506
|
+
const fromTables = this.tableCache.get(fromTableId);
|
|
502
507
|
// Old table needs to be cleaned up
|
|
503
|
-
if (
|
|
504
|
-
await batch.drop(
|
|
508
|
+
if (fromTables) {
|
|
509
|
+
await batch.drop(fromTables);
|
|
505
510
|
this.tableCache.delete(fromTableId);
|
|
506
511
|
}
|
|
507
512
|
// The new table matched a table in the sync config
|
|
@@ -511,7 +516,7 @@ export class BinLogStream {
|
|
|
511
516
|
} else {
|
|
512
517
|
const tableId = createTableId(change.schema, change.table);
|
|
513
518
|
|
|
514
|
-
const
|
|
519
|
+
const tables = this.getTables(tableId);
|
|
515
520
|
|
|
516
521
|
switch (change.type) {
|
|
517
522
|
case SchemaChangeType.ALTER_TABLE_COLUMN:
|
|
@@ -520,10 +525,10 @@ export class BinLogStream {
|
|
|
520
525
|
await this.handleCreateOrUpdateTable(batch, change.table, change.schema);
|
|
521
526
|
break;
|
|
522
527
|
case SchemaChangeType.TRUNCATE_TABLE:
|
|
523
|
-
await batch.truncate(
|
|
528
|
+
await batch.truncate(tables);
|
|
524
529
|
break;
|
|
525
530
|
case SchemaChangeType.DROP_TABLE:
|
|
526
|
-
await batch.drop(
|
|
531
|
+
await batch.drop(tables);
|
|
527
532
|
this.tableCache.delete(tableId);
|
|
528
533
|
break;
|
|
529
534
|
default:
|
|
@@ -549,13 +554,14 @@ export class BinLogStream {
|
|
|
549
554
|
batch: storage.BucketStorageBatch,
|
|
550
555
|
tableName: string,
|
|
551
556
|
schema: string
|
|
552
|
-
): Promise<SourceTable> {
|
|
557
|
+
): Promise<SourceTable[]> {
|
|
553
558
|
const replicaIdColumns = await this.getReplicaIdColumns(tableName, schema);
|
|
554
559
|
return await this.handleRelation(
|
|
555
560
|
batch,
|
|
556
561
|
{
|
|
557
562
|
name: tableName,
|
|
558
563
|
schema: schema,
|
|
564
|
+
connectionTag: this.connectionTag,
|
|
559
565
|
objectId: createTableId(schema, tableName),
|
|
560
566
|
replicaIdColumns: replicaIdColumns
|
|
561
567
|
},
|
|
@@ -575,23 +581,25 @@ export class BinLogStream {
|
|
|
575
581
|
const columns = common.toColumnDescriptors(msg.tableEntry);
|
|
576
582
|
const tableId = createTableId(msg.tableEntry.parentSchema, msg.tableEntry.tableName);
|
|
577
583
|
|
|
578
|
-
let
|
|
579
|
-
if (
|
|
584
|
+
let tables = this.tableCache.get(tableId);
|
|
585
|
+
if (tables == null) {
|
|
580
586
|
// This is an insert for a new table that matches a table in the sync config
|
|
581
587
|
// We need to create the table in the storage and cache it.
|
|
582
|
-
|
|
588
|
+
tables = await this.handleCreateOrUpdateTable(batch, msg.tableEntry.tableName, msg.tableEntry.parentSchema);
|
|
583
589
|
}
|
|
584
590
|
|
|
585
591
|
for (const [index, row] of msg.rows.entries()) {
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
592
|
+
for (const table of tables.filter((table) => table.syncAny)) {
|
|
593
|
+
await this.writeChange(batch, {
|
|
594
|
+
type: msg.type,
|
|
595
|
+
database: msg.tableEntry.parentSchema,
|
|
596
|
+
sourceTable: table,
|
|
597
|
+
table: msg.tableEntry.tableName,
|
|
598
|
+
columns: columns,
|
|
599
|
+
row: row,
|
|
600
|
+
previous_row: msg.rows_before?.[index]
|
|
601
|
+
});
|
|
602
|
+
}
|
|
595
603
|
}
|
|
596
604
|
return null;
|
|
597
605
|
}
|
package/src/utils/mysql-utils.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { logger } from '@powersync/lib-services-framework';
|
|
2
|
-
import {
|
|
2
|
+
import { SourceTableRef } from '@powersync/service-sync-rules';
|
|
3
3
|
import mysql from 'mysql2';
|
|
4
4
|
import mysqlPromise from 'mysql2/promise';
|
|
5
5
|
import { coerce, gte, satisfies } from 'semver';
|
|
@@ -101,10 +101,10 @@ export function satisfiesVersion(version: string, targetVersion: string): boolea
|
|
|
101
101
|
return satisfies(coercedVersion!, targetVersion!, { loose: true });
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
-
export function qualifiedMySQLTable(table:
|
|
104
|
+
export function qualifiedMySQLTable(table: SourceTableRef): string;
|
|
105
105
|
export function qualifiedMySQLTable(table: string, schema: string): string;
|
|
106
106
|
|
|
107
|
-
export function qualifiedMySQLTable(table:
|
|
107
|
+
export function qualifiedMySQLTable(table: SourceTableRef | string, schema?: string): string {
|
|
108
108
|
if (typeof table === 'object') {
|
|
109
109
|
return `\`${table.schema.replaceAll('`', '``')}\`.\`${table.name.replaceAll('`', '``')}\``;
|
|
110
110
|
} else if (schema) {
|
|
@@ -33,7 +33,7 @@ export class BinlogStreamTestContext {
|
|
|
33
33
|
private streamPromise?: Promise<void>;
|
|
34
34
|
public storage?: SyncRulesBucketStorage;
|
|
35
35
|
private replicationDone = false;
|
|
36
|
-
private syncRulesContent?: storage.
|
|
36
|
+
private syncRulesContent?: storage.PersistedSyncConfigContent;
|
|
37
37
|
|
|
38
38
|
static async open(factory: storage.TestStorageFactory, options?: { doNotClear?: boolean }) {
|
|
39
39
|
const f = await factory({ doNotClear: options?.doNotClear });
|
|
@@ -71,38 +71,38 @@ export class BinlogStreamTestContext {
|
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
async updateSyncRules(content: string): Promise<SyncRulesBucketStorage> {
|
|
74
|
-
const
|
|
74
|
+
const replicationStream = await this.factory.updateSyncRules(
|
|
75
75
|
updateSyncRulesFromYaml(content, { validate: true, storageVersion: LEGACY_STORAGE_VERSION })
|
|
76
76
|
);
|
|
77
|
-
this.syncRulesContent =
|
|
78
|
-
this.storage = this.factory.getInstance(
|
|
77
|
+
this.syncRulesContent = replicationStream.syncConfigContent[0];
|
|
78
|
+
this.storage = this.factory.getInstance(replicationStream);
|
|
79
79
|
return this.storage!;
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
async loadNextSyncRules() {
|
|
83
|
-
const
|
|
84
|
-
if (
|
|
83
|
+
const syncConfig = await this.factory.getDeployingSyncConfig();
|
|
84
|
+
if (syncConfig == null) {
|
|
85
85
|
throw new Error(`Next replication stream not available`);
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
this.syncRulesContent =
|
|
89
|
-
this.storage =
|
|
88
|
+
this.syncRulesContent = syncConfig.content;
|
|
89
|
+
this.storage = syncConfig.storage;
|
|
90
90
|
return this.storage!;
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
async loadActiveSyncRules() {
|
|
94
|
-
const
|
|
95
|
-
if (
|
|
94
|
+
const syncConfig = await this.factory.getActiveSyncConfig();
|
|
95
|
+
if (syncConfig == null) {
|
|
96
96
|
throw new Error(`Active replication stream not available`);
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
-
this.syncRulesContent =
|
|
100
|
-
this.storage =
|
|
99
|
+
this.syncRulesContent = syncConfig.content;
|
|
100
|
+
this.storage = syncConfig.storage;
|
|
101
101
|
this.replicationDone = true;
|
|
102
102
|
return this.storage!;
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
-
private
|
|
105
|
+
private getSyncConfigContent(): storage.PersistedSyncConfigContent {
|
|
106
106
|
if (this.syncRulesContent == null) {
|
|
107
107
|
throw new Error('Sync config not configured - call updateSyncRules() first');
|
|
108
108
|
}
|
|
@@ -165,8 +165,8 @@ export class BinlogStreamTestContext {
|
|
|
165
165
|
|
|
166
166
|
async getBucketsDataBatch(buckets: Record<string, InternalOpId>, options?: { timeout?: number }) {
|
|
167
167
|
const checkpoint = await this.getCheckpoint(options);
|
|
168
|
-
const
|
|
169
|
-
const map = Object.entries(buckets).map(([bucket, start]) => bucketRequest(
|
|
168
|
+
const syncConfigContent = this.getSyncConfigContent();
|
|
169
|
+
const map = Object.entries(buckets).map(([bucket, start]) => bucketRequest(syncConfigContent, bucket, start));
|
|
170
170
|
return test_utils.fromAsync(this.storage!.getBucketDataBatch(checkpoint, map));
|
|
171
171
|
}
|
|
172
172
|
|
|
@@ -179,9 +179,9 @@ export class BinlogStreamTestContext {
|
|
|
179
179
|
if (typeof start == 'string') {
|
|
180
180
|
start = BigInt(start);
|
|
181
181
|
}
|
|
182
|
-
const
|
|
182
|
+
const syncConfigContent = this.getSyncConfigContent();
|
|
183
183
|
const checkpoint = await this.getCheckpoint(options);
|
|
184
|
-
const map = [bucketRequest(
|
|
184
|
+
const map = [bucketRequest(syncConfigContent, bucket, start)];
|
|
185
185
|
const batch = this.storage!.getBucketDataBatch(checkpoint, map);
|
|
186
186
|
const batches = await test_utils.fromAsync(batch);
|
|
187
187
|
return batches[0]?.chunkData.data ?? [];
|
|
@@ -203,7 +203,7 @@ export async function getClientCheckpoint(
|
|
|
203
203
|
|
|
204
204
|
logger.info('Expected Checkpoint: ' + gtid.comparable);
|
|
205
205
|
while (Date.now() - start < timeout) {
|
|
206
|
-
const storage = await storageFactory.
|
|
206
|
+
const storage = (await storageFactory.getActiveSyncConfig())?.storage;
|
|
207
207
|
const cp = await storage?.getCheckpoint();
|
|
208
208
|
if (cp == null) {
|
|
209
209
|
throw new Error('No replication stream available');
|
package/tsconfig.json
CHANGED