mirror of
https://github.com/lukevella/rallly.git
synced 2025-05-31 01:36:23 +02:00
♻️ Make user required in subscription model (#1585)
This commit is contained in:
parent
01758f81ae
commit
aebea5a41c
5 changed files with 87 additions and 92 deletions
|
@ -0,0 +1,30 @@
|
|||
-- DropForeignKey
|
||||
ALTER TABLE "users" DROP CONSTRAINT "users_subscription_id_fkey";
|
||||
|
||||
-- DropIndex
|
||||
DROP INDEX "users_subscription_id_key";
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE "subscriptions" ADD COLUMN "user_id" TEXT;
|
||||
|
||||
-- Populate user_id in subscriptions table using data from users table
|
||||
UPDATE "subscriptions" s
|
||||
SET "user_id" = u.id
|
||||
FROM "users" u
|
||||
WHERE u."subscription_id" = s.id;
|
||||
|
||||
-- Delete orphaned subscriptions (subscriptions without a corresponding user)
|
||||
DELETE FROM "subscriptions"
|
||||
WHERE "user_id" IS NULL;
|
||||
|
||||
-- Make user_id NOT NULL after populating data
|
||||
ALTER TABLE "subscriptions" ALTER COLUMN "user_id" SET NOT NULL;
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE "users" DROP COLUMN "subscription_id";
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "subscriptions_user_id_key" ON "subscriptions"("user_id");
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "subscriptions" ADD CONSTRAINT "subscriptions_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
Loading…
Add table
Add a link
Reference in a new issue