Skip to content

Commit 4325052

Browse files
authored
fix(run-store): fix batch idempotency lookup on the dedicated run-ops store (#4271)
## Summary `batchTrigger` requests that set a per-item `idempotencyKey` failed with a 500 when the run-store is split across databases: the per-item idempotency lookup errored before any run was created. Batches without per-item keys, single `trigger` idempotency, and batch-level (`idempotency-key` header) idempotency were unaffected. ## Root cause `findRunsByIdempotencyKeys` built its `UNION ALL` of per-key point-lookups with `@trigger.dev/database`'s `Prisma.sql` / `Prisma.join`, then executed it on whichever store client it was handed. On the dedicated run-ops store that client is a *separate* generated Prisma client, and a `Sql` object from a different generated client is not recognized: the bare `$queryRaw(Prisma.join(...))` form dropped the query text entirely (`Argument \`query\` is missing`). The tagged-template form is no better here: joining nested `Prisma.sql` fragments across the two clients mis-numbers the bound parameters (`syntax error at or near "$1"`). ## Fix Build the lookup as a plain parameterized string and run it via `$queryRawUnsafe` with positional placeholders and bound values, so it no longer depends on which generated client executes it. The query text contains only static SQL and integer placeholders; every value (`runtimeEnvironmentId`, `taskIdentifier`, each key) is bound, so it is not a raw-interpolation site. Same per-key point-lookup shape as before, no change on the single-client path. Verified end-to-end against a bundled build with the run-store split enabled: before the fix, `batchTrigger` with a per-item key 500s; after, it returns the runs and dedups correctly across fresh, repeat, and mixed batches.
1 parent 80cbc46 commit 4325052

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
area: webapp
3+
type: fix
4+
---
5+
6+
Fix batchTrigger requests that set a per-item idempotency key failing with an error instead of creating and deduplicating the runs

internal-packages/run-store/src/PostgresRunStore.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export interface RunOpsCapableClient {
7272
// Standalone entity keyed by (environmentId, name); present on both schemas.
7373
waitpointTag: RunOpsDelegate<"upsert" | "findMany">;
7474
$queryRaw: PrismaClient["$queryRaw"];
75+
$queryRawUnsafe: PrismaClient["$queryRawUnsafe"];
7576
$executeRaw: PrismaClient["$executeRaw"];
7677
}
7778

@@ -1691,11 +1692,16 @@ export class PostgresRunStore implements RunStore {
16911692
return [];
16921693
}
16931694
const prisma = (client ?? this.readOnlyPrisma) as RunOpsCapableClient;
1694-
const branches = args.idempotencyKeys.map(
1695-
(key) =>
1696-
Prisma.sql`SELECT "friendlyId", "idempotencyKey", "idempotencyKeyExpiresAt" FROM "TaskRun" WHERE "runtimeEnvironmentId" = ${args.runtimeEnvironmentId} AND "taskIdentifier" = ${args.taskIdentifier} AND "idempotencyKey" = ${key}`
1695+
const params: string[] = [];
1696+
const branches = args.idempotencyKeys.map((key) => {
1697+
const base = params.length;
1698+
params.push(args.runtimeEnvironmentId, args.taskIdentifier, key);
1699+
return `SELECT "friendlyId", "idempotencyKey", "idempotencyKeyExpiresAt" FROM "TaskRun" WHERE "runtimeEnvironmentId" = $${base + 1} AND "taskIdentifier" = $${base + 2} AND "idempotencyKey" = $${base + 3}`;
1700+
});
1701+
return prisma.$queryRawUnsafe<IdempotencyKeyRunMatch[]>(
1702+
branches.join(" UNION ALL "),
1703+
...params
16971704
);
1698-
return prisma.$queryRaw<IdempotencyKeyRunMatch[]>(Prisma.join(branches, " UNION ALL "));
16991705
}
17001706

17011707
// --- run-ops persistence ---

0 commit comments

Comments
 (0)