Skip to content

Commit ee0fc27

Browse files
authored
Emit schema mismatch errors (#7)
Emit mismatched schema errors for events for which the table schema can no longer be retrieved
1 parent 52b43f3 commit ee0fc27

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

.changeset/calm-camels-slide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/mysql-zongji': minor
3+
---
4+
5+
Emit error when an unrecoverable schema change was encountered

index.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ ZongJi.prototype._fetchTableInfo = function (tableMapEvent, next) {
132132
if (rows.length === 0) {
133133
this.emit(
134134
'error',
135-
new Error('Insufficient permissions to access: ' + tableMapEvent.schemaName + '.' + tableMapEvent.tableName)
135+
new Error(
136+
`Insufficient permissions to access [${tableMapEvent.schemaName}.${tableMapEvent.tableName}], or the table has been dropped.`
137+
)
136138
);
137139
// This is a fatal error, no additional binlog events will be
138140
// processed since next() will never be called
@@ -242,8 +244,21 @@ ZongJi.prototype.start = function (options = {}) {
242244
if (!tableMap || tableMap.tableName !== event.tableName || tableMap.columns.length !== event.columnCount) {
243245
this.connection.pause();
244246
this._fetchTableInfo(event, () => {
245-
// merge the column info with metadata
246-
event.updateColumnInfo();
247+
// Merge the column info with metadata if available
248+
// This relies on the schema info in the DB. Some schema changes like dropped tables and columns
249+
// mean that an unrecoverable mismatch can occur. Catch and emit these errors when they happen
250+
try {
251+
event.updateColumnInfo();
252+
} catch (error) {
253+
const schemaError = new Error(
254+
`Event received for table [${event.schemaName}.${event.tableName}] that does not match its current schema.`,
255+
{
256+
cause: error
257+
}
258+
);
259+
this.emit('error', schemaError);
260+
return;
261+
}
247262
this.emit('binlog', event);
248263
this.connection.resume();
249264
});

0 commit comments

Comments
 (0)