CREATE TABLE "filing" ( "id" integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "filing_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1), "ticker" text NOT NULL, "filing_type" text NOT NULL, "filing_date" text NOT NULL, "accession_number" text NOT NULL, "cik" text NOT NULL, "company_name" text NOT NULL, "filing_url" text, "submission_url" text, "primary_document" text, "metrics" jsonb, "analysis" jsonb, "created_at" timestamp with time zone NOT NULL, "updated_at" timestamp with time zone NOT NULL ); --> statement-breakpoint CREATE TABLE "filing_link" ( "id" integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "filing_link_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1), "filing_id" integer NOT NULL, "link_type" text NOT NULL, "url" text NOT NULL, "source" text DEFAULT 'sec' NOT NULL, "created_at" timestamp with time zone NOT NULL ); --> statement-breakpoint CREATE TABLE "holding" ( "id" integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "holding_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1), "user_id" text NOT NULL, "ticker" text NOT NULL, "shares" numeric(30, 6) NOT NULL, "avg_cost" numeric(30, 6) NOT NULL, "current_price" numeric(30, 6), "market_value" numeric(30, 2) NOT NULL, "gain_loss" numeric(30, 2) NOT NULL, "gain_loss_pct" numeric(30, 2) NOT NULL, "last_price_at" timestamp with time zone, "created_at" timestamp with time zone NOT NULL, "updated_at" timestamp with time zone NOT NULL ); --> statement-breakpoint CREATE TABLE "portfolio_insight" ( "id" integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "portfolio_insight_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1), "user_id" text NOT NULL, "provider" text NOT NULL, "model" text NOT NULL, "content" text NOT NULL, "created_at" timestamp with time zone NOT NULL ); --> statement-breakpoint CREATE TABLE "task_run" ( "id" text PRIMARY KEY NOT NULL, "user_id" text NOT NULL, "task_type" text NOT NULL, "status" text NOT NULL, "priority" integer NOT NULL, "payload" jsonb NOT NULL, "result" jsonb, "error" text, "attempts" integer NOT NULL, "max_attempts" integer NOT NULL, "workflow_run_id" text, "created_at" timestamp with time zone NOT NULL, "updated_at" timestamp with time zone NOT NULL, "finished_at" timestamp with time zone ); --> statement-breakpoint CREATE TABLE "watchlist_item" ( "id" integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "watchlist_item_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1), "user_id" text NOT NULL, "ticker" text NOT NULL, "company_name" text NOT NULL, "sector" text, "created_at" timestamp with time zone NOT NULL ); --> statement-breakpoint ALTER TABLE "filing_link" ADD CONSTRAINT "filing_link_filing_id_filing_id_fk" FOREIGN KEY ("filing_id") REFERENCES "public"."filing"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "holding" ADD CONSTRAINT "holding_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "portfolio_insight" ADD CONSTRAINT "portfolio_insight_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "task_run" ADD CONSTRAINT "task_run_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "watchlist_item" ADD CONSTRAINT "watchlist_item_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint CREATE UNIQUE INDEX "filing_accession_uidx" ON "filing" USING btree ("accession_number");--> statement-breakpoint CREATE INDEX "filing_ticker_date_idx" ON "filing" USING btree ("ticker","filing_date");--> statement-breakpoint CREATE INDEX "filing_date_idx" ON "filing" USING btree ("filing_date");--> statement-breakpoint CREATE UNIQUE INDEX "filing_link_unique_uidx" ON "filing_link" USING btree ("filing_id","url");--> statement-breakpoint CREATE INDEX "filing_link_filing_idx" ON "filing_link" USING btree ("filing_id");--> statement-breakpoint CREATE UNIQUE INDEX "holding_user_ticker_uidx" ON "holding" USING btree ("user_id","ticker");--> statement-breakpoint CREATE INDEX "holding_user_idx" ON "holding" USING btree ("user_id");--> statement-breakpoint CREATE INDEX "insight_user_created_idx" ON "portfolio_insight" USING btree ("user_id","created_at");--> statement-breakpoint CREATE INDEX "task_user_created_idx" ON "task_run" USING btree ("user_id","created_at");--> statement-breakpoint CREATE INDEX "task_status_idx" ON "task_run" USING btree ("status");--> statement-breakpoint CREATE UNIQUE INDEX "task_workflow_run_uidx" ON "task_run" USING btree ("workflow_run_id");--> statement-breakpoint CREATE UNIQUE INDEX "watchlist_user_ticker_uidx" ON "watchlist_item" USING btree ("user_id","ticker");--> statement-breakpoint CREATE INDEX "watchlist_user_created_idx" ON "watchlist_item" USING btree ("user_id","created_at");