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.

Add backfill script for staging workspaces on paid plans

Kiran K95d422ab0f37
diff --git a/apps/web/lib/sandbox/create-staging-workspace.ts b/apps/web/lib/sandbox/create-staging-workspace.ts
index 5d98f442df..64f303e82a 100644
--- a/apps/web/lib/sandbox/create-staging-workspace.ts
+++ b/apps/web/lib/sandbox/create-staging-workspace.ts
@@ -47,7 +47,6 @@ export async function createStagingWorkspace(workspaceId: string) {
const stagingWorkspaceId = createWorkspaceId();
await prisma.$transaction([
- // Create a staging workspace
prisma.project.create({
data: {
id: stagingWorkspaceId,
@@ -76,7 +75,6 @@ export async function createStagingWorkspace(workspaceId: string) {
},
}),
- // Update live to point to the staging workspace
prisma.project.update({
where: {
id: workspace.id,
diff --git a/apps/web/scripts/migrations/backfill-staging-workspaces.ts b/apps/web/scripts/migrations/backfill-staging-workspaces.ts
new file mode 100644
index 0000000000..0a67eb3691
--- /dev/null
+++ b/apps/web/scripts/migrations/backfill-staging-workspaces.ts
@@ -0,0 +1,68 @@
+import { createStagingWorkspace } from "@/lib/sandbox/create-staging-workspace";
+import { prisma } from "@dub/prisma";
+import { WorkspaceEnvironment } from "@dub/prisma/client";
+import "dotenv-flow/config";
+
+const BATCH_SIZE = 50;
+
+async function main() {
+ let totalProcessed = 0;
+ let totalCreated = 0;
+ let totalFailed = 0;
+
+ // TODO:
+ // We should skip workspaces where the staging workspace was created manually.
+
+ while (true) {
+ const workspaces = await prisma.project.findMany({
+ where: {
+ environment: WorkspaceEnvironment.production,
+ stagingWorkspaceId: null,
+ plan: {
+ notIn: ["free", "pro"],
+ },
+ },
+ select: {
+ id: true,
+ slug: true,
+ plan: true,
+ },
+ take: BATCH_SIZE,
+ orderBy: {
+ createdAt: "asc",
+ },
+ });
+
+ if (workspaces.length === 0) {
+ break;
+ }
+
+ for (const workspace of workspaces) {
+ try {
+ await createStagingWorkspace(workspace.id);
+ totalCreated++;
+ console.log(`Created staging workspace for ${workspace.slug}`);
+ } catch (error) {
+ totalFailed++;
+ console.error(
+ `Failed to create staging workspace for ${workspace.slug} (${workspace.id}):`,
+ error,
+ );
+ }
+
+ await new Promise((resolve) => setTimeout(resolve, 1000));
+ }
+
+ totalProcessed += workspaces.length;
+
+ console.log(
+ `Processed batch of ${workspaces.length} workspaces (processed=${totalProcessed}, created=${totalCreated}, failed=${totalFailed})`,
+ );
+ }
+
+ console.log(
+ `Done creating staging workspaces (processed=${totalProcessed}, created=${totalCreated}, failed=${totalFailed})`,
+ );
+}
+
+main();