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;