Skip to content

Commit f333537

Browse files
committed
Add safe rootTaskRunId index and a README to @trigger.dev/database
1 parent bff38dc commit f333537

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

packages/database/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
## @trigger.dev/database
2+
3+
This is the internal database package for the Trigger.dev project. It exports a generated prisma client that can be instantiated with a connection string.
4+
5+
### How to add a new index on a large table
6+
7+
1. Modify the Prisma.schema with a single index change (no other changes, just one index at a time)
8+
2. Create a Prisma migration using `cd packages/database && pnpm run db:migrate:dev --create-only`
9+
3. Modify the SQL file: add IF NOT EXISTS to it and CONCURRENTLY:
10+
11+
```sql
12+
CREATE INDEX CONCURRENTLY IF NOT EXISTS "JobRun_eventId_idx" ON "JobRun" ("eventId");
13+
```
14+
15+
4. Don’t apply the Prisma migration locally yet. This is a good opportunity to test the flow.
16+
5. Manually apply the index to your database, by running the index command.
17+
6. Then locally run `pnpm run db:migrate:deploy`
18+
19+
#### Before deploying
20+
21+
Run the index creation before deploying
22+
23+
```sql
24+
CREATE INDEX CONCURRENTLY IF NOT EXISTS "JobRun_eventId_idx" ON "JobRun" ("eventId");
25+
```
26+
27+
These commands are useful:
28+
29+
```sql
30+
-- creates an index safely, this can take a long time (2 mins maybe)
31+
CREATE INDEX CONCURRENTLY IF NOT EXISTS "JobRun_eventId_idx" ON "JobRun" ("eventId");
32+
-- checks the status of an index
33+
SELECT * FROM pg_stat_progress_create_index WHERE relid = '"JobRun"'::regclass;
34+
-- checks if the index is there
35+
SELECT * FROM pg_indexes WHERE tablename = 'JobRun' AND indexname = 'JobRun_eventId_idx';
36+
```
37+
38+
Now, when you deploy and prisma runs the migration, it will skip the index creation because it already exists. If you don't do this, there will be pain.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- CreateIndex
2+
CREATE INDEX CONCURRENTLY IF NOT EXISTS "TaskRun_rootTaskRunId_idx" ON "TaskRun"("rootTaskRunId");

packages/database/prisma/schema.prisma

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,6 +1751,8 @@ model TaskRun {
17511751
@@unique([runtimeEnvironmentId, taskIdentifier, idempotencyKey])
17521752
// Finding child runs
17531753
@@index([parentTaskRunId])
1754+
// Finding ancestor runs
1755+
@@index([rootTaskRunId])
17541756
// Task activity graph
17551757
@@index([projectId, createdAt, taskIdentifier])
17561758
//Runs list

0 commit comments

Comments
 (0)