Skip to content

aimbat.db

Database engine for the AIMBAT project file.

The engine is created from Settings.db_url (see aimbat._config), which defaults to a SQLite database at aimbat.db in the current working directory. The path can be overridden via environment variable or .env file:

AIMBAT_PROJECT=/path/to/project.db  # derives db_url automatically
AIMBAT_DB_URL=sqlite+pysqlite:///absolute/path/to/project.db  # explicit override

For SQLite connections, PRAGMA foreign_keys=ON is set automatically on every new connection to enforce referential integrity.

Attributes:

Name Type Description
engine

AIMBAT database engine.

engine module-attribute

engine = create_engine(
    url=db_url,
    echo=False,
    connect_args={"check_same_thread": False, "timeout": 30}
    if "sqlite" in db_url
    else {},
)

AIMBAT database engine.

set_sqlite_pragma

set_sqlite_pragma(
    dbapi_connection: Connection,
    connection_record: ConnectionPoolEntry,
) -> None

Enable specific PRAGMAs for each new SQLite connection.

Source code in src/aimbat/db.py
@event.listens_for(engine, "connect")
def set_sqlite_pragma(
    dbapi_connection: sqlite3.Connection, connection_record: ConnectionPoolEntry
) -> None:
    """Enable specific PRAGMAs for each new SQLite connection."""
    logger.debug(
        f"Configuring SQLite connection with: {', '.join(_SQLITE_PRAGMAS)}."
    )
    cursor = dbapi_connection.cursor()
    for pragma in _SQLITE_PRAGMAS:
        cursor.execute(pragma)
    cursor.close()