"""Seed the client_domains table from scripts/seed_client_domains.json. Idempotent — running this multiple times updates existing rows in place instead of erroring. Safe to run after deploys or whenever you've added new mappings to the JSON file. Usage: python -m scripts.seed_client_domains """ from __future__ import annotations import asyncio import json import sys from pathlib import Path # Make repo root importable when invoked as `python scripts/seed_client_domains.py` sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) from app.config import settings from app.database import get_db from app.lib.utils.client_routing import add_or_update_client_domain SEED_FILE = Path(__file__).resolve().parent / "seed_client_domains.json" async def main() -> None: data = json.loads(SEED_FILE.read_text()) # Strip the README-style key if present. data.pop("_README", None) if not data: print("Nothing to seed — JSON file has no mappings.") return async with get_db(settings.database_path) as conn: for domain, client_name in data.items(): await add_or_update_client_domain(conn, domain, client_name) print(f" {domain:40s} → {client_name}") print(f"\nSeeded {len(data)} domain mapping(s) into {settings.database_path}.") if __name__ == "__main__": asyncio.run(main())