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 schema changes + migration script for multiple link tags

Tim Wilson538c0e8cefb9
diff --git a/apps/web/prisma/schema.prisma b/apps/web/prisma/schema.prisma
index 3b8c1613b1..01825f73b4 100644
--- a/apps/web/prisma/schema.prisma
+++ b/apps/web/prisma/schema.prisma
@@ -225,6 +225,9 @@ model Link {
tag Tag? @relation(fields: [tagId], references: [id])
tagId String?
+ // (Upcoming) multiple link tags
+ tags LinkTag[]
+
// Comments on the particular shortlink
comments String? @db.LongText
@@ -253,11 +256,26 @@ model Tag {
project Project @relation(fields: [projectId], references: [id], onUpdate: Cascade, onDelete: Cascade)
projectId String
links Link[]
+ linksNew LinkTag[]
@@unique([name, projectId])
@@index(projectId)
}
+model LinkTag {
+ id String @id @default(cuid())
+ createdAt DateTime @default(now())
+ updatedAt DateTime @updatedAt
+ link Link @relation(fields: [linkId], references: [id], onUpdate: Cascade, onDelete: Cascade)
+ linkId String
+ tag Tag @relation(fields: [tagId], references: [id], onUpdate: Cascade, onDelete: Cascade)
+ tagId String
+
+ @@unique([linkId, tagId])
+ @@index(linkId)
+ @@index(tagId)
+}
+
model jackson_index {
id Int @id @default(autoincrement())
key String @db.VarChar(250)
diff --git a/apps/web/scripts/sync-link-tags.ts b/apps/web/scripts/sync-link-tags.ts
new file mode 100644
index 0000000000..61f8ba9baa
--- /dev/null
+++ b/apps/web/scripts/sync-link-tags.ts
@@ -0,0 +1,46 @@
+import "dotenv-flow/config";
+import prisma from "@/lib/prisma";
+
+/**
+ * Propagates LinkTag records for links with tagIds
+ */
+async function main() {
+ // Get all links with Tags but without TagLinks
+ const links = await prisma.link.findMany({
+ where: {
+ tagId: {
+ not: null,
+ },
+ tags: {
+ none: {},
+ },
+ },
+ });
+
+ console.log(`Updating ${links.length} links...`);
+
+ const all = await Promise.allSettled(
+ links.map(async (link) => {
+ if (link.tagId == null) return;
+
+ await prisma.link.update({
+ where: {
+ id: link.id,
+ },
+ data: {
+ tags: {
+ create: {
+ tagId: link.tagId,
+ },
+ },
+ },
+ });
+ }),
+ );
+
+ const updated = all.filter(({ status }) => status === "fulfilled");
+
+ console.log(`Updated ${updated.length} / ${links.length} links.`);
+}
+
+main();