brackt/drizzle/0102_wild_wiccan.sql
Chris Parsons e5295812f6
Formalize simulator system with manifest, input-policy, runner, and admin UI (#409)
Introduces three new schema tables (simulator_profiles,
sports_season_simulator_configs, season_participant_simulator_inputs),
a central model layer (app/models/simulator.ts), and a single runner
entry point so every simulator run follows the same prepare → simulate
→ persist → snapshot → recalculate flow.

Key additions:
- manifest.ts: per-simulator display names, default configs, required/
  optional inputs, derivable-input declarations, and setup sections
- input-policy.ts: resolves sourceElo from projectedWins,
  projectedTablePoints, or sourceOdds; resolves ratings from sourceOdds;
  supports block / fallbackElo / averageKnown / worstKnownMinus strategies
- runner.ts: single entry point for admin simulation runs; materialises
  derived inputs, normalises result columns, zeroes omitted participants,
  snapshots EVs, and recalculates linked fantasy standings
- /admin/simulators: inventory page with per-season readiness and bulk run
- /admin/sports-seasons/:id/simulator: per-season setup page with readiness
  summary, input-policy editor, raw JSON config override, and CSV bulk input
- NCAAM/NCAAW simulators now read ratings from season_participant_simulator_inputs,
  falling back to the hardcoded name-keyed maps while DB data is being populated
- Clone flow copies simulator config by default; volatile inputs (odds, Elo)
  only copied when explicitly requested

Code-review fixes included in this commit:
- source field in compatibility bridge checked with !== null instead of !== undefined
- sourceEloRequirementLabel no longer appends "configured fallback" when the
  participant is already excluded from all resolved sources
- Duplicate inline label maps in input-policy.ts replaced with simulatorInputLabel
- save-config preserves existing inputPolicy when the submitted JSON omits it
- Input table truncation label added (Showing 20 of N)
- CSV description notes values must not contain commas
- N+1 comment added to listSportsSeasonSimulatorSummaries
- assertRegistrySchemaDriftFree called in manifest tests
- Runner test suite added covering happy path, already-running guard,
  readiness failure, empty results, and error recovery with status reset

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-11 21:09:53 -07:00

59 lines
No EOL
3 KiB
SQL

CREATE TABLE IF NOT EXISTS "season_participant_simulator_inputs" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"participant_id" uuid NOT NULL,
"sports_season_id" uuid NOT NULL,
"source_odds" integer,
"source_elo" integer,
"world_ranking" integer,
"rating" numeric(10, 4),
"projected_wins" numeric(6, 2),
"projected_table_points" numeric(6, 2),
"seed" integer,
"region" varchar(100),
"metadata" jsonb,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "simulator_profiles" (
"simulator_type" "simulator_type" PRIMARY KEY NOT NULL,
"display_name" varchar(255) NOT NULL,
"description" text,
"default_config" jsonb DEFAULT '{}'::jsonb NOT NULL,
"input_schema" jsonb DEFAULT '{}'::jsonb NOT NULL,
"is_active" boolean DEFAULT true NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "sports_season_simulator_configs" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"sports_season_id" uuid NOT NULL,
"simulator_type" "simulator_type" NOT NULL,
"config" jsonb DEFAULT '{}'::jsonb NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "season_participant_simulator_inputs" ADD CONSTRAINT "season_participant_simulator_inputs_participant_id_season_participants_id_fk" FOREIGN KEY ("participant_id") REFERENCES "public"."season_participants"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "season_participant_simulator_inputs" ADD CONSTRAINT "season_participant_simulator_inputs_sports_season_id_sports_seasons_id_fk" FOREIGN KEY ("sports_season_id") REFERENCES "public"."sports_seasons"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "sports_season_simulator_configs" ADD CONSTRAINT "sports_season_simulator_configs_sports_season_id_sports_seasons_id_fk" FOREIGN KEY ("sports_season_id") REFERENCES "public"."sports_seasons"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "season_participant_simulator_inputs_unique" ON "season_participant_simulator_inputs" USING btree ("participant_id","sports_season_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "season_participant_simulator_inputs_season_idx" ON "season_participant_simulator_inputs" USING btree ("sports_season_id");--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "sports_season_simulator_config_unique" ON "sports_season_simulator_configs" USING btree ("sports_season_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "sports_season_simulator_config_type_idx" ON "sports_season_simulator_configs" USING btree ("simulator_type");