From 43e91bc614475a5c0155e95190573e055709f0c2 Mon Sep 17 00:00:00 2001 From: Jannis Braun <151788261+TheZwiss@users.noreply.github.com> Date: Thu, 26 Mar 2026 20:51:49 +0100 Subject: [PATCH] fix(schema): handle pre-migration index creation gracefully on existing installs The CREATE INDEX on federated_id in createTables runs before migrations. On existing installs the column is still canonical_pair_id, causing a crash. Wrap in try-catch so the migration handles it instead. --- packages/server/src/db/index.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/server/src/db/index.ts b/packages/server/src/db/index.ts index 7bd57013..222ce26f 100644 --- a/packages/server/src/db/index.ts +++ b/packages/server/src/db/index.ts @@ -98,7 +98,7 @@ function createTables(db: Database.Database): void { deleted_at INTEGER, created_at INTEGER NOT NULL ); - CREATE UNIQUE INDEX IF NOT EXISTS idx_dm_federated ON dm_channels(federated_id) WHERE federated_id IS NOT NULL; + -- Note: idx_dm_federated index is created after this block (may fail on pre-migration tables, handled gracefully) CREATE TABLE IF NOT EXISTS dm_members ( dm_channel_id TEXT NOT NULL REFERENCES dm_channels(id) ON DELETE CASCADE, @@ -268,6 +268,14 @@ function createTables(db: Database.Database): void { PRIMARY KEY (space_id, user_id, restriction_type) ); `); + + // Index on federated_id — may fail on existing installs where the column hasn't been + // renamed yet (migration handles it). Safe to skip; migration creates the index. + try { + db.exec(`CREATE UNIQUE INDEX IF NOT EXISTS idx_dm_federated ON dm_channels(federated_id) WHERE federated_id IS NOT NULL`); + } catch { + // Column doesn't exist yet — migration will create the index + } } export function initDatabase() {