perf(webapp): scope env var create pre-check to submitted keys (#4579)

## Summary

Setting or importing environment variables ran a conflict pre-check that
loaded every variable in the project and every value across all of its
environments, only to decide whether the submitted keys already had a
value in the target environments. On projects with many variables and
environments that meant reading tens of thousands of rows on each
create/import call.

This scopes the pre-check to the submitted keys and target environments,
so it reads only the rows it actually inspects (submitted keys × target
envs), wrapped in `boundedIn` to keep the prepared-statement cache
stable. Same conflict detection, a handful of rows instead of the whole
project's env-var values.
This commit is contained in:
Eric Allam
2026-08-12 08:15:27 +01:00
committed by GitHub
parent 9a3bee0288
commit 26cdedda1c
2 changed files with 18 additions and 1 deletions
@@ -0,0 +1,6 @@
---
area: webapp
type: improvement
---
Speed up setting and importing environment variables for projects with many variables.
@@ -1,5 +1,10 @@
import type { AuthenticatedEnvironment } from "@trigger.dev/core/v3/auth/environment";
import { Prisma, type PrismaClient, type RuntimeEnvironmentType } from "@trigger.dev/database";
import {
boundedIn,
Prisma,
type PrismaClient,
type RuntimeEnvironmentType,
} from "@trigger.dev/database";
import { z } from "zod";
import { environmentFullTitle } from "~/components/environments/EnvironmentLabel";
import { $replica, $transaction, prisma, type PrismaReplicaClient } from "~/db.server";
@@ -66,9 +71,15 @@ export class EnvironmentVariablesRepository implements Repository {
},
},
environmentVariables: {
where: {
key: { in: boundedIn(options.variables.map((v) => v.key)) },
},
select: {
key: true,
values: {
where: {
environmentId: { in: boundedIn(options.environmentIds) },
},
select: {
environment: {
select: { id: true, type: true },