- CRITICAL: Fix migration FK refs player(id) → player(player_id) - Remove dead is_start flag from pitching groups (no starts column) - Fix hr → homerun in test make_play helper - Add explanatory comment to ruff.toml - Replace print() with logging in seed script Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
67 lines
2.1 KiB
Python
67 lines
2.1 KiB
Python
"""Seed script for EvolutionTrack records.
|
|
|
|
Loads track definitions from evolution_tracks.json and upserts them into the
|
|
database using get_or_create keyed on name. Existing tracks have their
|
|
thresholds and formula updated to match the JSON in case values have changed.
|
|
|
|
Can be run standalone:
|
|
python -m app.seed.evolution_tracks
|
|
"""
|
|
|
|
import json
|
|
import logging
|
|
from pathlib import Path
|
|
|
|
from app.db_engine import EvolutionTrack
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
_JSON_PATH = Path(__file__).parent / "evolution_tracks.json"
|
|
|
|
|
|
def seed_evolution_tracks() -> list[EvolutionTrack]:
|
|
"""Upsert evolution tracks from JSON seed data.
|
|
|
|
Returns a list of EvolutionTrack instances that were created or updated.
|
|
"""
|
|
raw = _JSON_PATH.read_text(encoding="utf-8")
|
|
track_defs = json.loads(raw)
|
|
|
|
results: list[EvolutionTrack] = []
|
|
|
|
for defn in track_defs:
|
|
track, created = EvolutionTrack.get_or_create(
|
|
name=defn["name"],
|
|
defaults={
|
|
"card_type": defn["card_type"],
|
|
"formula": defn["formula"],
|
|
"t1_threshold": defn["t1_threshold"],
|
|
"t2_threshold": defn["t2_threshold"],
|
|
"t3_threshold": defn["t3_threshold"],
|
|
"t4_threshold": defn["t4_threshold"],
|
|
},
|
|
)
|
|
|
|
if not created:
|
|
# Update mutable fields in case the JSON values changed.
|
|
track.card_type = defn["card_type"]
|
|
track.formula = defn["formula"]
|
|
track.t1_threshold = defn["t1_threshold"]
|
|
track.t2_threshold = defn["t2_threshold"]
|
|
track.t3_threshold = defn["t3_threshold"]
|
|
track.t4_threshold = defn["t4_threshold"]
|
|
track.save()
|
|
|
|
action = "created" if created else "updated"
|
|
logger.info("[%s] %s (card_type=%s)", action, track.name, track.card_type)
|
|
results.append(track)
|
|
|
|
return results
|
|
|
|
|
|
if __name__ == "__main__":
|
|
logging.basicConfig(level=logging.INFO)
|
|
logger.info("Seeding evolution tracks...")
|
|
tracks = seed_evolution_tracks()
|
|
logger.info("Done. %d track(s) processed.", len(tracks))
|