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 domain, createdAt index

Steven Tey5a066c5b566e
diff --git a/apps/web/app/(ee)/api/admin/links/route.ts b/apps/web/app/(ee)/api/admin/links/route.ts
index 0b1d7c86d4..cdf7060920 100644
--- a/apps/web/app/(ee)/api/admin/links/route.ts
+++ b/apps/web/app/(ee)/api/admin/links/route.ts
@@ -26,6 +26,11 @@ export const GET = withAdmin(async ({ searchParams }) => {
in: DUB_DOMAINS_ARRAY,
},
}),
+ ...(!search && {
+ createdAt: {
+ gte: new Date(Date.now() - 1000 * 60 * 60 * 24 * 30), // 30 days ago
+ },
+ }),
OR: [
{
userId: {
diff --git a/apps/web/app/(ee)/api/cron/domains/delete/route.ts b/apps/web/app/(ee)/api/cron/domains/delete/route.ts
index 0484b5fb76..33954d330d 100644
--- a/apps/web/app/(ee)/api/cron/domains/delete/route.ts
+++ b/apps/web/app/(ee)/api/cron/domains/delete/route.ts
@@ -1,6 +1,6 @@
-import { queueDomainDeletion } from "@/lib/api/domains/queue";
import { handleAndReturnErrorResponse } from "@/lib/api/errors";
import { linkCache } from "@/lib/api/links/cache";
+import { limiter } from "@/lib/cron/limiter";
import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
import { storage } from "@/lib/storage";
import { recordLink } from "@/lib/tinybird/record-link";
@@ -43,9 +43,14 @@ export async function POST(req: Request) {
},
},
},
- take: 100, // TODO: We can adjust this number based on the performance
+ take: 100,
+ orderBy: {
+ createdAt: "desc",
+ },
});
+ console.log(`Found ${links.length} links to delete`);
+
if (links.length === 0) {
return new Response("No more links to delete. Exiting...");
}
@@ -55,13 +60,16 @@ export async function POST(req: Request) {
linkCache.deleteMany(links),
// Record link in Tinybird
- // TODO: Maybe we can just delete these links instead?
recordLink(links),
// Remove image from R2 storage if it exists
links
.filter((link) => link.image?.startsWith(`${R2_URL}/images/${link.id}`))
- .map((link) => storage.delete(link.image!.replace(`${R2_URL}/`, ""))),
+ .map((link) =>
+ limiter.schedule(() =>
+ storage.delete(link.image!.replace(`${R2_URL}/`, "")),
+ ),
+ ),
// Remove the link from MySQL
prisma.link.deleteMany({
@@ -101,26 +109,26 @@ export async function POST(req: Request) {
console.log("remainingLinks", remainingLinks);
- if (remainingLinks > 0) {
- await queueDomainDeletion({
- domain,
- delay: 2,
- });
- return new Response(
- `Deleted ${links.length} links, ${remainingLinks} remaining. Starting next batch...`,
- );
- }
-
- // After all links are deleted, delete the domain and image
- await Promise.all([
- prisma.domain.delete({
- where: {
- slug: domain,
- },
- }),
- domainRecord.logo &&
- storage.delete(domainRecord.logo.replace(`${R2_URL}/`, "")),
- ]);
+ // if (remainingLinks > 0) {
+ // await queueDomainDeletion({
+ // domain,
+ // delay: 2,
+ // });
+ // return new Response(
+ // `Deleted ${links.length} links, ${remainingLinks} remaining. Starting next batch...`,
+ // );
+ // }
+
+ // // After all links are deleted, delete the domain and image
+ // await Promise.all([
+ // prisma.domain.delete({
+ // where: {
+ // slug: domain,
+ // },
+ // }),
+ // domainRecord.logo &&
+ // storage.delete(domainRecord.logo.replace(`${R2_URL}/`, "")),
+ // ]);
return new Response(
`Deleted ${links.length} links, no more links remaining. Domain deleted.`,
diff --git a/apps/web/app/(ee)/api/cron/domains/transfer/route.ts b/apps/web/app/(ee)/api/cron/domains/transfer/route.ts
index 25d0fa3aae..e9586783e1 100644
--- a/apps/web/app/(ee)/api/cron/domains/transfer/route.ts
+++ b/apps/web/app/(ee)/api/cron/domains/transfer/route.ts
@@ -29,6 +29,9 @@ export async function POST(req: Request) {
const links = await prisma.link.findMany({
where: { domain, projectId: currentWorkspaceId },
take: 100,
+ orderBy: {
+ createdAt: "desc",
+ },
});
// No remaining links to transfer
diff --git a/apps/web/app/(ee)/api/cron/domains/update/route.ts b/apps/web/app/(ee)/api/cron/domains/update/route.ts
index 94a41912cf..61707c9740 100644
--- a/apps/web/app/(ee)/api/cron/domains/update/route.ts
+++ b/apps/web/app/(ee)/api/cron/domains/update/route.ts
@@ -38,6 +38,9 @@ export async function POST(req: Request) {
domain: oldDomain,
},
take: 100,
+ orderBy: {
+ createdAt: "desc",
+ },
});
if (linksToUpdate.length === 0) {
diff --git a/apps/web/app/(ee)/api/cron/folders/delete/route.ts b/apps/web/app/(ee)/api/cron/folders/delete/route.ts
index c628cf94d6..2785db5432 100644
--- a/apps/web/app/(ee)/api/cron/folders/delete/route.ts
+++ b/apps/web/app/(ee)/api/cron/folders/delete/route.ts
@@ -28,6 +28,9 @@ export async function POST(req: Request) {
folderId,
},
take: MAX_LINKS_PER_BATCH,
+ orderBy: {
+ createdAt: "desc", // TODO we need to add [folderId, createdAt] index on Link table
+ },
include: includeTags,
});
diff --git a/apps/web/app/api/domains/route.ts b/apps/web/app/api/domains/route.ts
index 307234994b..69321377c6 100644
--- a/apps/web/app/api/domains/route.ts
+++ b/apps/web/app/api/domains/route.ts
@@ -41,12 +41,12 @@ export const GET = withWorkspace(
const links = includeLink
? await prisma.link.findMany({
where: {
- key: {
- in: ["_root", "akoJCU0="],
- },
domain: {
in: domains.map((domain) => domain.slug),
},
+ key: {
+ in: ["_root", "akoJCU0="],
+ },
},
include: {
tags: {
diff --git a/packages/prisma/schema/link.prisma b/packages/prisma/schema/link.prisma
index a19c4a54d1..b7f3a9613f 100644
--- a/packages/prisma/schema/link.prisma
+++ b/packages/prisma/schema/link.prisma
@@ -88,6 +88,7 @@ model Link {
@@index([projectId, url(length: 500)]) // for upserting a link by URL
@@index([projectId, folderId, archived, createdAt(sort: Desc)]) // most getLinksForWorkspace queries
@@index([programId, partnerId]) // for getting a referral link (programId + partnerId)
+ @@index([domain, createdAt]) // for bulk link deletion workflows (by domain)
@@index(folderId) // used in /api/folders
- @@index(userId) // for relation to User table
+ @@index(userId) // for relation to User table, used in /api/cron/cleanup too
}