"""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.")