From 99198e246babeaaa9c131853c7da8f9e00734d0a Mon Sep 17 00:00:00 2001 From: darkskygit Date: Wed, 4 Jun 2025 02:21:56 +0000 Subject: [PATCH] fix: migration compatible for postgres (#12659) fix AI-162 ## Summary by CodeRabbit - **Chores** - Improved database migration scripts to prevent errors by ensuring changes are only applied if relevant tables exist. No visible changes to user features or functionality. --- .../migration.sql | 11 +++++-- .../migration.sql | 29 ++++++++++++------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/packages/backend/server/migrations/20250429113337_workspace_file_blob_id/migration.sql b/packages/backend/server/migrations/20250429113337_workspace_file_blob_id/migration.sql index fe6de86cca..a7c779f11b 100644 --- a/packages/backend/server/migrations/20250429113337_workspace_file_blob_id/migration.sql +++ b/packages/backend/server/migrations/20250429113337_workspace_file_blob_id/migration.sql @@ -1,2 +1,9 @@ --- AlterTable -ALTER TABLE "ai_workspace_files" ADD COLUMN "blob_id" VARCHAR NOT NULL DEFAULT ''; +DO $$ + BEGIN + IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'ai_workspace_files') THEN + -- AlterTable + ALTER TABLE "ai_workspace_files" + ADD COLUMN "blob_id" VARCHAR NOT NULL DEFAULT ''; + END IF; + END +$$; \ No newline at end of file diff --git a/packages/backend/server/migrations/20250521083048_fix_workspace_embedding_chunk_primary_key/migration.sql b/packages/backend/server/migrations/20250521083048_fix_workspace_embedding_chunk_primary_key/migration.sql index 99277903bc..ae1dbc361d 100644 --- a/packages/backend/server/migrations/20250521083048_fix_workspace_embedding_chunk_primary_key/migration.sql +++ b/packages/backend/server/migrations/20250521083048_fix_workspace_embedding_chunk_primary_key/migration.sql @@ -5,16 +5,25 @@ - The primary key for the `ai_workspace_file_embeddings` table will be changed. If it partially fails, the table could be left without primary key constraint. */ --- DropIndex -DROP INDEX "ai_workspace_embeddings_workspace_id_doc_id_chunk_key"; +DO $$ + BEGIN + IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'ai_workspace_embeddings') AND + EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'ai_workspace_file_embeddings') THEN + -- DropIndex + DROP INDEX "ai_workspace_embeddings_workspace_id_doc_id_chunk_key"; --- DropIndex -DROP INDEX "ai_workspace_file_embeddings_workspace_id_file_id_chunk_key"; + -- DropIndex + DROP INDEX "ai_workspace_file_embeddings_workspace_id_file_id_chunk_key"; --- AlterTable -ALTER TABLE "ai_workspace_embeddings" DROP CONSTRAINT "ai_workspace_embeddings_pkey", -ADD CONSTRAINT "ai_workspace_embeddings_pkey" PRIMARY KEY ("workspace_id", "doc_id", "chunk"); + -- AlterTable + ALTER TABLE "ai_workspace_embeddings" + DROP CONSTRAINT "ai_workspace_embeddings_pkey", + ADD CONSTRAINT "ai_workspace_embeddings_pkey" PRIMARY KEY ("workspace_id", "doc_id", "chunk"); --- AlterTable -ALTER TABLE "ai_workspace_file_embeddings" DROP CONSTRAINT "ai_workspace_file_embeddings_pkey", -ADD CONSTRAINT "ai_workspace_file_embeddings_pkey" PRIMARY KEY ("workspace_id", "file_id", "chunk"); + -- AlterTable + ALTER TABLE "ai_workspace_file_embeddings" + DROP CONSTRAINT "ai_workspace_file_embeddings_pkey", + ADD CONSTRAINT "ai_workspace_file_embeddings_pkey" PRIMARY KEY ("workspace_id", "file_id", "chunk"); + END IF; + END +$$;