CREATE TABLE "user_portfolio_config" (
	"id" bigserial PRIMARY KEY NOT NULL,
	"user_id" bigint NOT NULL,
	"portfolio_id" bigint NOT NULL,
	"starred_at" timestamp (6) with time zone,
	"folder_id" bigint,
	"created_at" timestamp (6) with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp (6) with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "user_portfolio_config_user_portfolio_unique" UNIQUE("user_id","portfolio_id")
);
--> statement-breakpoint
CREATE TABLE "user_portfolio_folders" (
	"id" bigserial PRIMARY KEY NOT NULL,
	"user_id" bigint NOT NULL,
	"name" text NOT NULL,
	"position" integer NOT NULL,
	"created_at" timestamp (6) with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp (6) with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "user_portfolio_folders_id_user_unique" UNIQUE("id","user_id")
);
--> statement-breakpoint
ALTER TABLE "user_portfolio_config" ADD CONSTRAINT "user_portfolio_config_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "user_portfolio_config" ADD CONSTRAINT "user_portfolio_config_portfolio_id_portfolio_id_fk" FOREIGN KEY ("portfolio_id") REFERENCES "public"."portfolio"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "user_portfolio_config" ADD CONSTRAINT "user_portfolio_config_folder_owner_fk" FOREIGN KEY ("folder_id","user_id") REFERENCES "public"."user_portfolio_folders"("id","user_id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "user_portfolio_folders" ADD CONSTRAINT "user_portfolio_folders_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "idx_user_portfolio_config_folder" ON "user_portfolio_config" USING btree ("folder_id");--> statement-breakpoint
CREATE INDEX "idx_user_portfolio_folders_user_position" ON "user_portfolio_folders" USING btree ("user_id","position");