# backend/scripts/apply_trainer_schemas.py from __future__ import annotations import json from pathlib import Path REPO_ROOT = Path(__file__).resolve().parents[2] TEMPLATES_DIR = REPO_ROOT / "backend" / "templates" SCHEMAS_DIR = REPO_ROOT / "backend" / "trainer_schemas" def main() -> None: if not SCHEMAS_DIR.exists(): raise RuntimeError(f"Missing schemas dir: {SCHEMAS_DIR}") schema_files = sorted(SCHEMAS_DIR.glob("*.schema.json")) if not schema_files: raise RuntimeError(f"No schema files found in: {SCHEMAS_DIR}") applied = 0 for sf in schema_files: template_id = sf.name.replace(".schema.json", "") template_path = TEMPLATES_DIR / f"{template_id}.json" if not template_path.exists(): print(f"⚠️ skip (no template file): {template_path}") continue new_schema = json.loads(sf.read_text(encoding="utf-8")) if not isinstance(new_schema, dict): raise RuntimeError(f"Invalid schema json (not object): {sf}") if not isinstance(new_schema.get("fields"), list): raise RuntimeError(f"Invalid schema json (missing fields[]): {sf}") t = json.loads(template_path.read_text(encoding="utf-8")) t["schema"] = new_schema # Optional: bump template version when schema changes # t["version"] = int(t.get("version") or 0) + 1 template_path.write_text(json.dumps(t, indent=2) + "\n", encoding="utf-8") print(f"✅ updated {template_path} fields={len(new_schema['fields'])}") applied += 1 print(f"done. applied={applied}") if __name__ == "__main__": main()