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.

update script

Steven Teyc6f80658e32a
diff --git a/apps/web/app/api/cron/migrate/route.ts b/apps/web/app/api/cron/migrate/route.ts
index bcd52a6c6b..e3bb9e1030 100644
--- a/apps/web/app/api/cron/migrate/route.ts
+++ b/apps/web/app/api/cron/migrate/route.ts
@@ -3,10 +3,9 @@ import { linkCache } from "@/lib/api/links/cache";
import { encodeKeyIfCaseSensitive } from "@/lib/api/links/case-sensitivity";
import { qstash } from "@/lib/cron";
import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
-import { verifyVercelSignature } from "@/lib/cron/verify-vercel";
import { prisma } from "@dub/prisma";
import { Prisma } from "@dub/prisma/client";
-import { APP_DOMAIN_WITH_NGROK, linkConstructorSimple, log } from "@dub/utils";
+import { APP_DOMAIN_WITH_NGROK, linkConstructorSimple } from "@dub/utils";
import { NextResponse } from "next/server";
export const dynamic = "force-dynamic";
@@ -14,70 +13,66 @@ export const dynamic = "force-dynamic";
/***
One off cron to migrate
*/
-async function handler(req: Request) {
+export async function POST(req: Request) {
const domain = "buff.ly";
const userId = "user_EzRuKzR9sG3WmHapVV6aEec7";
const oldFolderId = "fold_LIZsdjTgFVbQVGYSUmYAi5vT";
const newFolderId = "fold_1JNQBVZV8P0NA0YGB11W2HHSQ";
try {
- if (req.method === "GET") {
- await verifyVercelSignature(req);
- } else if (req.method === "POST") {
- await verifyQstashSignature({
- req,
- rawBody: await req.text(),
- });
- }
-
- const where: Prisma.LinkWhereInput = {
- userId,
- domain,
- folderId: oldFolderId,
- createdAt: {
- lte: new Date("2025-03-07T16:33:32.084Z"),
- },
- };
-
- const links = await prisma.link.findMany({
- where,
- select: {
- id: true,
- domain: true,
- key: true,
- shortLink: true,
- },
- take: 100,
- orderBy: {
- createdAt: "desc",
- },
+ await verifyQstashSignature({
+ req,
+ rawBody: await req.text(),
});
+ } catch (error) {
+ return handleAndReturnErrorResponse(error);
+ }
- if (!links.length) {
- console.log("No more links to migrate.");
- return NextResponse.json({ message: "Successfully migrated all links" });
- }
-
- const remainingLinks = await prisma.link.count({
- where,
- });
+ const where: Prisma.LinkWhereInput = {
+ userId,
+ domain,
+ folderId: oldFolderId,
+ createdAt: {
+ lte: new Date("2025-03-07T16:33:32.084Z"),
+ },
+ };
+
+ const links = await prisma.link.findMany({
+ where,
+ select: {
+ id: true,
+ domain: true,
+ key: true,
+ shortLink: true,
+ },
+ take: 100,
+ orderBy: {
+ createdAt: "desc",
+ },
+ });
+
+ console.log(`Found ${links.length} links to migrate...`);
+
+ if (!links.length) {
+ console.log("No more links to migrate.");
+ return NextResponse.json({ message: "Successfully migrated all links" });
+ }
- console.log(
- `Remaining links to migrate after this batch: ${remainingLinks - 100}`,
- );
+ const linksToDelete: string[] = [];
- await Promise.allSettled(
- links.map(async (link) => {
- const newKey = encodeKeyIfCaseSensitive({
- domain,
- key: link.key,
- });
+ await Promise.allSettled(
+ links.map(async (link) => {
+ const newKey = encodeKeyIfCaseSensitive({
+ domain,
+ key: link.key,
+ });
- const newShortLink = linkConstructorSimple({
- domain,
- key: newKey,
- });
+ const newShortLink = linkConstructorSimple({
+ domain,
+ key: newKey,
+ });
+ try {
await prisma.link.update({
where: {
id: link.id,
@@ -92,29 +87,37 @@ async function handler(req: Request) {
console.log(
`Updated link ${link.id} from ${link.shortLink} to ${newShortLink} and new folder ${newFolderId}`,
);
- }),
- );
-
- // expire the Redis cache for the links so it fetches the latest version from the database
- await linkCache.expireMany(links);
-
- await qstash.publishJSON({
- url: `${APP_DOMAIN_WITH_NGROK}/api/cron/migrate`,
- method: "POST",
- body: {},
+ } catch (error) {
+ // if the link already exists, delete it
+ if (error.code === "P2002") {
+ console.log(
+ `Link ${link.id} (${link.domain}/${link.key}) already exists, deleting...`,
+ );
+ linksToDelete.push(link.id);
+ }
+ }
+ }),
+ );
+
+ if (linksToDelete.length) {
+ console.log(`Deleting ${linksToDelete.length} links...`);
+ await prisma.link.deleteMany({
+ where: {
+ id: { in: linksToDelete },
+ },
});
+ }
- return NextResponse.json({
- status: `Migrated ${links.length} links, ${remainingLinks - 100} remaining...`,
- });
- } catch (error) {
- await log({
- message: `Links and domain cleanup failed - ${error.message}`,
- type: "errors",
- });
+ // expire the Redis cache for the links so it fetches the latest version from the database
+ await linkCache.expireMany(links);
- return handleAndReturnErrorResponse(error);
- }
-}
+ await qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/migrate`,
+ method: "POST",
+ body: {},
+ });
-export { handler as GET, handler as POST };
+ return NextResponse.json({
+ status: `Migrated ${links.length} links and deleted ${linksToDelete.length} links.`,
+ });
+}
diff --git a/apps/web/scripts/buffer/migrate-to-case-sensitive.ts b/apps/web/scripts/buffer/migrate-to-case-sensitive.ts
index 79f681be94..778b8ee98d 100644
--- a/apps/web/scripts/buffer/migrate-to-case-sensitive.ts
+++ b/apps/web/scripts/buffer/migrate-to-case-sensitive.ts
@@ -34,18 +34,14 @@ async function main() {
},
});
+ console.log(`Found ${links.length} links to migrate...`);
+
if (!links.length) {
console.log("No more links to migrate.");
return;
}
- const remainingLinks = await prisma.link.count({
- where,
- });
-
- console.log(
- `Remaining links to migrate after this batch: ${remainingLinks - 100}`,
- );
+ const linksToDelete: string[] = [];
await Promise.allSettled(
links.map(async (link) => {
@@ -59,23 +55,42 @@ async function main() {
key: newKey,
});
- await prisma.link.update({
- where: {
- id: link.id,
- },
- data: {
- key: newKey,
- shortLink: newShortLink,
- folderId: newFolderId,
- },
- });
+ try {
+ await prisma.link.update({
+ where: {
+ id: link.id,
+ },
+ data: {
+ key: newKey,
+ shortLink: newShortLink,
+ folderId: newFolderId,
+ },
+ });
- console.log(
- `Updated link ${link.id} from ${link.shortLink} to ${newShortLink} and new folder ${newFolderId}`,
- );
+ console.log(
+ `Updated link ${link.id} from ${link.shortLink} to ${newShortLink} and new folder ${newFolderId}`,
+ );
+ } catch (error) {
+ // if the link already exists, delete it
+ if (error.code === "P2002") {
+ console.log(
+ `Link ${link.id} (${link.domain}/${link.key}) already exists, deleting...`,
+ );
+ linksToDelete.push(link.id);
+ }
+ }
}),
);
+ if (linksToDelete.length) {
+ console.log(`Deleting ${linksToDelete.length} links...`);
+ await prisma.link.deleteMany({
+ where: {
+ id: { in: linksToDelete },
+ },
+ });
+ }
+
// expire the Redis cache for the links so it fetches the latest version from the database
await linkCache.expireMany(links);
}