brackt/drizzle/meta
Chris Parsons 2848231235
Canonical tournament layer: schema + backfill (1/2) (#365)
* refactor(schema): rename per-window tables to season_* prefix

Renames participants, participant_expected_values, participant_qualifying_totals,
participant_results, participant_surface_elos to season_* prefixed names.
Renames event_results.participant_id to season_participant_id.
Phase 1a of canonical tournament layer migration.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* refactor: rename participant.ts model file to season-participant.ts

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* refactor(models): update model layer to use renamed schema exports

Updated all model files to use the renamed schema exports from Task 1:
- participants → seasonParticipants
- participantExpectedValues → seasonParticipantExpectedValues
- participantQualifyingTotals → seasonParticipantQualifyingTotals
- participantResults → seasonParticipantResults
- participantSurfaceElos → seasonParticipantSurfaceElos
- eventResults.participantId → eventResults.seasonParticipantId
- db.query relation accessors updated
- Relation field .participant → .seasonParticipant where applicable
- Import paths updated: ./participant → ./season-participant

Files updated (14 model files + 3 test files):
- draft-pick.ts
- draft-utils.ts
- event-result.ts
- group-stage-match.ts
- participant-result.ts
- qualifying-points.ts
- scoring-calculator.ts
- scoring-event.ts
- sports-season.ts
- surface-elo.ts
- team-score-events.ts
- cs2-major-stage.ts
- golf-skills.ts
- participant-expected-value.ts
- __tests__/sports-season.clone.test.ts
- __tests__/auto-pick.test.ts
- __tests__/executeAutoPick.timer.test.ts

Typecheck errors decreased: 779 → 499 (280 fewer)
All model file errors related to renamed schemas resolved.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* refactor(routes): update route layer to use renamed schema exports

- Update model import from ~/models/participant to ~/models/season-participant
- Rename schema.participants to schema.seasonParticipants
- Rename schema.participantResults to schema.seasonParticipantResults
- Rename db.query.participants to db.query.seasonParticipants
- Update 9 route files and 1 test file

Affected files:
- admin.sports-seasons.$id.events.$eventId.bracket.server.ts
- admin.sports-seasons.$id.participants.tsx
- api/draft.force-manual-pick.ts
- api/draft.make-pick.ts
- api/draft.replace-pick.ts
- api/seasons.$seasonId.draft.ts
- leagues/$leagueId.draft-board.$seasonId.tsx
- leagues/$leagueId.sports-seasons.$sportsSeasonId.server.ts
- admin/__tests__/sports-seasons-participants.test.ts

Error count reduced from 499 to 453 (46 errors fixed).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* refactor(routes): update route files for schema rename

Update route imports from ~/models/participant to ~/models/season-participant
and fix references to .participant/.participantId on event results to use
.seasonParticipant/.seasonParticipantId after schema rename.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* refactor(services): update simulators and services for renamed schema

Update all simulators, services, and server files to use renamed schema tables:
- participants → seasonParticipants
- participantExpectedValues → seasonParticipantExpectedValues
- participantResults → seasonParticipantResults
- eventResults.participantId → eventResults.seasonParticipantId

Files updated:
- 20 sport simulators (NBA, NHL, NFL, MLB, etc.)
- probability-updater.ts
- standings-sync/index.ts
- sports-data-sync.server.ts
- server/socket.ts

Typecheck errors reduced from 365 to 0.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* migration: rename per-window tables to season_* prefix

* fix(tests): update mock query keys after participants table rename

Change mock db.query.participants to db.query.seasonParticipants in test
files to match the schema rename from commit 66145a9. This fixes
"Cannot read properties of undefined (reading 'findFirst'/'findMany')"
errors that occurred when production code queries db.query.seasonParticipants
but test mocks only defined the old participants key.

Files updated:
- app/services/simulations/__tests__/world-cup-simulator.test.ts
- app/routes/api/__tests__/draft.force-manual-pick.test.ts
- app/routes/api/__tests__/draft.force-manual-pick.timer-mode.test.ts
- app/routes/api/__tests__/draft.make-pick.timer-mode.test.ts
- server/__tests__/timer-autodraft.test.ts
- app/models/__tests__/team-score-events.test.ts

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix(tests): update remaining mock paths and keys after schema rename

* fix(tests): final two mock stragglers after schema rename

- draft-pick.test.ts: assertion on db.query.participantQualifyingTotals
- process-match-result.test.ts: mock key participants → seasonParticipants

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* chore: add post-phase1a baseline capture (temp, for diff verification)

* chore: capture pre-migration baselines

* chore: remove post-phase1a capture helper after verification

* schema: add canonical tournament & participant tables

Adds tournaments, participants (canonical), tournament_results, and
participant_surface_elos (canonical). Adds nullable tournament_id to
scoring_events and nullable participant_id to season_participants.
Phase 1b of canonical tournament layer migration.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* feat(models): add canonical tournament, participant, result, surface-elo models

Adds CRUD modules for the canonical tables created in commit 775b905.
Each module mirrors existing app/models conventions (database() from
~/database/context, schema from ~/database/schema, mock-based tests).

Key implementation notes:
- participant.ts exports use "Canonical" prefix (CanonicalParticipant,
  createCanonicalParticipant, etc.) to avoid collision with existing
  season-participant.ts exports
- All four models include comprehensive unit tests following the
  audit-log.test.ts pattern
- Tests use mocked db responses (no real database access)
- Upsert functions use onConflictDoUpdate for appropriate unique constraints

Part of Phase 1b of canonical tournament layer migration.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* migration: create canonical tables, add nullable FKs

* scripts: add extractTournamentIdentity helper for backfill

Pure function that derives canonical (name, year) identity from a
scoring_events row, stripping trailing 4-digit years from the name or
falling back to eventDate. Used by the Phase 2 backfill to group
per-window events into canonical tournaments.

* scripts: add backfill orchestrator for canonical layer

Populates canonical tournaments, participants, tournament_results, and
participant_surface_elos from per-window data for qualifying-points
sports. Skips already-linked rows, is idempotent, and supports dry-run
mode.

Critical invariants enforced by the implementation:
- qualifying_points_awarded is never copied to tournament_results
- season_participant_qualifying_totals is never touched
- conflicting surface-Elo values between windows raise a loud error
  (recorded in report.errors) rather than overwriting

* scripts: add backfill CLI with dry-run default

Wires backfill-canonical-layer.ts to a CLI entry point exposed as
`npm run backfill:canonical`. Defaults to --dry-run; requires --apply
to actually write. Supports --sport=<uuid> to limit to a single sport.
Exits 2 if the backfill reports errors (e.g., surface-Elo conflicts).

* fix(backfill-cli): wrap runBackfill in DatabaseContext.run

The orchestrator uses database() from ~/database/context, which requires
AsyncLocalStorage to be populated. Wrap the CLI invocation with
DatabaseContext.run(db, ...) using server/db's cached connection pool.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix(backfill-cli): exit 0 on success so pg pool doesn't block

The cached postgres connection pool keeps the Node event loop open after
main() returns. Explicit process.exit(0) on success mirrors the pattern
in scripts/capture-baseline.ts.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

---------

Co-authored-by: Chris Parsons <chrisp@extrahop.com>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-01 20:13:18 -07:00
..
0000_snapshot.json Initial commit from create-react-router 2025-10-10 23:04:50 -07:00
0001_snapshot.json feat: add fantasy league schema and new league routes 2025-10-11 00:07:39 -07:00
0002_snapshot.json feat: add commissioner model and league management features 2025-10-11 00:29:04 -07:00
0003_snapshot.json feat: add user model, routes, and Clerk webhook integration 2025-10-11 00:53:39 -07:00
0004_snapshot.json feat: add user model, routes, and Clerk webhook integration 2025-10-11 00:53:39 -07:00
0005_snapshot.json refactor: remove guest book feature and update league settings success flow 2025-10-11 01:03:31 -07:00
0006_snapshot.json feat: add current season tracking and active league listing by user 2025-10-11 21:10:01 -07:00
0007_snapshot.json feat: add sports and participant models with admin user functionality 2025-10-12 21:16:00 -07:00
0008_snapshot.json feat: add icon support for sports with file upload and active seasons display 2025-10-13 10:30:47 -07:00
0009_snapshot.json refactor: remove isRequired field from season sports and templates 2025-10-13 15:00:59 -07:00
0010_snapshot.json feat: add invite code system for league seasons with member access control 2025-10-14 12:20:36 -07:00
0011_snapshot.json feat: add username field to users and display in league views 2025-10-14 21:20:58 -07:00
0012_snapshot.json feat: add team settings route and logo URL field to teams table 2025-10-14 22:04:37 -07:00
0013_snapshot.json feat: add draft order management with UI for manual and random ordering 2025-10-15 08:58:35 -07:00
0014_snapshot.json feat: add bulk participant creation and expected value field 2025-10-15 21:21:33 -07:00
0015_snapshot.json feat: add draft rounds configuration to league settings 2025-10-15 21:50:02 -07:00
0016_snapshot.json feat: add draft date/time selection to league creation and settings 2025-10-15 22:02:21 -07:00
0017_snapshot.json feat: add draft pick, queue, and timer models with database schema updates 2025-10-16 00:32:48 -07:00
0018_snapshot.json feat: add public draft board and simplify draft speed settings 2025-10-20 15:03:11 -07:00
0019_snapshot.json feat: add autodraft status and team connection indicators to draft grid 2025-10-21 23:22:17 -07:00
0020_snapshot.json feat: Add comprehensive scoring system database schema 2025-10-28 23:40:11 -07:00
0021_snapshot.json feat: Implement bracket expansion plan to support various tournament structures 2025-11-03 09:36:16 -08:00
0022_snapshot.json feat: Implement bracket expansion plan to support various tournament structures 2025-11-03 09:36:16 -08:00
0023_snapshot.json Refactor code structure for improved readability and maintainability 2025-11-03 13:16:37 -08:00
0024_snapshot.json feat: implement Expected Value System with ICM probability calculator 2025-11-17 22:19:46 -08:00
0035_snapshot.json User/chris/ev f1 framework (#93) 2026-03-09 15:34:31 -07:00
0038_snapshot.json feat: progressive floor scoring for playoff brackets (#100) 2026-03-10 10:27:58 -07:00
0040_snapshot.json Add playoff match game scheduling and odds management (#135) 2026-03-11 14:17:43 -07:00
0041_snapshot.json Add event_starts_at timestamp to scoring events for sub-day ordering (#146) 2026-03-15 10:22:42 -07:00
0043_snapshot.json Add per-event configurable region config for NCAA-style brackets (#149) (#149) 2026-03-15 21:52:47 -07:00
0048_snapshot.json Add Discord webhook notifications for standings updates (#157) 2026-03-17 11:16:36 -07:00
0050_snapshot.json Add NHL playoff simulator and fix simulator dropdown (#166) 2026-03-18 01:23:53 -07:00
0052_snapshot.json Add isDraftable toggle to sport seasons (#165) (#178) 2026-03-18 22:40:19 -07:00
0053_snapshot.json Add standard draft clock mode (#67) (#189) 2026-03-20 21:36:39 -07:00
0054_snapshot.json Add regular season standings for NBA/NHL (fixes #89) (#192) 2026-03-21 00:12:01 -07:00
0055_snapshot.json Add regular season standings for NBA/NHL (fixes #89) (#192) 2026-03-21 00:12:01 -07:00
0056_snapshot.json Add AFL season + finals simulator (closes #126) (#204) 2026-03-22 01:57:39 -07:00
0057_snapshot.json Add snooker bracket simulator and Elo ratings admin UI, fixes #119 2026-03-23 08:24:28 -07:00
0058_snapshot.json Add snooker bracket simulator and Elo ratings admin UI, fixes #119 2026-03-23 08:24:28 -07:00
0059_snapshot.json Add tennis Grand Slam simulator with surface Elo ratings, fixes #116 (#216) 2026-03-23 23:59:35 -07:00
0060_snapshot.json Add tennis Grand Slam simulator with surface Elo ratings, fixes #116 (#216) 2026-03-23 23:59:35 -07:00
0061_snapshot.json Add golf qualifying points simulator (Plackett-Luce Monte Carlo) (#223) 2026-03-24 21:46:02 -07:00
0063_snapshot.json Add WNBA playoff simulator with SRS-based Elo ratings, fixes #125 (#231) 2026-03-26 00:34:51 -07:00
0064_snapshot.json Prevent duplicate participants within a sports season, fixes #69 (#236) 2026-03-27 01:09:04 -07:00
0065_snapshot.json Add FIFA World Cup 2026 support with group stage display and Monte Carlo simulator (#242) 2026-03-29 10:27:47 -07:00
0066_snapshot.json Add FIFA World Cup 2026 support with group stage display and Monte Carlo simulator (#242) 2026-03-29 10:27:47 -07:00
0067_snapshot.json Clean up migration chain and add verification migration (#268) 2026-04-06 10:53:51 -04:00
0068_snapshot.json Clean up migration chain and add verification migration (#268) 2026-04-06 10:53:51 -04:00
0069_snapshot.json Clean up migration chain and add verification migration (#268) 2026-04-06 10:53:51 -04:00
0070_snapshot.json Clean up migration chain and add verification migration (#268) 2026-04-06 10:53:51 -04:00
0071_snapshot.json Add NFL season + playoffs Monte Carlo simulator (#272) 2026-04-07 09:39:34 -04:00
0072_snapshot.json Add NCAA Football CFP simulator (12-team bracket) (#278) 2026-04-08 09:48:32 -04:00
0073_snapshot.json Add LLWS bracket Monte Carlo simulator (#280) 2026-04-08 16:52:10 -04:00
0074_snapshot.json feat: add vorp_value column to participants table 2026-04-09 01:57:22 +00:00
0075_snapshot.json Add audit logging for commissioner actions (#293) 2026-04-13 15:45:39 -07:00
0076_snapshot.json fix: league settings sports swap and audit log detail improvements (#294) 2026-04-14 21:41:05 -07:00
0077_snapshot.json New design (#309) 2026-04-23 13:14:55 -07:00
0078_snapshot.json New design (#309) 2026-04-23 13:14:55 -07:00
0079_snapshot.json New design (#309) 2026-04-23 13:14:55 -07:00
0080_snapshot.json New design (#309) 2026-04-23 13:14:55 -07:00
0081_snapshot.json Migrate authentication from Clerk to BetterAuth (#324) 2026-04-24 22:00:49 -07:00
0082_snapshot.json Migrate authentication from Clerk to BetterAuth (#324) 2026-04-24 22:00:49 -07:00
0083_snapshot.json Add overnight pause for draft timers (#335) 2026-04-26 22:31:52 -07:00
0084_snapshot.json Migrate auth from Clerk to BetterAuth (#354) 2026-04-29 10:03:50 -07:00
0085_snapshot.json Mobile draft room improvements: watchlist, collapsible picks feed, autodraft controls (#355) 2026-04-29 11:49:26 -07:00
0086_snapshot.json Add draft room closure after completion (#359) 2026-04-30 10:14:14 -07:00
0087_snapshot.json Canonical tournament layer: schema + backfill (1/2) (#365) 2026-05-01 20:13:18 -07:00
0088_snapshot.json Canonical tournament layer: schema + backfill (1/2) (#365) 2026-05-01 20:13:18 -07:00
_journal.json Canonical tournament layer: schema + backfill (1/2) (#365) 2026-05-01 20:13:18 -07:00