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 script

Steven Teyda34d97c7618
diff --git a/apps/web/scripts/migrate-discounts.ts b/apps/web/scripts/migrate-discounts.ts
index e7db9b3efa..d33954f277 100644
--- a/apps/web/scripts/migrate-discounts.ts
+++ b/apps/web/scripts/migrate-discounts.ts
@@ -1,33 +1,62 @@
-// @ts-nocheck contains old fields that are not used anymore
-
import { prisma } from "@dub/prisma";
import "dotenv-flow/config";
+/*
+One time script to migrate the program-wide discounts to the new schema.
+*/
+
async function main() {
- const discounts = await prisma.discount.findMany({
+ // Migrate program-wide discounts
+ const programDiscounts = await prisma.discount.findMany({
+ where: {
+ defaultForProgram: {
+ isNot: null,
+ },
+ default: false,
+ },
select: {
id: true,
- duration: true,
- interval: true,
+ programId: true,
+ },
+ });
+ console.log({ programDiscounts });
+
+ const finalProgramDiscounts = programDiscounts.map((discount) => {
+ return {
+ discountId: discount.id,
+ programId: discount.programId,
+ };
+ });
+
+ console.table(finalProgramDiscounts);
+
+ // Update the default column in the Reward table
+ const res1 = await prisma.discount.updateMany({
+ where: {
+ id: {
+ in: finalProgramDiscounts.map((discount) => discount.discountId),
+ },
+ },
+ data: {
+ default: true,
},
});
- // Move the duration + interval into maxDuration
- for (const discount of discounts) {
- const maxDuration = discount.duration
- ? discount.interval === "month"
- ? discount.duration
- : discount.duration * 12
- : null;
+ console.log({ res1 });
+
+ for (const discount of finalProgramDiscounts) {
+ const discountId = discount.discountId;
- await prisma.discount.update({
+ const res2 = await prisma.programEnrollment.updateMany({
where: {
- id: discount.id,
+ programId: discount.programId,
},
data: {
- maxDuration,
+ discountId,
},
});
+
+ console.log({ res2 });
}
}
diff --git a/packages/prisma/schema/discount.prisma b/packages/prisma/schema/discount.prisma
index 9ebfd55756..ffdf163b2d 100644
--- a/packages/prisma/schema/discount.prisma
+++ b/packages/prisma/schema/discount.prisma
@@ -18,5 +18,8 @@ model Discount {
programEnrollments ProgramEnrollment[]
program Program @relation("ProgramDiscounts", fields: [programId], references: [id], onDelete: Cascade, onUpdate: Cascade)
+ // TODO: remove after https://github.com/dubinc/dub/pull/2611 + https://github.com/dubinc/dub/pull/2595 merges
+ defaultForProgram Program? @relation("ProgramDefaultDiscount")
+
@@index(programId)
}
diff --git a/packages/prisma/schema/program.prisma b/packages/prisma/schema/program.prisma
index 3925617e48..6d77ce9312 100644
--- a/packages/prisma/schema/program.prisma
+++ b/packages/prisma/schema/program.prisma
@@ -37,7 +37,11 @@ model Program {
cookieLength Int @default(90)
holdingPeriodDays Int @default(0) // number of days to wait before earnings are added to a payout
minPayoutAmount Int @default(10000) // Default minimum payout amount of $100
+
+ // TODO: remove after https://github.com/dubinc/dub/pull/2611 + https://github.com/dubinc/dub/pull/2595 merges
defaultDiscountId String? @unique // default discount for the program
+ defaultDiscount Discount? @relation("ProgramDefaultDiscount", fields: [defaultDiscountId], references: [id], onDelete: NoAction, onUpdate: NoAction)
+
embedData Json? @db.Json
landerData Json? @db.Json
landerPublishedAt DateTime?