Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Retry staging sync without the slug when another workspace claims it

Kiran K24e5663d5daf
diff --git a/apps/web/lib/jobs/handlers/sync-staging-workspace-job.ts b/apps/web/lib/jobs/handlers/sync-staging-workspace-job.ts
index e445a875dc..8977c22afb 100644
--- a/apps/web/lib/jobs/handlers/sync-staging-workspace-job.ts
+++ b/apps/web/lib/jobs/handlers/sync-staging-workspace-job.ts
@@ -1,5 +1,8 @@
import { prisma } from "@/lib/prisma";
-import { getPreferredStagingSlug } from "@/lib/sandbox/staging-slug";
+import {
+ getPreferredStagingSlug,
+ isSlugUniqueConstraintError,
+} from "@/lib/sandbox/staging-slug";
import { Project } from "@prisma/client";
import * as z from "zod/v4";
import { defineJob } from "../index";
@@ -181,7 +184,8 @@ async function syncWorkspace({
const preferredSlug = getPreferredStagingSlug(workspace.slug);
// `{slug}-staging` may belong to another workspace, in which case the staging
- // workspace keeps its current (suffixed) slug.
+ // workspace keeps its current (suffixed) slug. A concurrent claim between
+ // this check and the update is handled the same way.
const slugOwner = await prisma.project.findUnique({
where: {
slug: preferredSlug,
@@ -194,17 +198,39 @@ async function syncWorkspace({
const canUsePreferredSlug =
!slugOwner || slugOwner.id === workspace.stagingWorkspaceId;
- await prisma.project.update({
- where: {
- id: workspace.stagingWorkspaceId,
- },
- data: {
- logo: workspace.logo,
- name: `${workspace.name} (Staging)`,
- ...(canUsePreferredSlug && { slug: preferredSlug }),
- plan: workspace.plan,
- planTier: workspace.planTier,
- planPeriod: workspace.planPeriod,
- },
- });
+ const data: Pick<
+ Project,
+ "logo" | "name" | "plan" | "planTier" | "planPeriod"
+ > = {
+ logo: workspace.logo,
+ name: `${workspace.name} (Staging)`,
+ plan: workspace.plan,
+ planTier: workspace.planTier,
+ planPeriod: workspace.planPeriod,
+ };
+
+ try {
+ await prisma.project.update({
+ where: {
+ id: workspace.stagingWorkspaceId,
+ },
+ data: {
+ ...data,
+ ...(canUsePreferredSlug && { slug: preferredSlug }),
+ },
+ });
+ } catch (error) {
+ // Another workspace claimed `{slug}-staging` after the ownership check.
+ // Keep the current slug and still apply plan and branding.
+ if (!canUsePreferredSlug || !isSlugUniqueConstraintError(error)) {
+ throw error;
+ }
+
+ await prisma.project.update({
+ where: {
+ id: workspace.stagingWorkspaceId,
+ },
+ data,
+ });
+ }
}
diff --git a/apps/web/lib/sandbox/staging-slug.ts b/apps/web/lib/sandbox/staging-slug.ts
index 0b56ac8f09..23abb12037 100644
--- a/apps/web/lib/sandbox/staging-slug.ts
+++ b/apps/web/lib/sandbox/staging-slug.ts
@@ -7,7 +7,7 @@ export function getPreferredStagingSlug(slug: string) {
return `${slug}-staging`;
}
-function isSlugUniqueConstraintError(error: unknown) {
+export function isSlugUniqueConstraintError(error: unknown) {
if (
!(error instanceof Prisma.PrismaClientKnownRequestError) ||
error.code !== "P2002"