The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
stash
Steven Teyde2d983ba1cb
diff --git a/apps/web/app/api/cron/migrate/route.ts b/apps/web/app/api/cron/migrate/route.tsindex e3bb9e1030..399033737c 100644--- a/apps/web/app/api/cron/migrate/route.ts+++ b/apps/web/app/api/cron/migrate/route.ts@@ -1,11 +1,7 @@import { handleAndReturnErrorResponse } from "@/lib/api/errors";-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 { prisma } from "@dub/prisma";import { Prisma } from "@dub/prisma/client";-import { APP_DOMAIN_WITH_NGROK, linkConstructorSimple } from "@dub/utils";import { NextResponse } from "next/server";export const dynamic = "force-dynamic";@@ -14,11 +10,6 @@ export const dynamic = "force-dynamic";One off cron to migrate*/export async function POST(req: Request) {- const domain = "buff.ly";- const userId = "user_EzRuKzR9sG3WmHapVV6aEec7";- const oldFolderId = "fold_LIZsdjTgFVbQVGYSUmYAi5vT";- const newFolderId = "fold_1JNQBVZV8P0NA0YGB11W2HHSQ";-try {await verifyQstashSignature({req,@@ -29,11 +20,11 @@ export async function POST(req: Request) {}const where: Prisma.LinkWhereInput = {- userId,- domain,- folderId: oldFolderId,+ projectId: "cm05wnnpo000711ztj05wwdbu",+ archived: false,+ folderId: "fold_LIZsdjTgFVbQVGYSUmYAi5vT",createdAt: {- lte: new Date("2025-03-07T16:33:32.084Z"),+ lte: new Date("2025-02-27T00:00:00.000Z"),},};@@ -41,83 +32,44 @@ export async function POST(req: Request) {where,select: {id: true,- domain: true,key: true,- shortLink: true,+ createdAt: true,},- take: 100,+ take: 1000,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("No more links to delete.");+ return NextResponse.json({+ status: "No more links to delete.",+ });}- const linksToDelete: string[] = [];-- await Promise.allSettled(- links.map(async (link) => {- const newKey = encodeKeyIfCaseSensitive({- domain,- key: link.key,- });-- const newShortLink = linkConstructorSimple({- domain,- key: newKey,- });-- 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}`,- );- } 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);- }- }- }),- );+ console.table(links);- if (linksToDelete.length) {- console.log(`Deleting ${linksToDelete.length} links...`);- await prisma.link.deleteMany({- where: {- id: { in: linksToDelete },+ await prisma.link.deleteMany({+ where: {+ id: {+ in: links.map((link) => link.id),},- });- }+ },+ });- // expire the Redis cache for the links so it fetches the latest version from the database- await linkCache.expireMany(links);+ console.log(`Deleted ${links.length} links`);+ console.log(+ `Final deleted link timestamp: ${new Date(links[links.length - 1].createdAt).toISOString()}`,+ );- await qstash.publishJSON({- url: `${APP_DOMAIN_WITH_NGROK}/api/cron/migrate`,- method: "POST",- body: {},- });+ // await qstash.publishJSON({+ // url: `${APP_DOMAIN_WITH_NGROK}/api/cron/migrate`,+ // method: "POST",+ // body: {},+ // });return NextResponse.json({- status: `Migrated ${links.length} links and deleted ${linksToDelete.length} links.`,+ status: `Deleted ${links.length} links. Final deleted link timestamp: ${new Date(links[links.length - 1].createdAt).toISOString()}`,});}diff --git a/apps/web/scripts/buffer/delete-old-links.ts b/apps/web/scripts/buffer/delete-old-links.tsindex 4ca9c89b7b..74acd31c74 100644--- a/apps/web/scripts/buffer/delete-old-links.ts+++ b/apps/web/scripts/buffer/delete-old-links.ts@@ -4,41 +4,33 @@ import "dotenv-flow/config";async function main() {const where: Prisma.LinkWhereInput = {- projectId: "xxx",+ projectId: "cm05wnnpo000711ztj05wwdbu",archived: false,- folderId: "xxx",- AND: [- {- createdAt: {- lte: new Date("2025-02-15T15:27:00.000Z"),- },- },- {- createdAt: {- gte: new Date("2025-01-29T14:27:00.000Z"),- },- },- ],+ folderId: "fold_LIZsdjTgFVbQVGYSUmYAi5vT",+ createdAt: {+ lte: new Date("2025-02-27T00:00:00.000Z"),+ },};const links = await prisma.link.findMany({where,select: {id: true,- shortLink: true,+ key: true,+ createdAt: true,},take: 1000,orderBy: {- createdAt: "asc",+ createdAt: "desc",},});if (!links.length) {- console.log("No more links to migrate.");+ console.log("No more links to delete.");return;}- console.table(links);+ // console.table(links);await prisma.link.deleteMany({where: {@@ -49,6 +41,10 @@ async function main() {});console.log(`Deleted ${links.length} links`);+ const finalDeletedLink = links[links.length - 1];+ console.log(+ `Final deleted link: ${finalDeletedLink.key} (${new Date(finalDeletedLink.createdAt).toISOString()})`,+ );}main();