Skip to content

Commit

Permalink
fix (client): solve migration bug with indexes (#471)
Browse files Browse the repository at this point in the history
This PR solves the index bug described in #457.
The bug was caused by the client migration code which tried to build
table triggers even if the table information is missing. The code was
modified not to build any triggers if the table information is missing.
The table information may be missing e.g. when only creating an index.
  • Loading branch information
kevin-dp authored Sep 26, 2023
1 parent e997365 commit 5a0f922
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-terms-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"electric-sql": patch
---

Fix the bug where the client would crash/stop working/stop syncing if it received a migration containing a new index creation.
3 changes: 3 additions & 0 deletions clients/typescript/src/migrators/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ export function makeMigration(migration: MetaData): Migration {
.map((op) => op.stmts.map((stmt) => stmt.sql))
.flat()
const tables = migration.ops
// if the operation did not change any table
// then ignore it as we don't have to build triggers for it
.filter((op) => op.table !== undefined)
.map((op) => op.table!)
// remove duplicate tables
.filter((tbl, idx, arr) => {
Expand Down
26 changes: 26 additions & 0 deletions clients/typescript/test/migrators/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,32 @@ test('generate migration from meta data', (t) => {
)
})

test('generate index creation migration from meta data', (t) => {
const metaData = parseMetadata({
format: 'SatOpMigrate',
ops: [
encodeSatOpMigrateMsg(
SatOpMigrate.fromPartial({
version: '20230613112725_814',
stmts: [
SatOpMigrate_Stmt.create({
type: SatOpMigrate_Type.CREATE_INDEX,
sql: 'CREATE INDEX idx_stars_username ON stars(username);',
}),
],
})
),
],
protocol_version: 'Electric.Satellite',
version: '20230613112725_814',
})
const migration = makeMigration(metaData)
t.assert(migration.version === migrationMetaData.version)
t.deepEqual(migration.statements, [
'CREATE INDEX idx_stars_username ON stars(username);',
])
})

const migrationsFolder = path.join('./test/migrators/support/migrations')

test('read migration meta data', async (t) => {
Expand Down

0 comments on commit 5a0f922

Please sign in to comment.