Files
buzz-sheet/drizzle/0073_unusual_darkhawk.sql
gunshiz 5812d99715
CI / Verify (push) Successful in 2m30s
CI / Build immutable images and deploy (push) Successful in 3m9s
feat : comment feature
2026-10-08 01:40:08 +07:00

81 lines
6.9 KiB
SQL

CREATE TABLE "guides"."comment_attachment" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"comment_id" uuid NOT NULL,
"object_key" text NOT NULL,
"mime_type" text NOT NULL,
"byte_size" integer NOT NULL,
CONSTRAINT "comment_image_size" CHECK ("guides"."comment_attachment"."byte_size" > 0 and "guides"."comment_attachment"."byte_size" <= 10485760),
CONSTRAINT "comment_image_type" CHECK ("guides"."comment_attachment"."mime_type" in ('image/jpeg', 'image/png', 'image/webp'))
);
--> statement-breakpoint
CREATE TABLE "guides"."comment_reaction" (
"comment_id" uuid NOT NULL,
"user_id" text NOT NULL,
"value" integer NOT NULL,
CONSTRAINT "comment_reaction_comment_id_user_id_pk" PRIMARY KEY("comment_id","user_id"),
CONSTRAINT "comment_reaction_value" CHECK ("guides"."comment_reaction"."value" in (-1, 1))
);
--> statement-breakpoint
CREATE TABLE "guides"."comment_revision_attachment" (
"revision_id" uuid NOT NULL,
"attachment_id" uuid NOT NULL,
"position" integer NOT NULL,
CONSTRAINT "comment_revision_attachment_revision_id_attachment_id_pk" PRIMARY KEY("revision_id","attachment_id"),
CONSTRAINT "comment_attachment_position" CHECK ("guides"."comment_revision_attachment"."position" >= 0 and "guides"."comment_revision_attachment"."position" < 5)
);
--> statement-breakpoint
CREATE TABLE "guides"."comment_revision" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"comment_id" uuid NOT NULL,
"version" integer NOT NULL,
"text" text NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT "comment_text_length" CHECK (length("guides"."comment_revision"."text") <= 4000)
);
--> statement-breakpoint
CREATE TABLE "guides"."comment_thread" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"guide_id" uuid,
"schedule_id" integer,
CONSTRAINT "comment_thread_one_target" CHECK (("guides"."comment_thread"."guide_id" is null) <> ("guides"."comment_thread"."schedule_id" is null))
);
--> statement-breakpoint
CREATE TABLE "guides"."comment" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"thread_id" uuid NOT NULL,
"author_id" text NOT NULL,
"root_id" uuid,
"reply_to_id" uuid,
"version" integer DEFAULT 1 NOT NULL,
"hidden" boolean DEFAULT false NOT NULL,
"deleted_at" timestamp with time zone,
"hearted_by_id" text,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT "comment_reply_shape" CHECK (("guides"."comment"."root_id" is null) = ("guides"."comment"."reply_to_id" is null)),
CONSTRAINT "comment_version_positive" CHECK ("guides"."comment"."version" > 0)
);
--> statement-breakpoint
CREATE UNIQUE INDEX "comment_thread_id_unique" ON "guides"."comment" USING btree ("thread_id","id");--> statement-breakpoint
ALTER TABLE "guides"."comment_attachment" ADD CONSTRAINT "comment_attachment_comment_id_comment_id_fk" FOREIGN KEY ("comment_id") REFERENCES "guides"."comment"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "guides"."comment_reaction" ADD CONSTRAINT "comment_reaction_comment_id_comment_id_fk" FOREIGN KEY ("comment_id") REFERENCES "guides"."comment"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "guides"."comment_reaction" ADD CONSTRAINT "comment_reaction_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "guides"."comment_revision_attachment" ADD CONSTRAINT "comment_revision_attachment_revision_id_comment_revision_id_fk" FOREIGN KEY ("revision_id") REFERENCES "guides"."comment_revision"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "guides"."comment_revision_attachment" ADD CONSTRAINT "comment_revision_attachment_attachment_id_comment_attachment_id_fk" FOREIGN KEY ("attachment_id") REFERENCES "guides"."comment_attachment"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "guides"."comment_revision" ADD CONSTRAINT "comment_revision_comment_id_comment_id_fk" FOREIGN KEY ("comment_id") REFERENCES "guides"."comment"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "guides"."comment_thread" ADD CONSTRAINT "comment_thread_guide_id_guide_id_fk" FOREIGN KEY ("guide_id") REFERENCES "guides"."guide"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "guides"."comment_thread" ADD CONSTRAINT "comment_thread_schedule_id_stygian_schedule_schedule_id_fk" FOREIGN KEY ("schedule_id") REFERENCES "guides"."stygian_schedule"("schedule_id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "guides"."comment" ADD CONSTRAINT "comment_thread_id_comment_thread_id_fk" FOREIGN KEY ("thread_id") REFERENCES "guides"."comment_thread"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "guides"."comment" ADD CONSTRAINT "comment_author_id_user_id_fk" FOREIGN KEY ("author_id") REFERENCES "auth"."user"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "guides"."comment" ADD CONSTRAINT "comment_hearted_by_id_user_id_fk" FOREIGN KEY ("hearted_by_id") REFERENCES "auth"."user"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "guides"."comment" ADD CONSTRAINT "comment_thread_id_root_id_comment_thread_id_id_fk" FOREIGN KEY ("thread_id","root_id") REFERENCES "guides"."comment"("thread_id","id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "guides"."comment" ADD CONSTRAINT "comment_thread_id_reply_to_id_comment_thread_id_id_fk" FOREIGN KEY ("thread_id","reply_to_id") REFERENCES "guides"."comment"("thread_id","id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "comment_attachment_key_unique" ON "guides"."comment_attachment" USING btree ("object_key");--> statement-breakpoint
CREATE INDEX "comment_attachment_comment_idx" ON "guides"."comment_attachment" USING btree ("comment_id");--> statement-breakpoint
CREATE UNIQUE INDEX "comment_revision_attachment_position_unique" ON "guides"."comment_revision_attachment" USING btree ("revision_id","position");--> statement-breakpoint
CREATE UNIQUE INDEX "comment_revision_version_unique" ON "guides"."comment_revision" USING btree ("comment_id","version");--> statement-breakpoint
CREATE UNIQUE INDEX "comment_thread_guide_unique" ON "guides"."comment_thread" USING btree ("guide_id");--> statement-breakpoint
CREATE UNIQUE INDEX "comment_thread_schedule_unique" ON "guides"."comment_thread" USING btree ("schedule_id");--> statement-breakpoint
CREATE INDEX "comment_thread_root_time_idx" ON "guides"."comment" USING btree ("thread_id","root_id","created_at","id");--> statement-breakpoint
CREATE INDEX "comment_root_time_idx" ON "guides"."comment" USING btree ("root_id","created_at","id");--> statement-breakpoint
CREATE INDEX "comment_inbox_time_idx" ON "guides"."comment" USING btree ("created_at","id");