Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627CREATE TABLE "conversations" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "user_a_id" uuid NOT NULL, "user_b_id" uuid NOT NULL, "last_message_at" timestamp with time zone, "user_a_last_read_at" timestamp with time zone, "user_b_last_read_at" timestamp with time zone, "created_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "conversations_pair_ordered" CHECK ("conversations"."user_a_id" < "conversations"."user_b_id"));--> statement-breakpointCREATE TABLE "messages" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "conversation_id" uuid NOT NULL, "sender_id" uuid NOT NULL, "text" text NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL);--> statement-breakpointALTER TABLE "conversations" ADD CONSTRAINT "conversations_user_a_id_users_id_fk" FOREIGN KEY ("user_a_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpointALTER TABLE "conversations" ADD CONSTRAINT "conversations_user_b_id_users_id_fk" FOREIGN KEY ("user_b_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpointALTER TABLE "messages" ADD CONSTRAINT "messages_conversation_id_conversations_id_fk" FOREIGN KEY ("conversation_id") REFERENCES "public"."conversations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpointALTER TABLE "messages" ADD CONSTRAINT "messages_sender_id_users_id_fk" FOREIGN KEY ("sender_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpointCREATE UNIQUE INDEX "conversations_pair_idx" ON "conversations" USING btree ("user_a_id","user_b_id");--> statement-breakpointCREATE INDEX "conversations_user_b_id_idx" ON "conversations" USING btree ("user_b_id");--> statement-breakpointCREATE INDEX "messages_conversation_created_idx" ON "messages" USING btree ("conversation_id","created_at");--> statement-breakpointCREATE INDEX "messages_sender_id_idx" ON "messages" USING btree ("sender_id");