Skip to content

[MySQL] Automatic schema change handling #287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 59 commits into from
Jul 29, 2025

Conversation

Rentacookie
Copy link
Contributor

@Rentacookie Rentacookie commented Jun 25, 2025

Currently, when syncing from a MySQL database database schema changes are ignored. This is problematic when those changes affect tables that are in the defined sync rules. The work around for that has been to redeploy the sync rules, thereby triggering a full re-sync.

This change set adds functionality to automatically handle schema changes affecting tables in the sync rules by listening for and parsing DDL query binlog events.

The basic mechanism for this works as follows:

  1. Listen for query binlog events.
  2. Parse these events to see if they affect the schema of tables in the sync rules.
  3. Pause the binlog listener and update the stored bucket data based on the schema change if required
  4. Restart the binlog listener.

Parsing of the query SQL is accomplished by using the node-sql-parser package. This does introduce a limitation in that if the library can't parse the query, we cannot easily interpret the intended schema change either. If this is the case, some best effort pattern matching is done to check if the query is a schema change that affects one of the replicated tables and a warning is logged.

The following schema changes are detectable:

  • Create, rename, drop Table
  • Truncate Table
  • Create / Drop unique indexes and primary keys
  • Add, modify, drop, rename columns

With the exception of Create Table, all of the schema events are detected by parsing the DDL statements received in the binlog query events. For Create table, these changes are picked up when row events are received for the new table.

…kage directly

Added check for tablemap events
…tener class.

Introduced a mechanism to limit the maximum size of the binlog processing queue, thus also limiting memory usage.
This maximum processing queue size is configurable
# Conflicts:
#	modules/module-mysql/package.json
Cleaned up BinLogStream logs a bit
Simplified BinLogListener stopping mechanism
Added a few more defensive stopped checks to the binlog listener
… memory usage rather than number of events.

Introduced a maximum timeout that the binlog processing queue can be paused before auto-resuming. This is to prevent the replication connection timing out.
Made SourceTable implement SourceEntityDescriptor interface
…trics instead of ignoring them. SourceTable.

Moved MySQL table detail retrieval logic to utility function.
# Conflicts:
#	modules/module-mongodb-storage/src/storage/implementation/MongoBucketBatch.ts
#	modules/module-mongodb-storage/src/storage/implementation/MongoSyncBucketStorage.ts
#	modules/module-mongodb/src/replication/ChangeStream.ts
#	modules/module-mysql/package.json
#	modules/module-mysql/src/replication/BinLogStream.ts
#	modules/module-mysql/src/replication/zongji/BinLogListener.ts
#	modules/module-mysql/src/replication/zongji/zongji-utils.ts
#	modules/module-mysql/test/src/BinLogListener.test.ts
#	modules/module-postgres-storage/src/storage/PostgresSyncRulesStorage.ts
#	modules/module-postgres/src/replication/WalStream.ts
#	packages/service-core/src/storage/SourceTable.ts
#	pnpm-lock.yaml
Improved binlog table filtering
Added extended type definitions for node-sql-parser package
Cleaned up MySQL tests in general and added a few new test utils
- Added more detections of constraint changes
- Removed detection of create table statements since they can be detected and reacted to when row events are received for new tables
- Added multiple extra test cases
@Rentacookie Rentacookie marked this pull request as ready for review July 9, 2025 11:22
@stevensJourney stevensJourney requested a review from Copilot July 11, 2025 07:08
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds automatic handling of schema changes (DDL events) in MySQL replication and standardizes the use of a name property instead of table across sync-rules and storage modules.

  • Renamed SourceTableInterface.table to .name everywhere in sync-rules and service-core
  • Introduced BinLogListener logic to parse DDL statements and emit schema change events
  • Updated all storage and API layers (Postgres, MySQL, MongoDB) to use table.name and qualifiedName

Reviewed Changes

Copilot reviewed 41 out of 42 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
packages/sync-rules/src/…/SourceTableInterface.ts Renamed tablename in interface
packages/service-core/src/storage/SourceTable.ts Refactored SourceTable to take an options object
modules/module-mysql/src/replication/zongji/BinLogListener.ts Added DDL parsing, schema change handling, restart logic
modules/module-postgres-storage/src/storage/batch/PostgresBucketBatch.ts Updated mapping to use table.name
modules/module-mysql/test/src/BinLogStream.test.ts Fixed missing await and test descriptions
modules/module-mysql/src/utils/parser-utils.ts Introduced DDL‐matching helper
packages/service-core/src/metrics/open-telemetry/util.ts Added service.version and JSON import of package.json
Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported
Comments suppressed due to low confidence (4)

packages/service-core/src/metrics/open-telemetry/util.ts:66

  • Use the semantic key 'service.name' (per OpenTelemetry conventions) instead of 'service' for the service identifier.
        ['service']: 'PowerSync',

modules/module-mysql/test/src/BinLogStream.test.ts:16

  • [nitpick] Typo in the test suite title: it should read 'BinLogStream tests' to match the class name.
describe('BigLogStream tests', () => {

packages/service-core/src/storage/SourceTable.ts:55

  • [nitpick] The hasReplicaIdentity getter was removed; verify that no downstream code relies on it or reintroduce it if still used.
   */

modules/module-postgres-storage/src/storage/batch/PostgresBucketBatch.ts:467

  • Confirm that the table object has a name property; if it still uses table.table, update the mapping to use table.table or rename the source field accordingly.
        name: table.name,

@Rentacookie Rentacookie requested a review from rkistner July 28, 2025 13:20
rkistner
rkistner previously approved these changes Jul 29, 2025
@Rentacookie Rentacookie merged commit d4db4e2 into main Jul 29, 2025
21 checks passed
@Rentacookie Rentacookie deleted the feat/mysql-schema-change-handling branch July 29, 2025 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants