diff --git a/app/seed/evolution_tracks.json b/app/seed/evolution_tracks.json index a4bd1f0..4f06142 100644 --- a/app/seed/evolution_tracks.json +++ b/app/seed/evolution_tracks.json @@ -1,5 +1,29 @@ [ - {"name": "Batter", "card_type": "batter", "formula": "pa+tb*2", "t1": 37, "t2": 149, "t3": 448, "t4": 896}, - {"name": "Starting Pitcher", "card_type": "sp", "formula": "ip+k", "t1": 10, "t2": 40, "t3": 120, "t4": 240}, - {"name": "Relief Pitcher", "card_type": "rp", "formula": "ip+k", "t1": 3, "t2": 12, "t3": 35, "t4": 70} + { + "name": "Batter Track", + "card_type": "batter", + "formula": "pa + tb * 2", + "t1_threshold": 37, + "t2_threshold": 149, + "t3_threshold": 448, + "t4_threshold": 896 + }, + { + "name": "Starting Pitcher Track", + "card_type": "sp", + "formula": "ip + k", + "t1_threshold": 10, + "t2_threshold": 40, + "t3_threshold": 120, + "t4_threshold": 240 + }, + { + "name": "Relief Pitcher Track", + "card_type": "rp", + "formula": "ip + k", + "t1_threshold": 3, + "t2_threshold": 12, + "t3_threshold": 35, + "t4_threshold": 70 + } ] diff --git a/app/seed/evolution_tracks.py b/app/seed/evolution_tracks.py index 178f68e..3875a95 100644 --- a/app/seed/evolution_tracks.py +++ b/app/seed/evolution_tracks.py @@ -1,41 +1,62 @@ -"""Seed data fixture for EvolutionTrack. +"""Seed script for EvolutionTrack records. -Inserts the three universal evolution tracks (Batter, Starting Pitcher, -Relief Pitcher) if they do not already exist. Safe to call multiple times -thanks to get_or_create — depends on WP-01 (EvolutionTrack model) to run. +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 os +from pathlib import Path -_JSON_PATH = os.path.join(os.path.dirname(__file__), "evolution_tracks.json") +from app.db_engine import EvolutionTrack + +_JSON_PATH = Path(__file__).parent / "evolution_tracks.json" -def load_tracks(): - """Return the locked list of evolution track dicts from the JSON fixture.""" - with open(_JSON_PATH) as fh: - return json.load(fh) +def seed_evolution_tracks() -> list[EvolutionTrack]: + """Upsert evolution tracks from JSON seed data. - -def seed(model_class=None): - """Insert evolution tracks that are not yet in the database. - - Args: - model_class: Peewee model with get_or_create support. Defaults to - ``app.db_engine.EvolutionTrack`` (imported lazily so this module - can be imported before WP-01 lands). - - Returns: - List of (instance, created) tuples from get_or_create. + Returns a list of EvolutionTrack instances that were created or updated. """ - if model_class is None: - from app.db_engine import EvolutionTrack as model_class # noqa: PLC0415 + raw = _JSON_PATH.read_text(encoding="utf-8") + track_defs = json.loads(raw) - results = [] - for track in load_tracks(): - instance, created = model_class.get_or_create( - card_type=track["card_type"], - defaults=track, + 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"], + }, ) - results.append((instance, created)) + + 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" + print(f" [{action}] {track.name} (card_type={track.card_type})") + results.append(track) + return results + + +if __name__ == "__main__": + print("Seeding evolution tracks...") + tracks = seed_evolution_tracks() + print(f"Done. {len(tracks)} track(s) processed.")