Skip to content

Commit 57a0189

Browse files
committed
fix(db): split referral_legacy migration to handle PostgreSQL enum limitation
PostgreSQL ADD VALUE for enums is not visible within the same transaction, so the UPDATE statements need to run in a separate migration after the enum value is committed. - 0039: Add referral_legacy enum value + is_legacy column (DEFAULT true) - 0040: Backfill credit_ledger with referral_legacy type
1 parent 4b8b2e9 commit 57a0189

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed
Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,2 @@
11
ALTER TYPE "public"."grant_type" ADD VALUE 'referral_legacy' BEFORE 'purchase';--> statement-breakpoint
2-
ALTER TABLE "referral" ADD COLUMN "is_legacy" boolean DEFAULT false NOT NULL;--> statement-breakpoint
3-
-- Backfill: Mark all existing referrals as legacy (they were created under the old recurring program)
4-
UPDATE "referral" SET "is_legacy" = true;--> statement-breakpoint
5-
-- Migrate existing referral grants that have an expiry date to referral_legacy type
6-
-- (These are the recurring grants from the old program)
7-
UPDATE "credit_ledger"
8-
SET "type" = 'referral_legacy',
9-
"priority" = 30
10-
WHERE "type" = 'referral'
11-
AND "expires_at" IS NOT NULL;--> statement-breakpoint
12-
-- Update priority for remaining referral grants (one-time grants, if any exist) to new priority
13-
UPDATE "credit_ledger"
14-
SET "priority" = 50
15-
WHERE "type" = 'referral'
16-
AND "expires_at" IS NULL;
2+
ALTER TABLE "referral" ADD COLUMN "is_legacy" boolean DEFAULT true NOT NULL;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- Migrate existing referral grants that have an expiry date to referral_legacy type
2+
-- (These are the recurring grants from the old program)
3+
UPDATE "credit_ledger"
4+
SET "type" = 'referral_legacy',
5+
"priority" = 30
6+
WHERE "type" = 'referral'
7+
AND "expires_at" IS NOT NULL;--> statement-breakpoint
8+
-- Update priority for remaining referral grants (one-time grants, if any exist) to new priority
9+
UPDATE "credit_ledger"
10+
SET "priority" = 50
11+
WHERE "type" = 'referral'
12+
AND "expires_at" IS NULL;

packages/internal/src/db/migrations/meta/_journal.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,13 @@
281281
"when": 1769482939158,
282282
"tag": "0039_quiet_franklin_storm",
283283
"breakpoints": true
284+
},
285+
{
286+
"idx": 40,
287+
"version": "7",
288+
"when": 1769650000000,
289+
"tag": "0040_referral_legacy_backfill",
290+
"breakpoints": true
284291
}
285292
]
286293
}

0 commit comments

Comments
 (0)