Skip to content

Commit 1b55e07

Browse files
App: 🗃️ Add migration to give all users a subscription (#2788)
1 parent 1574af2 commit 1b55e07

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
-- AlterTable
2+
ALTER TABLE "Subscription" ALTER COLUMN "status" SET DEFAULT 'FREE_TRIAL';
3+
4+
DO
5+
$$
6+
DECLARE
7+
subscriptionId TEXT;
8+
userRecord RECORD;
9+
BEGIN
10+
-- Loop through User records with null Subscription relation
11+
FOR userRecord IN SELECT * FROM "User" WHERE "subscriptionId" IS NULL LOOP
12+
-- Generate cuid for the new Subscription record
13+
subscriptionId := uuid_generate_v4()::TEXT;
14+
15+
-- Insert a new Subscription record with cuid value, current timestamp for updatedAt, and link to the user by userId
16+
INSERT INTO "Subscription" (id, "status", "type", "updatedAt", "userId")
17+
VALUES (subscriptionId, 'IN_TRIAL', 'FREE', NOW(), userRecord.id);
18+
19+
-- Update User record to link to the newly created Subscription record
20+
UPDATE "User"
21+
SET "subscriptionId" = subscriptionId
22+
WHERE "id" = userRecord.id;
23+
END LOOP;
24+
END
25+
$$;

app/api/db/schema.prisma

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ model Subscription {
190190
startDate DateTime?
191191
updatedAt DateTime @updatedAt
192192
trailEnds DateTime?
193-
status SubscriptionStatus
193+
status SubscriptionStatus @default(FREE_TRIAL)
194194
type String
195195
data Json?
196196
userId String

0 commit comments

Comments
 (0)