@powersync/service-module-mysql 0.7.4 → 0.9.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.
Files changed (63) hide show
  1. package/CHANGELOG.md +61 -0
  2. package/LICENSE +3 -3
  3. package/dev/docker/mysql/init-scripts/my.cnf +1 -3
  4. package/dist/api/MySQLRouteAPIAdapter.js +12 -4
  5. package/dist/api/MySQLRouteAPIAdapter.js.map +1 -1
  6. package/dist/common/ReplicatedGTID.js +4 -0
  7. package/dist/common/ReplicatedGTID.js.map +1 -1
  8. package/dist/common/common-index.d.ts +1 -2
  9. package/dist/common/common-index.js +1 -2
  10. package/dist/common/common-index.js.map +1 -1
  11. package/dist/common/mysql-to-sqlite.d.ts +1 -1
  12. package/dist/common/mysql-to-sqlite.js +4 -0
  13. package/dist/common/mysql-to-sqlite.js.map +1 -1
  14. package/dist/common/schema-utils.d.ts +20 -0
  15. package/dist/common/{get-replication-columns.js → schema-utils.js} +73 -30
  16. package/dist/common/schema-utils.js.map +1 -0
  17. package/dist/replication/BinLogReplicationJob.js +4 -1
  18. package/dist/replication/BinLogReplicationJob.js.map +1 -1
  19. package/dist/replication/BinLogStream.d.ts +9 -6
  20. package/dist/replication/BinLogStream.js +117 -73
  21. package/dist/replication/BinLogStream.js.map +1 -1
  22. package/dist/replication/zongji/BinLogListener.d.ts +60 -6
  23. package/dist/replication/zongji/BinLogListener.js +347 -89
  24. package/dist/replication/zongji/BinLogListener.js.map +1 -1
  25. package/dist/replication/zongji/zongji-utils.d.ts +4 -1
  26. package/dist/replication/zongji/zongji-utils.js +9 -0
  27. package/dist/replication/zongji/zongji-utils.js.map +1 -1
  28. package/dist/types/node-sql-parser-extended-types.d.ts +31 -0
  29. package/dist/types/node-sql-parser-extended-types.js +2 -0
  30. package/dist/types/node-sql-parser-extended-types.js.map +1 -0
  31. package/dist/utils/mysql-utils.d.ts +4 -2
  32. package/dist/utils/mysql-utils.js +15 -3
  33. package/dist/utils/mysql-utils.js.map +1 -1
  34. package/dist/utils/parser-utils.d.ts +16 -0
  35. package/dist/utils/parser-utils.js +58 -0
  36. package/dist/utils/parser-utils.js.map +1 -0
  37. package/package.json +12 -11
  38. package/src/api/MySQLRouteAPIAdapter.ts +15 -4
  39. package/src/common/ReplicatedGTID.ts +6 -1
  40. package/src/common/common-index.ts +1 -2
  41. package/src/common/mysql-to-sqlite.ts +7 -1
  42. package/src/common/{get-replication-columns.ts → schema-utils.ts} +96 -37
  43. package/src/replication/BinLogReplicationJob.ts +4 -1
  44. package/src/replication/BinLogStream.ts +139 -94
  45. package/src/replication/zongji/BinLogListener.ts +421 -100
  46. package/src/replication/zongji/zongji-utils.ts +16 -1
  47. package/src/types/node-sql-parser-extended-types.ts +25 -0
  48. package/src/utils/mysql-utils.ts +19 -4
  49. package/src/utils/parser-utils.ts +73 -0
  50. package/test/src/BinLogListener.test.ts +421 -77
  51. package/test/src/BinLogStream.test.ts +128 -52
  52. package/test/src/BinlogStreamUtils.ts +12 -2
  53. package/test/src/mysql-to-sqlite.test.ts +5 -5
  54. package/test/src/parser-utils.test.ts +24 -0
  55. package/test/src/schema-changes.test.ts +659 -0
  56. package/test/src/util.ts +87 -1
  57. package/tsconfig.tsbuildinfo +1 -1
  58. package/dist/common/get-replication-columns.d.ts +0 -12
  59. package/dist/common/get-replication-columns.js.map +0 -1
  60. package/dist/common/get-tables-from-pattern.d.ts +0 -7
  61. package/dist/common/get-tables-from-pattern.js +0 -28
  62. package/dist/common/get-tables-from-pattern.js.map +0 -1
  63. package/src/common/get-tables-from-pattern.ts +0 -44
package/test/src/util.ts CHANGED
@@ -1,11 +1,16 @@
1
1
  import * as types from '@module/types/types.js';
2
- import { getMySQLVersion, isVersionAtLeast } from '@module/utils/mysql-utils.js';
2
+ import { createRandomServerId, getMySQLVersion, isVersionAtLeast } from '@module/utils/mysql-utils.js';
3
3
  import * as mongo_storage from '@powersync/service-module-mongodb-storage';
4
4
  import * as postgres_storage from '@powersync/service-module-postgres-storage';
5
5
  import mysqlPromise from 'mysql2/promise';
6
6
  import { env } from './env.js';
7
7
  import { describe, TestOptions } from 'vitest';
8
8
  import { TestStorageFactory } from '@powersync/service-core';
9
+ import { MySQLConnectionManager } from '@module/replication/MySQLConnectionManager.js';
10
+ import { BinLogEventHandler, BinLogListener, Row, SchemaChange } from '@module/replication/zongji/BinLogListener.js';
11
+ import { TableMapEntry } from '@powersync/mysql-zongji';
12
+ import * as common from '@module/common/common-index.js';
13
+ import { TablePattern } from '@powersync/service-sync-rules';
9
14
 
10
15
  export const TEST_URI = env.MYSQL_TEST_URI;
11
16
 
@@ -52,3 +57,84 @@ export async function clearTestDb(connection: mysqlPromise.Connection) {
52
57
  }
53
58
  }
54
59
  }
60
+
61
+ export async function createTestDb(connectionManager: MySQLConnectionManager, dbName: string) {
62
+ await connectionManager.query(`DROP DATABASE IF EXISTS ${dbName}`);
63
+ await connectionManager.query(`CREATE DATABASE IF NOT EXISTS ${dbName}`);
64
+ }
65
+
66
+ export async function getFromGTID(connectionManager: MySQLConnectionManager) {
67
+ const connection = await connectionManager.getConnection();
68
+ const fromGTID = await common.readExecutedGtid(connection);
69
+ connection.release();
70
+
71
+ return fromGTID;
72
+ }
73
+
74
+ export interface CreateBinlogListenerParams {
75
+ connectionManager: MySQLConnectionManager;
76
+ eventHandler: BinLogEventHandler;
77
+ sourceTables: TablePattern[];
78
+ startGTID?: common.ReplicatedGTID;
79
+ }
80
+ export async function createBinlogListener(params: CreateBinlogListenerParams): Promise<BinLogListener> {
81
+ let { connectionManager, eventHandler, sourceTables, startGTID } = params;
82
+
83
+ if (!startGTID) {
84
+ startGTID = await getFromGTID(connectionManager);
85
+ }
86
+
87
+ return new BinLogListener({
88
+ connectionManager: connectionManager,
89
+ eventHandler: eventHandler,
90
+ startGTID: startGTID!,
91
+ sourceTables: sourceTables,
92
+ serverId: createRandomServerId(1)
93
+ });
94
+ }
95
+
96
+ export class TestBinLogEventHandler implements BinLogEventHandler {
97
+ rowsWritten = 0;
98
+ rowsUpdated = 0;
99
+ rowsDeleted = 0;
100
+ commitCount = 0;
101
+ schemaChanges: SchemaChange[] = [];
102
+ lastKeepAlive: string | undefined;
103
+
104
+ unpause: ((value: void | PromiseLike<void>) => void) | undefined;
105
+ private pausedPromise: Promise<void> | undefined;
106
+
107
+ pause() {
108
+ this.pausedPromise = new Promise((resolve) => {
109
+ this.unpause = resolve;
110
+ });
111
+ }
112
+
113
+ async onWrite(rows: Row[], tableMap: TableMapEntry) {
114
+ if (this.pausedPromise) {
115
+ await this.pausedPromise;
116
+ }
117
+ this.rowsWritten = this.rowsWritten + rows.length;
118
+ }
119
+
120
+ async onUpdate(afterRows: Row[], beforeRows: Row[], tableMap: TableMapEntry) {
121
+ this.rowsUpdated = this.rowsUpdated + afterRows.length;
122
+ }
123
+
124
+ async onDelete(rows: Row[], tableMap: TableMapEntry) {
125
+ this.rowsDeleted = this.rowsDeleted + rows.length;
126
+ }
127
+
128
+ async onCommit(lsn: string) {
129
+ this.commitCount++;
130
+ }
131
+
132
+ async onSchemaChange(change: SchemaChange) {
133
+ this.schemaChanges.push(change);
134
+ }
135
+ async onTransactionStart(options: { timestamp: Date }) {}
136
+ async onRotate() {}
137
+ async onKeepAlive(lsn: string) {
138
+ this.lastKeepAlive = lsn;
139
+ }
140
+ }