"""Seed script for RefractorTrack records. Loads track definitions from refractor_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.refractor_tracks """ import json import logging from pathlib import Path from app.db_engine import RefractorTrack logger = logging.getLogger(__name__) _JSON_PATH = Path(__file__).parent / "refractor_tracks.json" def seed_refractor_tracks() -> list[RefractorTrack]: """Upsert refractor tracks from JSON seed data. Returns a list of RefractorTrack instances that were created or updated. """ raw = _JSON_PATH.read_text(encoding="utf-8") track_defs = json.loads(raw) results: list[RefractorTrack] = [] for defn in track_defs: track, created = RefractorTrack.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 refractor tracks...") tracks = seed_refractor_tracks() logger.info("Done. %d track(s) processed.", len(tracks))