-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0001_nostalgic_master_mold.sql
More file actions
41 lines (33 loc) · 1.39 KB
/
0001_nostalgic_master_mold.sql
File metadata and controls
41 lines (33 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
CREATE EXTENSION IF NOT EXISTS pgcrypto;
CREATE TABLE "account_types" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"name" varchar(100) NOT NULL,
"slug" varchar(120) NOT NULL
);
--> statement-breakpoint
-- 1) Add column nullable
ALTER TABLE "accounts" ADD COLUMN "typeId" uuid;--> statement-breakpoint
-- 2) (Optional but recommended) Ensure a default account type exists for backfilling
INSERT INTO "account_types" ("name", "slug")
SELECT 'Private', 'private'
ON CONFLICT ("slug") DO NOTHING;--> statement-breakpoint
-- 3) Backfill existing rows (choose the appropriate default type)
UPDATE "accounts" a
SET "typeId" = at.id
FROM "account_types" at
WHERE a."typeId" IS NULL
AND at."slug" = 'private';--> statement-breakpoint
-- 4) Enforce NOT NULL
ALTER TABLE "accounts" ALTER COLUMN "typeId" SET NOT NULL;--> statement-breakpoint
-- 5) Keep the unique index on account_types.slug
CREATE UNIQUE INDEX "account_types_slug_unique_idx"
ON "account_types" USING btree ("slug");--> statement-breakpoint
-- 6) Add the foreign key constraint
ALTER TABLE "accounts"
ADD CONSTRAINT "accounts_typeId_account_types_id_fk"
FOREIGN KEY ("typeId")
REFERENCES "public"."account_types"("id")
ON DELETE RESTRICT
ON UPDATE NO ACTION;--> statement-breakpoint
-- 7) Add an index on accounts.typeId for performance
CREATE INDEX "accounts_type_idx" ON "accounts" USING btree ("typeId");