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)

AIMBAT database engine.

set_sqlite_pragma

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

Enable foreign key support 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 foreign key support for each new SQLite connection."""
    cursor = dbapi_connection.cursor()
    cursor.execute("PRAGMA foreign_keys=ON")
    cursor.close()