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 migration scripts

Kiran Kfe7e7a0b8906
diff --git a/apps/web/app/api/workspaces/route.ts b/apps/web/app/api/workspaces/route.ts
index d8b722f4d4..b491566005 100644
--- a/apps/web/app/api/workspaces/route.ts
+++ b/apps/web/app/api/workspaces/route.ts
@@ -143,12 +143,6 @@ export const POST = withSession(async ({ req, session }) => {
},
});
- const domainRecord = await addDomain({
- ...addDomainBodySchema.parse({ slug: domain }),
- workspace: projectResponse as WorkspaceProps,
- userId: session.user.id,
- });
-
waitUntil(
(async () => {
if (session.user["defaultWorkspace"] === null) {
@@ -171,6 +165,12 @@ export const POST = withSession(async ({ req, session }) => {
},
});
} else {
+ const domainRecord = await addDomain({
+ ...addDomainBodySchema.parse({ slug: domain }),
+ workspace: projectResponse as WorkspaceProps,
+ userId: session.user.id,
+ });
+
await setRootDomain({
id: domainRecord.id,
domain,
diff --git a/apps/web/lib/api/domains/add-domain.ts b/apps/web/lib/api/domains/add-domain.ts
index 84311bac15..70bf9bddce 100644
--- a/apps/web/lib/api/domains/add-domain.ts
+++ b/apps/web/lib/api/domains/add-domain.ts
@@ -22,7 +22,6 @@ export const addDomain = async (input: AddDomainInput) => {
expiredUrl,
userId,
noindex,
- archived,
} = input;
// Add domain to the workspace
@@ -47,7 +46,7 @@ export const addDomain = async (input: AddDomainInput) => {
domain: domain.slug,
key: "_root",
createdAt: domain.createdAt,
- archived: archived,
+ archived: false,
proxy: false,
publicStats: false,
trackConversion: false,
diff --git a/apps/web/scripts/migrate-domains.ts b/apps/web/scripts/migrate-domains.ts
new file mode 100644
index 0000000000..c285603e1c
--- /dev/null
+++ b/apps/web/scripts/migrate-domains.ts
@@ -0,0 +1,71 @@
+import { createLink } from "@/lib/api/links";
+import { prisma } from "@/lib/prisma";
+import "dotenv-flow/config";
+
+// Standardize domains into links
+async function main() {
+ const domains = await prisma.domain.findMany({
+ include: {
+ project: {
+ select: {
+ id: true,
+ plan: true,
+ users: {
+ select: {
+ id: true,
+ },
+ },
+ },
+ },
+ },
+ take: 5000, // TODO: Adjust this based on the number of domains
+ });
+
+ const promises = domains.map(async (domain) => {
+ const user = domain?.project?.users[0];
+ const workspace = domain.project!;
+
+ const link = await createLink({
+ id: domain.id,
+ domain: domain.slug,
+ key: "_root",
+ description: domain.description,
+ publicStats: domain.publicStats,
+ projectId: domain.projectId,
+ userId: user?.id,
+ createdAt: domain.createdAt,
+ ...(workspace.plan === "free"
+ ? {
+ url: "",
+ expiredUrl: null,
+ rewrite: false,
+ noindex: false,
+ }
+ : {
+ url: domain.target || "",
+ expiredUrl: domain.expiredUrl || null,
+ rewrite: domain.type === "rewrite",
+ noindex: domain.noindex,
+ }),
+ archived: false,
+ proxy: false,
+ trackConversion: false,
+ });
+
+ return await prisma.link.update({
+ where: {
+ id: link.id,
+ },
+ data: {
+ lastClicked: domain.lastClicked,
+ clicks: domain.clicks,
+ },
+ });
+ });
+
+ const result = await Promise.allSettled(promises);
+
+ console.log(result);
+}
+
+main();