Skip to content

feat: Region-datasets join table and simulation year#95

Closed
anth-volk wants to merge 2 commits intoapp-v2-migrationfrom
region-datasets-join-table
Closed

feat: Region-datasets join table and simulation year#95
anth-volk wants to merge 2 commits intoapp-v2-migrationfrom
region-datasets-join-table

Conversation

@anth-volk
Copy link
Contributor

Fixes #94

Summary

  • Replace regions.dataset_id FK with region_datasets many-to-many join table so each region can link to multiple yearly datasets (2024–2035)
  • Add year column to simulations table for direct queryability
  • Add year parameter to economy analysis and simulation request schemas
  • Update seed_regions.py as the sole source of truth for region→dataset wiring (matches datasets by filepath pattern: states/, districts/, CPS fallback)
  • Remove region update code from import_state_datasets.py

Test plan

  • alembic upgrade head — join table created, data migrated, FK dropped, year column added
  • Run seed_regions.py — regions created with dataset links via join table
  • Verify DB: SELECT r.code, d.name, d.year FROM region_datasets rd JOIN regions r ON r.id = rd.region_id JOIN datasets d ON d.id = rd.dataset_id WHERE r.code = 'state/al' ORDER BY d.year → multiple rows (one per year)
  • POST /analysis/economic-impact with region: "state/al", year: 2026 → uses correct yearly dataset
  • Same request without year → uses latest available year

🤖 Generated with Claude Code

anth-volk and others added 2 commits March 3, 2026 19:30
Each region can now link to multiple datasets (one per year, 2024-2035)
via a many-to-many join table, and simulations store their year directly.
seed_regions.py is the sole source of truth for region-to-dataset wiring.

- Add RegionDatasetLink model (composite PK: region_id + dataset_id)
- Remove dataset_id FK from Region, add datasets list relationship
- Add year column to Simulation model
- Alembic migration with data migration from old FK to join table
- Update analysis.py and simulations.py to resolve datasets from join
  table with optional year filtering
- Update seed_regions.py to create RegionDatasetLink entries based on
  dataset filepath patterns (states/, districts/, CPS fallback)
- Remove region update code from import_state_datasets.py

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Test fixtures were still setting dataset_id on Region (removed column).
Now they create RegionDatasetLink entries via the join table instead.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@anth-volk anth-volk marked this pull request as ready for review March 3, 2026 18:59
@anth-volk
Copy link
Contributor Author

Closing and reopening — GitHub has been stuck for 5+ minutes trying to determine merge status.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant