diff --git a/app/seed/__init__.py b/app/seed/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/seed/evolution_tracks.json b/app/seed/evolution_tracks.json new file mode 100644 index 0000000..4f06142 --- /dev/null +++ b/app/seed/evolution_tracks.json @@ -0,0 +1,29 @@ +[ + { + "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 new file mode 100644 index 0000000..3875a95 --- /dev/null +++ b/app/seed/evolution_tracks.py @@ -0,0 +1,62 @@ +"""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 +from pathlib import Path + +from app.db_engine import EvolutionTrack + +_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" + 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.")