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 +$$;