Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(evals): drop foreign key on job configurations (#15994)

Tobias Wochingerb845e764c129
diff --git a/packages/shared/prisma/migrations/20260807120000_drop_job_execution_configuration_fk/migration.sql b/packages/shared/prisma/migrations/20260807120000_drop_job_execution_configuration_fk/migration.sql
new file mode 100644
index 0000000000..7558306476
--- /dev/null
+++ b/packages/shared/prisma/migrations/20260807120000_drop_job_execution_configuration_fk/migration.sql
@@ -0,0 +1,10 @@
+-- Prisma does not automatically wrap PostgreSQL migrations in a transaction.
+BEGIN;
+
+SET LOCAL lock_timeout = '5s';
+SET LOCAL statement_timeout = '30s';
+
+ALTER TABLE "job_executions"
+ DROP CONSTRAINT IF EXISTS "job_executions_job_configuration_id_fkey";
+
+COMMIT;
diff --git a/packages/shared/prisma/schema.prisma b/packages/shared/prisma/schema.prisma
index a94174b834..88ca872fbb 100644
--- a/packages/shared/prisma/schema.prisma
+++ b/packages/shared/prisma/schema.prisma
@@ -953,7 +953,6 @@ model JobConfiguration {
sampling Decimal // ratio of jobs that are executed for sampling (0..1)
delay Int // delay in milliseconds
timeScope String[] @default(["NEW"]) @map("time_scope")
- JobExecution JobExecution[]
@@index([projectId, id])
@@map("job_configurations")
@@ -975,8 +974,7 @@ model JobExecution {
projectId String @map("project_id")
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
- jobConfigurationId String @map("job_configuration_id")
- jobConfiguration JobConfiguration @relation(fields: [jobConfigurationId], references: [id], onDelete: Cascade)
+ jobConfigurationId String @map("job_configuration_id") // no fk constraint - deletion handled in application code (deleteJobConfigurationWithExecutions) and via project cascade
jobTemplateId String? @map("job_template_id")
diff --git a/web/src/__tests__/server/evaluator-repository.servertest.ts b/web/src/__tests__/server/evaluator-repository.servertest.ts
index e6b88037c8..87646d6287 100644
--- a/web/src/__tests__/server/evaluator-repository.servertest.ts
+++ b/web/src/__tests__/server/evaluator-repository.servertest.ts
@@ -2,6 +2,7 @@ import { EvalTemplateType } from "@prisma/client";
import { prisma } from "@langfuse/shared/src/db";
import { createOrgProjectAndApiKey } from "@langfuse/shared/src/server";
import {
+ deleteJobConfigurationWithExecutions,
deleteEvalTemplatesByIds,
findDefaultModelEvalTemplateIds,
findEvalTemplateById,
@@ -106,6 +107,41 @@ describe("evaluatorRepository", () => {
});
});
+ describe("deleteJobConfigurationWithExecutions", () => {
+ it("deletes executions before deleting their job configuration", async () => {
+ const project = await prepareProject();
+ const template = await createTemplate({
+ projectId: project.id,
+ name: "delete-config-with-executions",
+ });
+ const config = await createJobConfiguration({
+ projectId: project.id,
+ evalTemplateId: template.id,
+ scoreName: "delete-config-with-executions",
+ });
+ const execution = await prisma.jobExecution.create({
+ data: {
+ projectId: project.id,
+ jobConfigurationId: config.id,
+ status: "PENDING",
+ },
+ });
+
+ await deleteJobConfigurationWithExecutions({
+ prisma,
+ projectId: project.id,
+ jobConfigurationId: config.id,
+ });
+
+ await expect(
+ prisma.jobExecution.findUnique({ where: { id: execution.id } }),
+ ).resolves.toBeNull();
+ await expect(
+ prisma.jobConfiguration.findUnique({ where: { id: config.id } }),
+ ).resolves.toBeNull();
+ });
+ });
+
describe("findEvalTemplateById", () => {
it("returns the template or null", async () => {
const project = await prepareProject();
diff --git a/web/src/features/evals/server/evaluatorRepository.ts b/web/src/features/evals/server/evaluatorRepository.ts
index 5e4e2717a5..5175de6b58 100644
--- a/web/src/features/evals/server/evaluatorRepository.ts
+++ b/web/src/features/evals/server/evaluatorRepository.ts
@@ -1,4 +1,8 @@
-import { EvalTemplateType, Prisma } from "@langfuse/shared/src/db";
+import {
+ EvalTemplateType,
+ Prisma,
+ type PrismaClient,
+} from "@langfuse/shared/src/db";
/**
* Data access for eval templates ("evaluators" in the public naming).
@@ -117,3 +121,28 @@ export async function deleteEvalTemplatesByIds({
where: { projectId, id: { in: evalTemplateIds } },
});
}
+
+/**
+ * Deletes a job configuration and its executions atomically. Takes the plain
+ * client rather than a `tx` because it runs a batch (array) transaction: an
+ * interactive `$transaction(async (tx) => …)` would cap the delete at Prisma's
+ * default 5s timeout, which configurations with a long execution history exceed.
+ */
+export async function deleteJobConfigurationWithExecutions({
+ prisma,
+ projectId,
+ jobConfigurationId,
+}: {
+ prisma: PrismaClient;
+ projectId: string;
+ jobConfigurationId: string;
+}) {
+ await prisma.$transaction([
+ prisma.jobExecution.deleteMany({
+ where: { projectId, jobConfigurationId },
+ }),
+ prisma.jobConfiguration.delete({
+ where: { id: jobConfigurationId, projectId },
+ }),
+ ]);
+}
diff --git a/web/src/features/evals/server/router.ts b/web/src/features/evals/server/router.ts
index 35a7bf4524..7fa3f5797b 100644
--- a/web/src/features/evals/server/router.ts
+++ b/web/src/features/evals/server/router.ts
@@ -96,6 +96,7 @@ import {
prepareConfigsForTemplateUpgrade,
prepareVariableMappingForEvaluatorUpgrade,
} from "@/src/features/evals/server/evaluatorUpgrade";
+import { deleteJobConfigurationWithExecutions } from "@/src/features/evals/server/evaluatorRepository";
export { CreateEvalTemplateInputSchema } from "@/src/features/evals/server/evalTemplateCreation";
// Filter columns that used to be backed by the Postgres `traces` and
@@ -1751,11 +1752,10 @@ export const evalRouter = createTRPCRouter({
action: "delete",
});
- await ctx.prisma.jobConfiguration.delete({
- where: {
- id: evalConfigId,
- projectId: projectId,
- },
+ await deleteJobConfigurationWithExecutions({
+ prisma: ctx.prisma,
+ projectId,
+ jobConfigurationId: evalConfigId,
});
// Clear the "no job configs" caches to ensure they are re-evaluated
diff --git a/web/src/features/evals/server/unstable-public-api/evaluation-rule-service.ts b/web/src/features/evals/server/unstable-public-api/evaluation-rule-service.ts
index d69c3ef585..4b7c1f5e96 100644
--- a/web/src/features/evals/server/unstable-public-api/evaluation-rule-service.ts
+++ b/web/src/features/evals/server/unstable-public-api/evaluation-rule-service.ts
@@ -43,6 +43,7 @@ import {
} from "./validation";
import { createUnstablePublicApiError } from "@/src/features/public-api/server/unstable-public-api-error-contract";
import { assertUnreachable } from "@/src/utils/types";
+import { deleteJobConfigurationWithExecutions } from "@/src/features/evals/server/evaluatorRepository";
const MAX_ACTIVE_EVALUATION_RULES = 500;
@@ -473,11 +474,10 @@ export async function deletePublicEvaluationRule(params: {
const existing = await findPublicEvaluationRuleOrThrow(params);
const existingPublic = toApiWritableEvaluationRule(existing);
- await prisma.jobConfiguration.delete({
- where: {
- id: params.evaluationRuleId,
- projectId: params.projectId,
- },
+ await deleteJobConfigurationWithExecutions({
+ prisma,
+ projectId: params.projectId,
+ jobConfigurationId: params.evaluationRuleId,
});
await invalidateProjectEvalConfigCaches(params.projectId);