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) => {