@powersync/service-module-mysql 0.12.5 → 0.13.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.
@@ -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.HydratedSyncRules;
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
 
@@ -122,16 +122,17 @@ export class BinLogStream {
122
122
  return this.connections.databaseName;
123
123
  }
124
124
 
125
- async handleRelation(batch: storage.BucketStorageBatch, entity: storage.SourceEntityDescriptor, snapshot: boolean) {
126
- const result = await this.storage.resolveTable({
127
- group_id: this.groupId,
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
- connection_tag: this.connectionTag,
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(entity.objectId!, result.table);
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 shouldSnapshot = snapshot && !result.table.snapshotComplete && result.table.syncAny;
144
-
145
- if (shouldSnapshot) {
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
- await this.snapshotTable(connection as mysql.Connection, batch, result.table);
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 [table] = await batch.markTableSnapshotDone([result.table], gtid.comparable);
170
- return table;
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.table;
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 table = await this.handleRelation(
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(table);
208
+ tables.push(...resolvedTables);
204
209
  }
205
210
  return tables;
206
211
  }
@@ -276,7 +281,7 @@ export class BinLogStream {
276
281
  }
277
282
  }
278
283
  const snapshotDoneGtid = await common.readExecutedGtid(promiseConnection);
279
- await batch.markAllSnapshotDone(snapshotDoneGtid.comparable);
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,14 +395,14 @@ export class BinLogStream {
390
395
  }
391
396
  }
392
397
 
393
- private getTable(tableId: string): storage.SourceTable {
394
- const table = this.tableCache.get(tableId);
395
- if (table == null) {
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 table;
405
+ return tables;
401
406
  }
402
407
 
403
408
  async streamChanges() {
@@ -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 fromTable = this.tableCache.get(fromTableId);
506
+ const fromTables = this.tableCache.get(fromTableId);
502
507
  // Old table needs to be cleaned up
503
- if (fromTable) {
504
- await batch.drop([fromTable]);
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 table = this.getTable(tableId);
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([table]);
528
+ await batch.truncate(tables);
524
529
  break;
525
530
  case SchemaChangeType.DROP_TABLE:
526
- await batch.drop([table]);
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 table = this.tableCache.get(tableId);
579
- if (table == null) {
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
- table = await this.handleCreateOrUpdateTable(batch, msg.tableEntry.tableName, msg.tableEntry.parentSchema);
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
- await this.writeChange(batch, {
587
- type: msg.type,
588
- database: msg.tableEntry.parentSchema,
589
- sourceTable: table!,
590
- table: msg.tableEntry.tableName,
591
- columns: columns,
592
- row: row,
593
- previous_row: msg.rows_before?.[index]
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
  }
@@ -1,5 +1,5 @@
1
1
  import { logger } from '@powersync/lib-services-framework';
2
- import { SourceEntityDescriptor } from '@powersync/service-core';
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: SourceEntityDescriptor): string;
104
+ export function qualifiedMySQLTable(table: SourceTableRef): string;
105
105
  export function qualifiedMySQLTable(table: string, schema: string): string;
106
106
 
107
- export function qualifiedMySQLTable(table: SourceEntityDescriptor | string, schema?: string): string {
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) {