Files
electro4ort/apps/api/drizzle/20261201220558_friends_and_conversations/migration.sql
T
olehhapuk 98b2e0ce4b
CI / Lint, types, build, tests (push) Canceled after 0s
CI / Docker images build (push) Canceled after 0s
update
2026-12-03 10:53:09 +02:00

46 lines
2.9 KiB
SQL

CREATE TYPE "conversation_type" AS ENUM('direct');--> statement-breakpoint
CREATE TYPE "friendship_status" AS ENUM('pending', 'accepted');--> statement-breakpoint
CREATE TABLE "conversation" (
"id" uuid PRIMARY KEY DEFAULT uuidv7(),
"type" "conversation_type" NOT NULL,
"direct_key" text UNIQUE,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"last_activity_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "conversation_member" (
"conversation_id" uuid,
"user_id" text,
"joined_at" timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT "conversation_member_pkey" PRIMARY KEY("conversation_id","user_id")
);
--> statement-breakpoint
CREATE TABLE "message" (
"id" uuid PRIMARY KEY DEFAULT uuidv7(),
"conversation_id" uuid NOT NULL,
"author_id" text NOT NULL,
"content" text NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "friendship" (
"user_low_id" text,
"user_high_id" text,
"requested_by_id" text NOT NULL,
"status" "friendship_status" DEFAULT 'pending'::"friendship_status" NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"accepted_at" timestamp with time zone,
CONSTRAINT "friendship_pkey" PRIMARY KEY("user_low_id","user_high_id"),
CONSTRAINT "friendship_ordered_pair" CHECK ("user_low_id" < "user_high_id" COLLATE "C")
);
--> statement-breakpoint
CREATE INDEX "conversation_member_user_id_idx" ON "conversation_member" ("user_id");--> statement-breakpoint
CREATE INDEX "message_conversation_id_id_idx" ON "message" ("conversation_id","id");--> statement-breakpoint
CREATE INDEX "friendship_user_high_id_idx" ON "friendship" ("user_high_id");--> statement-breakpoint
ALTER TABLE "conversation_member" ADD CONSTRAINT "conversation_member_conversation_id_conversation_id_fkey" FOREIGN KEY ("conversation_id") REFERENCES "conversation"("id") ON DELETE CASCADE;--> statement-breakpoint
ALTER TABLE "conversation_member" ADD CONSTRAINT "conversation_member_user_id_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "user"("id") ON DELETE CASCADE;--> statement-breakpoint
ALTER TABLE "message" ADD CONSTRAINT "message_conversation_id_conversation_id_fkey" FOREIGN KEY ("conversation_id") REFERENCES "conversation"("id") ON DELETE CASCADE;--> statement-breakpoint
ALTER TABLE "message" ADD CONSTRAINT "message_author_id_user_id_fkey" FOREIGN KEY ("author_id") REFERENCES "user"("id") ON DELETE CASCADE;--> statement-breakpoint
ALTER TABLE "friendship" ADD CONSTRAINT "friendship_user_low_id_user_id_fkey" FOREIGN KEY ("user_low_id") REFERENCES "user"("id") ON DELETE CASCADE;--> statement-breakpoint
ALTER TABLE "friendship" ADD CONSTRAINT "friendship_user_high_id_user_id_fkey" FOREIGN KEY ("user_high_id") REFERENCES "user"("id") ON DELETE CASCADE;--> statement-breakpoint
ALTER TABLE "friendship" ADD CONSTRAINT "friendship_requested_by_id_user_id_fkey" FOREIGN KEY ("requested_by_id") REFERENCES "user"("id") ON DELETE CASCADE;