@@ -11,11 +11,31 @@ CREATE TYPE "PaymentStatus" AS ENUM ('created', 'authorized', 'captured', 'refun
1111-- CreateEnum
1212CREATE TYPE "SubscriptionStatus " AS ENUM (' created' , ' authenticated' , ' active' , ' pending' , ' halted' , ' cancelled' , ' completed' , ' expired' );
1313
14- -- AlterTable
15- ALTER TABLE " Payment" DROP COLUMN " status" ,
16- ADD COLUMN " status" " PaymentStatus" NOT NULL ;
17-
18- -- AlterTable
19- ALTER TABLE " Subscription" DROP COLUMN " status" ,
20- ADD COLUMN " status" " SubscriptionStatus" NOT NULL ,
21- ALTER COLUMN " endDate" DROP NOT NULL ;
14+ -- AlterTable: Migrate Payment.status from TEXT to PaymentStatus enum
15+ -- First, temporarily drop NOT NULL constraint to allow safe conversion
16+ ALTER TABLE " Payment" ALTER COLUMN " status" DROP NOT NULL ;
17+
18+ -- Convert the column type preserving existing data
19+ ALTER TABLE " Payment" ALTER COLUMN " status" TYPE " PaymentStatus" USING status::" PaymentStatus" ;
20+
21+ -- Backfill any NULLs with a default value if needed (shouldn't be necessary but being safe)
22+ -- If there are NULLs, we'd set a default here, but since original was NOT NULL, this shouldn't be needed
23+
24+ -- Re-add NOT NULL constraint after successful conversion
25+ ALTER TABLE " Payment" ALTER COLUMN " status" SET NOT NULL ;
26+
27+ -- AlterTable: Migrate Subscription.status from TEXT to SubscriptionStatus enum
28+ -- First, temporarily drop NOT NULL constraint to allow safe conversion
29+ ALTER TABLE " Subscription" ALTER COLUMN " status" DROP NOT NULL ;
30+
31+ -- Convert the column type preserving existing data
32+ ALTER TABLE " Subscription" ALTER COLUMN " status" TYPE " SubscriptionStatus" USING status::" SubscriptionStatus" ;
33+
34+ -- Backfill any NULLs with a default value if needed (shouldn't be necessary but being safe)
35+ -- If there are NULLs, we'd set a default here, but since original was NOT NULL, this shouldn't be needed
36+
37+ -- Re-add NOT NULL constraint after successful conversion
38+ ALTER TABLE " Subscription" ALTER COLUMN " status" SET NOT NULL ;
39+
40+ -- AlterTable: Make Subscription.endDate nullable
41+ ALTER TABLE " Subscription" ALTER COLUMN " endDate" DROP NOT NULL ;
0 commit comments