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 backfill script for Commission.source=user

Kiran K57dd36d5450d
diff --git a/apps/web/scripts/migrations/backfill-commission-source.ts b/apps/web/scripts/migrations/backfill-commission-source.ts
new file mode 100644
index 0000000000..9efe9e1c98
--- /dev/null
+++ b/apps/web/scripts/migrations/backfill-commission-source.ts
@@ -0,0 +1,62 @@
+import { prisma } from "@/lib/prisma";
+import { CommissionSource } from "@prisma/client";
+import "dotenv-flow/config";
+
+const BATCH_SIZE = 500;
+
+// Backfill Commission.source = user when:
+// - userId is set (manual / dashboard creates), or
+// - the commission is linked from a BountySubmission
+async function main() {
+ let totalUpdated = 0;
+
+ while (true) {
+ const commissions = await prisma.commission.findMany({
+ where: {
+ source: null,
+ OR: [
+ {
+ userId: {
+ not: null,
+ },
+ },
+ {
+ bountySubmission: {
+ isNot: null,
+ },
+ },
+ ],
+ },
+ select: {
+ id: true,
+ },
+ take: BATCH_SIZE,
+ orderBy: {
+ id: "asc",
+ },
+ });
+
+ if (commissions.length === 0) {
+ break;
+ }
+
+ const { count } = await prisma.commission.updateMany({
+ where: {
+ id: {
+ in: commissions.map(({ id }) => id),
+ },
+ source: null,
+ },
+ data: {
+ source: CommissionSource.user,
+ },
+ });
+
+ totalUpdated += count;
+ console.log(`Updated ${count} commissions (total: ${totalUpdated})`);
+ }
+
+ console.log(`Done. Set source=user on ${totalUpdated} commissions.`);
+}
+
+main();