Skip to content

Commit 0bdd9db

Browse files
committed
Made the parentEnvironmentId migrations safe
1 parent 085c53a commit 0bdd9db

File tree

1 file changed

+19
-4
lines changed
  • internal-packages/database/prisma/migrations/20250509180155_runtime_environment_branching

1 file changed

+19
-4
lines changed

internal-packages/database/prisma/migrations/20250509180155_runtime_environment_branching/migration.sql

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
-- AlterTable
22
ALTER TABLE "RuntimeEnvironment"
3-
ADD COLUMN "archivedAt" TIMESTAMP(3),
4-
ADD COLUMN "branchName" TEXT,
5-
ADD COLUMN "git" JSONB;
3+
ADD COLUMN IF NOT EXISTS "archivedAt" TIMESTAMP(3),
4+
ADD COLUMN IF NOT EXISTS "branchName" TEXT,
5+
ADD COLUMN IF NOT EXISTS "git" JSONB;
66

77
-- Add the parentEnvironmentId column
88
DO $$
@@ -19,4 +19,19 @@ BEGIN
1919
END $$;
2020

2121
-- AddForeignKey
22-
ALTER TABLE "RuntimeEnvironment" ADD CONSTRAINT "RuntimeEnvironment_parentEnvironmentId_fkey" FOREIGN KEY ("parentEnvironmentId") REFERENCES "RuntimeEnvironment" ("id") ON DELETE CASCADE ON UPDATE CASCADE;
22+
DO $$
23+
BEGIN
24+
IF NOT EXISTS (
25+
SELECT 1
26+
FROM information_schema.table_constraints
27+
WHERE table_name = 'RuntimeEnvironment'
28+
AND constraint_name = 'RuntimeEnvironment_parentEnvironmentId_fkey'
29+
) THEN
30+
ALTER TABLE "RuntimeEnvironment"
31+
ADD CONSTRAINT "RuntimeEnvironment_parentEnvironmentId_fkey"
32+
FOREIGN KEY ("parentEnvironmentId")
33+
REFERENCES "RuntimeEnvironment" ("id")
34+
ON DELETE CASCADE
35+
ON UPDATE CASCADE;
36+
END IF;
37+
END $$;

0 commit comments

Comments
 (0)