|
| 1 | +// blockRunWithWaitpointEdges' legacy branch joined `FROM "Waitpoint" w`, so a LEGACY run blocking on |
| 2 | +// a NEW-resident (run-ops) token — whose Waitpoint row lives on #new — matched 0 rows and wrote NO |
| 3 | +// blocking edge. countPendingWaitpoints then fans out, still sees the token PENDING, so the run is |
| 4 | +// suspended believing it's blocked while nothing will ever resume it -> silent hang. The fix sources |
| 5 | +// the edge rows from the id array via `unnest` (FK-free, mirroring the dedicated branch); a migration |
| 6 | +// drops the _WaitpointRunConnections -> Waitpoint FK so the cross-DB connection can be recorded too. |
| 7 | +// Real two-DB topology; never mocked. |
| 8 | + |
| 9 | +import { heteroRunOpsPostgresTest } from "@internal/testcontainers"; |
| 10 | +import type { PrismaClient } from "@trigger.dev/database"; |
| 11 | +import type { RunOpsPrismaClient } from "@internal/run-ops-database"; |
| 12 | +import { expect } from "vitest"; |
| 13 | +import { PostgresRunStore } from "./PostgresRunStore.js"; |
| 14 | +import { RoutingRunStore } from "./runOpsStore.js"; |
| 15 | + |
| 16 | +const CUID_25 = "c".repeat(25); // LEGACY run -> #legacy (prisma14, full schema) |
| 17 | + |
| 18 | +function makeRouter(prisma14: PrismaClient, prisma17: RunOpsPrismaClient) { |
| 19 | + const newStore = new PostgresRunStore({ |
| 20 | + prisma: prisma17 as never, |
| 21 | + readOnlyPrisma: prisma17 as never, |
| 22 | + schemaVariant: "dedicated", |
| 23 | + }); |
| 24 | + const legacyStore = new PostgresRunStore({ |
| 25 | + prisma: prisma14, |
| 26 | + readOnlyPrisma: prisma14, |
| 27 | + schemaVariant: "legacy", |
| 28 | + }); |
| 29 | + return new RoutingRunStore({ new: newStore, legacy: legacyStore }); |
| 30 | +} |
| 31 | + |
| 32 | +async function seedEnvironmentLegacy(prisma: PrismaClient, suffix: string) { |
| 33 | + const organization = await prisma.organization.create({ |
| 34 | + data: { title: `Org ${suffix}`, slug: `org-${suffix}` }, |
| 35 | + }); |
| 36 | + const project = await prisma.project.create({ |
| 37 | + data: { |
| 38 | + name: `Project ${suffix}`, |
| 39 | + slug: `project-${suffix}`, |
| 40 | + externalRef: `proj_${suffix}`, |
| 41 | + organizationId: organization.id, |
| 42 | + }, |
| 43 | + }); |
| 44 | + const environment = await prisma.runtimeEnvironment.create({ |
| 45 | + data: { |
| 46 | + type: "DEVELOPMENT", |
| 47 | + slug: "dev", |
| 48 | + projectId: project.id, |
| 49 | + organizationId: organization.id, |
| 50 | + apiKey: `tr_dev_${suffix}`, |
| 51 | + pkApiKey: `pk_dev_${suffix}`, |
| 52 | + shortcode: `short_${suffix}`, |
| 53 | + }, |
| 54 | + }); |
| 55 | + return { organization, project, environment }; |
| 56 | +} |
| 57 | + |
| 58 | +function taskRunData(opts: { |
| 59 | + id: string; |
| 60 | + organizationId: string; |
| 61 | + projectId: string; |
| 62 | + runtimeEnvironmentId: string; |
| 63 | +}) { |
| 64 | + return { |
| 65 | + id: opts.id, |
| 66 | + engine: "V2" as const, |
| 67 | + status: "EXECUTING" as const, |
| 68 | + friendlyId: `run_${opts.id}`, |
| 69 | + runtimeEnvironmentId: opts.runtimeEnvironmentId, |
| 70 | + environmentType: "DEVELOPMENT" as const, |
| 71 | + organizationId: opts.organizationId, |
| 72 | + projectId: opts.projectId, |
| 73 | + taskIdentifier: "xdb-task", |
| 74 | + payload: "{}", |
| 75 | + payloadType: "application/json", |
| 76 | + traceContext: {}, |
| 77 | + traceId: `trace_${opts.id}`, |
| 78 | + spanId: `span_${opts.id}`, |
| 79 | + queue: "task/xdb-task", |
| 80 | + isTest: false, |
| 81 | + taskEventStore: "taskEvent", |
| 82 | + depth: 0, |
| 83 | + }; |
| 84 | +} |
| 85 | + |
| 86 | +describe("run-ops split — a LEGACY run blocking on a NEW-resident token gets its blocking edge (cross-DB)", () => { |
| 87 | + heteroRunOpsPostgresTest( |
| 88 | + "blockRunWithWaitpointEdges writes the edge for a cross-DB token instead of stranding the run", |
| 89 | + async ({ prisma14, prisma17 }: { prisma14: PrismaClient; prisma17: RunOpsPrismaClient }) => { |
| 90 | + const router = makeRouter(prisma14, prisma17); |
| 91 | + // The harness builds the schema with `prisma db push`, which re-creates the FKs that the |
| 92 | + // run-ops split migrations (20260705210000 / 20260705220000) drop in prod. Mirror those drops on |
| 93 | + // this clone so the cross-DB insert exercises the real prod state (FKs gone, app-enforced). |
| 94 | + await prisma14.$executeRawUnsafe( |
| 95 | + `ALTER TABLE "TaskRunWaitpoint" DROP CONSTRAINT IF EXISTS "TaskRunWaitpoint_waitpointId_fkey"` |
| 96 | + ); |
| 97 | + await prisma14.$executeRawUnsafe( |
| 98 | + `ALTER TABLE "_WaitpointRunConnections" DROP CONSTRAINT IF EXISTS "_WaitpointRunConnections_B_fkey"` |
| 99 | + ); |
| 100 | + const seed = await seedEnvironmentLegacy(prisma14, "xdbblock"); |
| 101 | + const runId = `run_${CUID_25}`; // LEGACY run, resident on #legacy |
| 102 | + await prisma14.taskRun.create({ |
| 103 | + data: taskRunData({ |
| 104 | + id: runId, |
| 105 | + organizationId: seed.organization.id, |
| 106 | + projectId: seed.project.id, |
| 107 | + runtimeEnvironmentId: seed.environment.id, |
| 108 | + }), |
| 109 | + }); |
| 110 | + |
| 111 | + // The token was minted co-located with a run-ops run, so its Waitpoint row lives on #new. |
| 112 | + const tokenId = "waitpoint_" + "x".repeat(20); |
| 113 | + await prisma17.waitpoint.create({ |
| 114 | + data: { |
| 115 | + id: tokenId, |
| 116 | + friendlyId: "wp_xdb", |
| 117 | + type: "MANUAL", |
| 118 | + status: "PENDING", |
| 119 | + idempotencyKey: `idem_${tokenId}`, |
| 120 | + userProvidedIdempotencyKey: false, |
| 121 | + projectId: seed.project.id, |
| 122 | + environmentId: seed.environment.id, |
| 123 | + }, |
| 124 | + }); |
| 125 | + |
| 126 | + await router.blockRunWithWaitpointEdges({ |
| 127 | + runId, |
| 128 | + waitpointIds: [tokenId], |
| 129 | + projectId: seed.project.id, |
| 130 | + }); |
| 131 | + |
| 132 | + // RED: the legacy `FROM "Waitpoint"` join matches 0 rows -> no edge -> the run is stranded. |
| 133 | + // GREEN: `unnest` writes the edge from the id directly. |
| 134 | + expect( |
| 135 | + await prisma14.taskRunWaitpoint.count({ where: { taskRunId: runId, waitpointId: tokenId } }) |
| 136 | + ).toBe(1); |
| 137 | + // The historical connection is recorded too (needs the dropped B-fkey migration). |
| 138 | + const conn = (await prisma14.$queryRaw` |
| 139 | + SELECT "B" FROM "_WaitpointRunConnections" WHERE "A" = ${runId} |
| 140 | + `) as unknown[]; |
| 141 | + expect(conn.length).toBe(1); |
| 142 | + } |
| 143 | + ); |
| 144 | +}); |
0 commit comments