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.

remove old defaultForProgram / defaultDiscountId columns

Steven Tey82abebc3c4e0
diff --git a/apps/web/scripts/migrate-discounts.ts b/apps/web/scripts/migrate-discounts.ts
index cd62295445..4b757f96ae 100644
--- a/apps/web/scripts/migrate-discounts.ts
+++ b/apps/web/scripts/migrate-discounts.ts
@@ -1,4 +1,4 @@
-// this is a one-time migration script for
+// @ts-nocheck – this is a one-time migration script for
// when we migrate the program-wide discounts to the new schema
import { prisma } from "@dub/prisma";
@@ -17,7 +17,6 @@ async function main() {
},
},
});
- console.table(nonDefaultDiscounts, ["id", "programId", "_count"]);
console.log(
`Found ${nonDefaultDiscounts.reduce(
(acc, discount) => acc + discount._count.programEnrollments,
@@ -26,7 +25,7 @@ async function main() {
);
// Migrate program-wide discounts
- const programDiscounts = await prisma.discount.findMany({
+ const programDiscount = await prisma.discount.findFirst({
where: {
defaultForProgram: {
isNot: null,
@@ -39,44 +38,55 @@ async function main() {
},
});
- const finalProgramDiscounts = programDiscounts.map((discount) => {
- return {
- discountId: discount.id,
- programId: discount.programId,
- };
- });
+ console.log({ programDiscount });
- console.table(finalProgramDiscounts);
+ if (!programDiscount) {
+ console.log("No program discounts to migrate");
+ return;
+ }
- // 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,
- },
- });
+ const { id: discountId, programId } = programDiscount;
- console.log({ res1 });
+ while (true) {
+ const programEnrollmentsToUpdate = await prisma.programEnrollment.findMany({
+ where: {
+ programId,
+ discountId: null, // only update if the discountId is null
+ },
+ take: 500,
+ });
- for (const discount of finalProgramDiscounts) {
- const discountId = discount.discountId;
+ if (programEnrollmentsToUpdate.length === 0) {
+ break;
+ }
- const res2 = await prisma.programEnrollment.updateMany({
+ const data = await prisma.programEnrollment.updateMany({
where: {
- programId: discount.programId,
- discountId: null, // only update if the discountId is null
+ id: {
+ in: programEnrollmentsToUpdate.map((enrollment) => enrollment.id),
+ },
},
data: {
discountId,
},
});
- console.log({ res2 });
+ console.log(`Updated ${data.count} program enrollments`);
}
+
+ // Update the default column in the Reward table
+ await prisma.discount.update({
+ where: {
+ id: programDiscount.id,
+ },
+ data: {
+ default: true,
+ },
+ });
+
+ console.log(
+ `Updated program-wide discount ${programDiscount.id} to use default: true`,
+ );
}
main();
diff --git a/packages/prisma/schema/discount.prisma b/packages/prisma/schema/discount.prisma
index a43e11064d..c0b38b896e 100644
--- a/packages/prisma/schema/discount.prisma
+++ b/packages/prisma/schema/discount.prisma
@@ -14,8 +14,5 @@ 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 6d77ce9312..5eb13a6704 100644
--- a/packages/prisma/schema/program.prisma
+++ b/packages/prisma/schema/program.prisma
@@ -37,10 +37,6 @@ 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