Debian Codegraph @

scripts/write_legacy_cache.py

raw
1 """Write the version-2 catalog cache that `legacy_history_cache` copies.
2
3 A version-2 pickle names `harlequin.catalog_cache.CatalogCache` and
4 `harlequin.history.History`/`QueryExecution` by module path, so the fixture is
5 only worth what it was written by: run this against a *released* Harlequin that
6 still has that version, never against the working tree, which no longer has it.
7
8 uv run --with 'harlequin==2.14.0' --no-project python scripts/write_legacy_cache.py
9
10 The committed file is what pins the on-disk shape; regenerate it only to add a
11 record, and only the same way.
12 """
13
14 from __future__ import annotations
15
16 import pickle
17 from collections import deque
18 from datetime import datetime
19 from pathlib import Path
20
21 import harlequin.catalog_cache as catalog_cache
22 from harlequin.history import History, QueryExecution
23
24 OUT = Path(__file__).parent.parent / "tests" / "data" / "catalog-cache-2.pickle"
25
26 HISTORY: dict[str, list[tuple[str, datetime, int, float]]] = {
27 "abc123": [
28 ("select 1", datetime(2026, 8, 1, 9, 30), 1, 0.5),
29 ("sel", datetime(2026, 8, 1, 9, 31), -1, 0.0),
30 ],
31 "foo": [("select * from line_items", datetime(2026, 8, 1, 10, 30), 3, 1.25)],
32 }
33 """Kept in step with `tests/conftest.py::LEGACY_HISTORY`, which is what the
34 tests assert against."""
35
36
37 def main() -> None:
38 if catalog_cache.CACHE_VERSION != 2:
39 raise SystemExit(
40 f"this Harlequin writes version {catalog_cache.CACHE_VERSION} caches; "
41 "run it against a release that still writes version 2"
42 )
43 cache = catalog_cache.CatalogCache(
44 databases={},
45 s3={},
46 history={
47 connection: History(
48 queries=deque(
49 [
50 QueryExecution(
51 query_text=sql,
52 executed_at=ran_at,
53 result_row_count=rows,
54 elapsed=elapsed,
55 )
56 for sql, ran_at, rows, elapsed in records
57 ],
58 maxlen=500,
59 )
60 )
61 for connection, records in HISTORY.items()
62 },
63 )
64 OUT.write_bytes(pickle.dumps(cache))
65 print(f"wrote {OUT} ({OUT.stat().st_size} bytes)")
66
67
68 if __name__ == "__main__":
69 main()