CREATE TABLE "fact_events" ( "id" bigserial PRIMARY KEY NOT NULL, "round_id" bigint NOT NULL, "kind" text NOT NULL, "symbol" text, "payload" jsonb, "source" text, "at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "round_decisions" ( "id" bigserial PRIMARY KEY NOT NULL, "round_id" bigint NOT NULL, "intent_id" bigint, "symbol" text NOT NULL, "side" text NOT NULL, "qty" numeric, "order_type" text, "expected_price" numeric, "status" text DEFAULT 'intended' NOT NULL, "reason" text, "reason_detail" text, "superseded_by_decision_id" bigint, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "round_intents" ( "id" bigserial PRIMARY KEY NOT NULL, "round_id" bigint NOT NULL, "version" bigint NOT NULL, "supersedes_intent_id" bigint, "target_portfolio" jsonb, "raw_strategy_output" jsonb, "reason" text, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "round_orders" ( "id" bigserial PRIMARY KEY NOT NULL, "decision_id" bigint NOT NULL, "round_id" bigint NOT NULL, "alpaca_order_id" text, "client_order_id" text, "qty_intended" numeric, "qty_filled" numeric DEFAULT '0' NOT NULL, "avg_fill_price" numeric, "status" text DEFAULT 'accepted' NOT NULL, "superseded_by_order_id" bigint, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "trading_rounds" ( "id" bigserial PRIMARY KEY NOT NULL, "source" text DEFAULT 'scheduled' NOT NULL, "target_date" date NOT NULL, "signal_date" date, "scheduler_run_id" bigint, "rd_experiment_id" bigint, "experiment_name" text, "run_id" text, "model_path" text, "strategy_snapshot" jsonb, "account_equity_at_sizing" numeric, "status" text DEFAULT 'open' NOT NULL, "locked_intent_id" bigint, "summary_metrics" jsonb, "feedback_note" text, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint ALTER TABLE "fact_events" ADD CONSTRAINT "fact_events_round_id_trading_rounds_id_fk" FOREIGN KEY ("round_id") REFERENCES "public"."trading_rounds"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "round_decisions" ADD CONSTRAINT "round_decisions_round_id_trading_rounds_id_fk" FOREIGN KEY ("round_id") REFERENCES "public"."trading_rounds"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "round_decisions" ADD CONSTRAINT "round_decisions_intent_id_round_intents_id_fk" FOREIGN KEY ("intent_id") REFERENCES "public"."round_intents"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "round_decisions" ADD CONSTRAINT "round_decisions_superseded_by_round_decisions_id_fk" FOREIGN KEY ("superseded_by_decision_id") REFERENCES "public"."round_decisions"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "round_intents" ADD CONSTRAINT "round_intents_round_id_trading_rounds_id_fk" FOREIGN KEY ("round_id") REFERENCES "public"."trading_rounds"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "round_intents" ADD CONSTRAINT "round_intents_supersedes_intent_id_round_intents_id_fk" FOREIGN KEY ("supersedes_intent_id") REFERENCES "public"."round_intents"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "round_orders" ADD CONSTRAINT "round_orders_round_id_trading_rounds_id_fk" FOREIGN KEY ("round_id") REFERENCES "public"."trading_rounds"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "round_orders" ADD CONSTRAINT "round_orders_decision_id_round_decisions_id_fk" FOREIGN KEY ("decision_id") REFERENCES "public"."round_decisions"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "round_orders" ADD CONSTRAINT "round_orders_superseded_by_round_orders_id_fk" FOREIGN KEY ("superseded_by_order_id") REFERENCES "public"."round_orders"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "trading_rounds" ADD CONSTRAINT "trading_rounds_scheduler_run_id_scheduler_runs_id_fk" FOREIGN KEY ("scheduler_run_id") REFERENCES "public"."scheduler_runs"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "trading_rounds" ADD CONSTRAINT "trading_rounds_rd_experiment_id_rd_experiments_id_fk" FOREIGN KEY ("rd_experiment_id") REFERENCES "public"."rd_experiments"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint CREATE INDEX "fact_events_round_id_idx" ON "fact_events" USING btree ("round_id");--> statement-breakpoint CREATE INDEX "fact_events_round_kind_idx" ON "fact_events" USING btree ("round_id","kind");--> statement-breakpoint CREATE INDEX "round_decisions_round_id_idx" ON "round_decisions" USING btree ("round_id");--> statement-breakpoint CREATE INDEX "round_decisions_round_symbol_idx" ON "round_decisions" USING btree ("round_id","symbol");--> statement-breakpoint CREATE INDEX "round_decisions_intent_id_idx" ON "round_decisions" USING btree ("intent_id");--> statement-breakpoint CREATE INDEX "round_intents_round_id_idx" ON "round_intents" USING btree ("round_id");--> statement-breakpoint CREATE INDEX "round_intents_round_version_idx" ON "round_intents" USING btree ("round_id","version");--> statement-breakpoint CREATE INDEX "round_orders_round_id_idx" ON "round_orders" USING btree ("round_id");--> statement-breakpoint CREATE INDEX "round_orders_decision_id_idx" ON "round_orders" USING btree ("decision_id");--> statement-breakpoint CREATE INDEX "round_orders_alpaca_order_id_idx" ON "round_orders" USING btree ("alpaca_order_id");--> statement-breakpoint CREATE INDEX "trading_rounds_target_date_idx" ON "trading_rounds" USING btree ("target_date");--> statement-breakpoint CREATE INDEX "trading_rounds_scheduler_run_id_idx" ON "trading_rounds" USING btree ("scheduler_run_id");--> statement-breakpoint CREATE INDEX "trading_rounds_rd_experiment_id_idx" ON "trading_rounds" USING btree ("rd_experiment_id");--> statement-breakpoint CREATE INDEX "trading_rounds_locked_intent_id_idx" ON "trading_rounds" USING btree ("locked_intent_id");