From 5a0f922872e9d6ca2ad3059288ab2015ded719eb Mon Sep 17 00:00:00 2001 From: Kevin Date: Tue, 26 Sep 2023 13:28:56 +0200 Subject: [PATCH] fix (client): solve migration bug with indexes (#471) 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. --- .changeset/funny-terms-knock.md | 5 ++++ clients/typescript/src/migrators/builder.ts | 3 +++ .../typescript/test/migrators/builder.test.ts | 26 +++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 .changeset/funny-terms-knock.md diff --git a/.changeset/funny-terms-knock.md b/.changeset/funny-terms-knock.md new file mode 100644 index 0000000000..73babd4d5a --- /dev/null +++ b/.changeset/funny-terms-knock.md @@ -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. diff --git a/clients/typescript/src/migrators/builder.ts b/clients/typescript/src/migrators/builder.ts index 5a342fbaad..3f25b68996 100644 --- a/clients/typescript/src/migrators/builder.ts +++ b/clients/typescript/src/migrators/builder.ts @@ -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) => { diff --git a/clients/typescript/test/migrators/builder.test.ts b/clients/typescript/test/migrators/builder.test.ts index 4e125df8f7..7fcc8b431a 100644 --- a/clients/typescript/test/migrators/builder.test.ts +++ b/clients/typescript/test/migrators/builder.test.ts @@ -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) => {