Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .server-changes/prevent-duplicate-root-environments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
area: webapp
type: fix
---

Prevent duplicate Staging and Preview environments when account setup requests overlap
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type ActionFunctionArgs, json } from "@remix-run/server-runtime";
import {
Prisma,
type RuntimeEnvironment,
type Organization,
type Project,
Expand Down Expand Up @@ -61,9 +62,16 @@ async function upsertEnvironment(
type: RuntimeEnvironmentType,
isBranchableEnvironment: boolean
) {
const existingEnvironment = project.environments.find((env) => env.type === type);
const existingEnvironment = project.environments.find(
(env) => env.type === type && env.parentEnvironmentId === null
);

if (!existingEnvironment) {
if (existingEnvironment) {
await updateEnvConcurrencyLimits({ ...existingEnvironment, organization, project });
return { status: "updated", environment: existingEnvironment };
}

try {
const newEnvironment = await createEnvironment({
organization,
project,
Expand All @@ -72,8 +80,23 @@ async function upsertEnvironment(
});
await updateEnvConcurrencyLimits({ ...newEnvironment, organization, project });
return { status: "created", environment: newEnvironment };
} else {
await updateEnvConcurrencyLimits({ ...existingEnvironment, organization, project });
return { status: "updated", environment: existingEnvironment };
} catch (error) {
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2002") {
const existingAfterConflict = await prisma.runtimeEnvironment.findFirst({
where: {
organizationId: organization.id,
projectId: project.id,
type,
parentEnvironmentId: null,
},
});

if (existingAfterConflict) {
await updateEnvConcurrencyLimits({ ...existingAfterConflict, organization, project });
return { status: "updated", environment: existingAfterConflict };
}
}

throw error;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS "RuntimeEnvironment_projectId_type_staging_preview_root_key"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not merge until this has been backfilled.

ON "RuntimeEnvironment" ("projectId", "type")
WHERE "parentEnvironmentId" IS NULL
AND "type" IN ('STAGING', 'PREVIEW');
2 changes: 2 additions & 0 deletions internal-packages/database/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ model RuntimeEnvironment {
taskIdentifiers TaskIdentifier[]
revokedApiKeys RevokedApiKey[]

// A partial unique index also enforces one STAGING/PREVIEW root per project and type.
// It is defined in SQL because Prisma does not support partial indexes in the schema.
@@unique([projectId, slug, orgMemberId])
@@unique([projectId, shortcode])
@@index([parentEnvironmentId])
Expand Down