Something went wrong. Try again.
Integrated repository, worktree, git stacker, and project manager
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384"""Package-level SQLite helpers.
Two context managers: `transaction()` wraps a unit of work with commit/rollback,`readonly()` is a connection-only CM for reads. Both take an optional `schema`string that is executed (idempotent `CREATE TABLE IF NOT EXISTS ...`) andcommitted before the caller's block runs, so a rollback can't drop the tableson first use."""
from __future__ import annotations
import sqlite3from collections.abc import Iteratorfrom contextlib import contextmanagerfrom pathlib import Path
def _apply_pragmas( conn: sqlite3.Connection, *, wal: bool, busy_timeout_ms: int | None,) -> None: """Apply opt-in connection PRAGMAs.
`busy_timeout` is set first so a concurrent writer is waited on rather than failing fast with SQLITE_BUSY. `wal` enables WAL journaling — the right mode when a long-lived writer (the `pm agent serve` daemon) and external readers share the file, since readers then get a non-blocking consistent snapshot. Both values are ints / a literal mode name, never user input, so the f-string is safe. """ if busy_timeout_ms is not None: conn.execute(f"PRAGMA busy_timeout={int(busy_timeout_ms)}") if wal: conn.execute("PRAGMA journal_mode=WAL")
@contextmanagerdef transaction( db_path: Path, *, schema: str = "", row_factory: type | None = None, wal: bool = False, busy_timeout_ms: int | None = None,) -> Iterator[sqlite3.Connection]: db_path.parent.mkdir(parents=True, exist_ok=True) conn = sqlite3.connect(db_path) _apply_pragmas(conn, wal=wal, busy_timeout_ms=busy_timeout_ms) if row_factory is not None: conn.row_factory = row_factory try: if schema: conn.executescript(schema) conn.commit() with conn: yield conn finally: conn.close()
@contextmanagerdef readonly( db_path: Path, *, schema: str = "", row_factory: type | None = None, wal: bool = False, busy_timeout_ms: int | None = None,) -> Iterator[sqlite3.Connection]: db_path.parent.mkdir(parents=True, exist_ok=True) conn = sqlite3.connect(db_path) _apply_pragmas(conn, wal=wal, busy_timeout_ms=busy_timeout_ms) if row_factory is not None: conn.row_factory = row_factory try: if schema: conn.executescript(schema) conn.commit() yield conn finally: conn.close()