Skip to content

sqlite

Full name: tenets.storage.sqlite

sqlite

SQLite storage utilities for Tenets.

This module centralizes SQLite database path resolution, connection management, and pragmas. All persistent storage (sessions, tenets, config state) should use this utility to open connections inside the configured cache directory.

By default, the cache directory is resolved by TenetsConfig. Do not write inside the installed package directory. When Tenets is installed via pip, the package location may be read-only; the cache directory will be user- or project-local and writable.

Classes

SQLitePathsdataclass

Python
SQLitePaths(root: Path, main_db: Path)

Resolved paths for SQLite databases.

ATTRIBUTEDESCRIPTION
root

The cache directory root where DB files live.

TYPE:Path

main_db

Path to the main Tenets database file.

TYPE:Path

Database

Python
Database(config: TenetsConfig)

SQLite database manager applying Tenets pragmas.

Use this to obtain connections to the main Tenets DB file located in the configured cache directory.

Source code in tenets/storage/sqlite.py
Python
def __init__(self, config: TenetsConfig):
    self.config = config
    self.logger = get_logger(__name__)
    self.paths = self._resolve_paths(config)
    self._ensure_dirs()
Functions
connect
Python
connect(db_path: Optional[Path] = None) -> sqlite3.Connection

Open a SQLite connection with configured PRAGMAs applied.

PARAMETERDESCRIPTION
db_path

Optional custom DB path; defaults to main DB path.

TYPE:Optional[Path]DEFAULT:None

Source code in tenets/storage/sqlite.py
Python
def connect(self, db_path: Optional[Path] = None) -> sqlite3.Connection:
    """Open a SQLite connection with configured PRAGMAs applied.

    Args:
        db_path: Optional custom DB path; defaults to main DB path.
    Returns:
        sqlite3.Connection ready for use.
    """
    path = Path(db_path) if db_path else self.paths.main_db
    # Enable declared-type and column-name based conversions and allow
    # cross-thread usage for tests that access the same connection across threads.
    conn = sqlite3.connect(
        path,
        detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES,
        check_same_thread=False,
    )
    self._apply_pragmas(conn, self.config.cache.sqlite_pragmas)
    return conn

Functions