|
| 1 | +## Creating and applying migrations |
| 2 | + |
| 3 | +We use prisma migrations to manage the database schema. Please follow the following steps when editing the `internal-packages/database/prisma/schema.prisma` file: |
| 4 | + |
| 5 | +Edit the `schema.prisma` file to add or modify the schema. |
| 6 | + |
| 7 | +Create a new migration file but don't apply it yet: |
| 8 | + |
| 9 | +```bash |
| 10 | +cd internal-packages/database |
| 11 | +pnpm run db:migrate:dev:create --name "add_new_column_to_table" |
| 12 | +``` |
| 13 | + |
| 14 | +The migration file will be created in the `prisma/migrations` directory, but it will have a bunch of edits to the schema that are not needed and will need to be removed before we can apply the migration. Here's an example of what the migration file might look like: |
| 15 | + |
| 16 | +```sql |
| 17 | +-- AlterEnum |
| 18 | +ALTER TYPE "public"."TaskRunExecutionStatus" ADD VALUE 'DELAYED'; |
| 19 | + |
| 20 | +-- AlterTable |
| 21 | +ALTER TABLE "public"."TaskRun" ADD COLUMN "debounce" JSONB; |
| 22 | + |
| 23 | +-- AlterTable |
| 24 | +ALTER TABLE "public"."_BackgroundWorkerToBackgroundWorkerFile" ADD CONSTRAINT "_BackgroundWorkerToBackgroundWorkerFile_AB_pkey" PRIMARY KEY ("A", "B"); |
| 25 | + |
| 26 | +-- DropIndex |
| 27 | +DROP INDEX "public"."_BackgroundWorkerToBackgroundWorkerFile_AB_unique"; |
| 28 | + |
| 29 | +-- AlterTable |
| 30 | +ALTER TABLE "public"."_BackgroundWorkerToTaskQueue" ADD CONSTRAINT "_BackgroundWorkerToTaskQueue_AB_pkey" PRIMARY KEY ("A", "B"); |
| 31 | + |
| 32 | +-- DropIndex |
| 33 | +DROP INDEX "public"."_BackgroundWorkerToTaskQueue_AB_unique"; |
| 34 | + |
| 35 | +-- AlterTable |
| 36 | +ALTER TABLE "public"."_TaskRunToTaskRunTag" ADD CONSTRAINT "_TaskRunToTaskRunTag_AB_pkey" PRIMARY KEY ("A", "B"); |
| 37 | + |
| 38 | +-- DropIndex |
| 39 | +DROP INDEX "public"."_TaskRunToTaskRunTag_AB_unique"; |
| 40 | + |
| 41 | +-- AlterTable |
| 42 | +ALTER TABLE "public"."_WaitpointRunConnections" ADD CONSTRAINT "_WaitpointRunConnections_AB_pkey" PRIMARY KEY ("A", "B"); |
| 43 | + |
| 44 | +-- DropIndex |
| 45 | +DROP INDEX "public"."_WaitpointRunConnections_AB_unique"; |
| 46 | + |
| 47 | +-- AlterTable |
| 48 | +ALTER TABLE "public"."_completedWaitpoints" ADD CONSTRAINT "_completedWaitpoints_AB_pkey" PRIMARY KEY ("A", "B"); |
| 49 | + |
| 50 | +-- DropIndex |
| 51 | +DROP INDEX "public"."_completedWaitpoints_AB_unique"; |
| 52 | + |
| 53 | +-- CreateIndex |
| 54 | +CREATE INDEX "SecretStore_key_idx" ON "public"."SecretStore"("key" text_pattern_ops); |
| 55 | + |
| 56 | +-- CreateIndex |
| 57 | +CREATE INDEX "TaskRun_runtimeEnvironmentId_id_idx" ON "public"."TaskRun"("runtimeEnvironmentId", "id" DESC); |
| 58 | + |
| 59 | +-- CreateIndex |
| 60 | +CREATE INDEX "TaskRun_runtimeEnvironmentId_createdAt_idx" ON "public"."TaskRun"("runtimeEnvironmentId", "createdAt" DESC); |
| 61 | +``` |
| 62 | + |
| 63 | +All the following lines should be removed: |
| 64 | + |
| 65 | +```sql |
| 66 | +-- AlterTable |
| 67 | +ALTER TABLE "public"."_BackgroundWorkerToBackgroundWorkerFile" ADD CONSTRAINT "_BackgroundWorkerToBackgroundWorkerFile_AB_pkey" PRIMARY KEY ("A", "B"); |
| 68 | + |
| 69 | +-- DropIndex |
| 70 | +DROP INDEX "public"."_BackgroundWorkerToBackgroundWorkerFile_AB_unique"; |
| 71 | + |
| 72 | +-- AlterTable |
| 73 | +ALTER TABLE "public"."_BackgroundWorkerToTaskQueue" ADD CONSTRAINT "_BackgroundWorkerToTaskQueue_AB_pkey" PRIMARY KEY ("A", "B"); |
| 74 | + |
| 75 | +-- DropIndex |
| 76 | +DROP INDEX "public"."_BackgroundWorkerToTaskQueue_AB_unique"; |
| 77 | + |
| 78 | +-- AlterTable |
| 79 | +ALTER TABLE "public"."_TaskRunToTaskRunTag" ADD CONSTRAINT "_TaskRunToTaskRunTag_AB_pkey" PRIMARY KEY ("A", "B"); |
| 80 | + |
| 81 | +-- DropIndex |
| 82 | +DROP INDEX "public"."_TaskRunToTaskRunTag_AB_unique"; |
| 83 | + |
| 84 | +-- AlterTable |
| 85 | +ALTER TABLE "public"."_WaitpointRunConnections" ADD CONSTRAINT "_WaitpointRunConnections_AB_pkey" PRIMARY KEY ("A", "B"); |
| 86 | + |
| 87 | +-- DropIndex |
| 88 | +DROP INDEX "public"."_WaitpointRunConnections_AB_unique"; |
| 89 | + |
| 90 | +-- AlterTable |
| 91 | +ALTER TABLE "public"."_completedWaitpoints" ADD CONSTRAINT "_completedWaitpoints_AB_pkey" PRIMARY KEY ("A", "B"); |
| 92 | + |
| 93 | +-- DropIndex |
| 94 | +DROP INDEX "public"."_completedWaitpoints_AB_unique"; |
| 95 | + |
| 96 | +-- CreateIndex |
| 97 | +CREATE INDEX "SecretStore_key_idx" ON "public"."SecretStore"("key" text_pattern_ops); |
| 98 | + |
| 99 | +-- CreateIndex |
| 100 | +CREATE INDEX "TaskRun_runtimeEnvironmentId_id_idx" ON "public"."TaskRun"("runtimeEnvironmentId", "id" DESC); |
| 101 | + |
| 102 | +-- CreateIndex |
| 103 | +CREATE INDEX "TaskRun_runtimeEnvironmentId_createdAt_idx" ON "public"."TaskRun"("runtimeEnvironmentId", "createdAt" DESC); |
| 104 | +``` |
| 105 | + |
| 106 | +Leaving only this: |
| 107 | + |
| 108 | +```sql |
| 109 | +-- AlterEnum |
| 110 | +ALTER TYPE "public"."TaskRunExecutionStatus" ADD VALUE 'DELAYED'; |
| 111 | + |
| 112 | +-- AlterTable |
| 113 | +ALTER TABLE "public"."TaskRun" ADD COLUMN "debounce" JSONB; |
| 114 | +``` |
| 115 | + |
| 116 | +After editing the migration file, apply the migration: |
| 117 | + |
| 118 | +```bash |
| 119 | +cd internal-packages/database |
| 120 | +pnpm run db:migrate:deploy && pnpm run generate |
| 121 | +``` |
0 commit comments