|
| 1 | +import { postgresTest } from "@internal/testcontainers"; |
| 2 | +import { generateWaitpointId } from "@trigger.dev/core/v3/isomorphic"; |
| 3 | +import { describe, expect } from "vitest"; |
| 4 | +import type { PrismaClient } from "@trigger.dev/database"; |
| 5 | +import { PostgresRunStore } from "./PostgresRunStore.js"; |
| 6 | +import type { CreateRunInput } from "./types.js"; |
| 7 | + |
| 8 | +// The _completedWaitpoints join has a foreign key to "Waitpoint". A waitpoint that lives |
| 9 | +// outside Postgres has no row there, and its snapshot link travels with the snapshot entry |
| 10 | +// instead. Offering such an id to the insert fails the constraint, and because the insert |
| 11 | +// shares the resume's transaction it takes the whole resume down: the run then never |
| 12 | +// continues. So those ids have to be dropped before the statement runs, not after it fails. |
| 13 | +describe("createExecutionSnapshot completed-waitpoint links", () => { |
| 14 | + postgresTest( |
| 15 | + "skips a waitpoint id that has no Postgres row instead of violating the foreign key", |
| 16 | + async ({ prisma }) => { |
| 17 | + const { organization, project, environment } = await seedEnvironment(prisma); |
| 18 | + const store = new PostgresRunStore({ prisma, readOnlyPrisma: prisma }); |
| 19 | + const runId = "run_mixed_links"; |
| 20 | + |
| 21 | + await store.createRun( |
| 22 | + buildCreateRunInput({ |
| 23 | + runId, |
| 24 | + organizationId: organization.id, |
| 25 | + projectId: project.id, |
| 26 | + runtimeEnvironmentId: environment.id, |
| 27 | + }) |
| 28 | + ); |
| 29 | + |
| 30 | + const legacyWaitpoint = await prisma.waitpoint.create({ |
| 31 | + data: { |
| 32 | + id: "waitpoint_legacy_link", |
| 33 | + friendlyId: "waitpoint_legacy_link", |
| 34 | + type: "MANUAL", |
| 35 | + status: "COMPLETED", |
| 36 | + idempotencyKey: "legacy-link", |
| 37 | + userProvidedIdempotencyKey: false, |
| 38 | + environmentId: environment.id, |
| 39 | + projectId: project.id, |
| 40 | + }, |
| 41 | + }); |
| 42 | + const storeWaitpointId = generateWaitpointId("MANUAL"); |
| 43 | + |
| 44 | + const snapshot = await store.createExecutionSnapshot( |
| 45 | + { |
| 46 | + run: { id: runId, status: "PENDING", attemptNumber: 1 }, |
| 47 | + snapshot: { executionStatus: "EXECUTING", description: "resumed" }, |
| 48 | + environmentId: environment.id, |
| 49 | + environmentType: "PRODUCTION", |
| 50 | + projectId: project.id, |
| 51 | + organizationId: organization.id, |
| 52 | + completedWaitpoints: [{ id: legacyWaitpoint.id }, { id: storeWaitpointId }], |
| 53 | + }, |
| 54 | + prisma |
| 55 | + ); |
| 56 | + |
| 57 | + const links = await prisma.$queryRaw<{ B: string }[]>` |
| 58 | + SELECT "B" FROM "_completedWaitpoints" WHERE "A" = ${snapshot.id}`; |
| 59 | + |
| 60 | + expect(links.map((link) => link.B)).toEqual([legacyWaitpoint.id]); |
| 61 | + } |
| 62 | + ); |
| 63 | +}); |
| 64 | + |
| 65 | +async function seedEnvironment(prisma: PrismaClient) { |
| 66 | + const organization = await prisma.organization.create({ |
| 67 | + data: { |
| 68 | + title: "Test Organization", |
| 69 | + slug: "test-organization", |
| 70 | + }, |
| 71 | + }); |
| 72 | + |
| 73 | + const project = await prisma.project.create({ |
| 74 | + data: { |
| 75 | + name: "Test Project", |
| 76 | + slug: "test-project", |
| 77 | + externalRef: "proj_1234", |
| 78 | + organizationId: organization.id, |
| 79 | + }, |
| 80 | + }); |
| 81 | + |
| 82 | + const environment = await prisma.runtimeEnvironment.create({ |
| 83 | + data: { |
| 84 | + type: "DEVELOPMENT", |
| 85 | + slug: "dev", |
| 86 | + projectId: project.id, |
| 87 | + organizationId: organization.id, |
| 88 | + apiKey: "tr_dev_apikey", |
| 89 | + pkApiKey: "pk_dev_apikey", |
| 90 | + shortcode: "short_code", |
| 91 | + }, |
| 92 | + }); |
| 93 | + |
| 94 | + return { organization, project, environment }; |
| 95 | +} |
| 96 | + |
| 97 | +function buildCreateRunInput(params: { |
| 98 | + runId: string; |
| 99 | + organizationId: string; |
| 100 | + projectId: string; |
| 101 | + runtimeEnvironmentId: string; |
| 102 | +}): CreateRunInput { |
| 103 | + return { |
| 104 | + data: { |
| 105 | + id: params.runId, |
| 106 | + engine: "V2", |
| 107 | + status: "PENDING", |
| 108 | + friendlyId: "run_friendly_1", |
| 109 | + runtimeEnvironmentId: params.runtimeEnvironmentId, |
| 110 | + environmentType: "DEVELOPMENT", |
| 111 | + organizationId: params.organizationId, |
| 112 | + projectId: params.projectId, |
| 113 | + taskIdentifier: "my-task", |
| 114 | + payload: "{}", |
| 115 | + payloadType: "application/json", |
| 116 | + traceContext: {}, |
| 117 | + traceId: "trace_1", |
| 118 | + spanId: "span_1", |
| 119 | + queue: "task/my-task", |
| 120 | + isTest: false, |
| 121 | + taskEventStore: "taskEvent", |
| 122 | + depth: 0, |
| 123 | + }, |
| 124 | + snapshot: { |
| 125 | + engine: "V2", |
| 126 | + executionStatus: "RUN_CREATED", |
| 127 | + description: "Run was created", |
| 128 | + runStatus: "PENDING", |
| 129 | + environmentId: params.runtimeEnvironmentId, |
| 130 | + environmentType: "DEVELOPMENT", |
| 131 | + projectId: params.projectId, |
| 132 | + organizationId: params.organizationId, |
| 133 | + }, |
| 134 | + }; |
| 135 | +} |
0 commit comments