utils/sql_drills/scripts/regen_fixtures.py
Krzysztof kuhy Rudnicki f865508eb4
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
file length / file-length (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
palette drift / palette (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
shared gate tests / gate unit tests (push) Waiting to run
sql-drills pre-commit / pre-commit (push) Waiting to run
sql-drills tests / test (push) Waiting to run
Add sql_drills engine package
Shared MySQL drill engine extracted from testsAndMisc: sandboxed
per-run db and user, exact-match cell diff, predict drills (builtin
theory set incl. an error drill), save_generated validation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWYGW5NZL5uVpnqmNKo
2026-11-02 13:53:39 +01:00

50 lines
1.5 KiB
Python

"""Rebuild expected.txt, tables.json and error metadata of builtin drills.
Needs the live container. Reference = the saved solution (write drills) or the
query itself (predict drills); run it, then store what MySQL printed.
"""
from __future__ import annotations
import json
from pathlib import Path
import sys
from sql_drills import _paths, _sandbox, ensure_running
SOLUTIONS = Path(__file__).parents[1] / "sql_drills" / "tests" / "solutions"
def _reference(folder: Path, kind: str) -> str:
if kind == "predict":
return (folder / "query.sql").read_text()
return (SOLUTIONS / f"{folder.name}.sql").read_text()
def _regen(folder: Path) -> None:
meta_path = folder / "meta.json"
meta = json.loads(meta_path.read_text())
with _sandbox.sandbox((folder / "schema.sql").read_text()) as box:
done = box.execute(_reference(folder, meta["kind"]))
tables = box.tables()
if done.ok:
rows = "".join("\t".join(r) + "\n" for r in done.rows)
else:
meta |= {"error_code": done.code, "error_message": done.error}
rows = f"ERROR {done.code}\n"
meta_path.write_text(json.dumps(meta) + "\n")
(folder / "expected.txt").write_text(rows)
payload = {name: table.__dict__ for name, table in tables.items()}
(folder / "tables.json").write_text(json.dumps(payload, indent=1) + "\n")
def main() -> None:
ensure_running()
for folder in sorted(_paths.builtin_dir().iterdir()):
_regen(folder)
sys.stdout.write(f"{folder.name}\n")
if __name__ == "__main__":
main()