create_sync_session#

safir.database.create_sync_session(url, password, logger=None, *, isolation_level=None, statement=None)#

Create a new sync database session.

Used instead of create_database_engine and create_async_session for sync code, such as Dramatiq workers. This combines engine creation with session creation.

Parameters:
  • url (str) – Database connection URL, not including the password.

  • password (str or None) – Database connection password.

  • logger (structlog.stdlib.BoundLogger, optional) – Logger for reporting errors. Used only if a statement is provided.

  • isolation_level (str, optional) – If specified, sets a non-default isolation level for the database engine.

  • statement (sqlalchemy.sql.expression.Select, optional) – If provided, statement to run to check database connectivity. This will be modified with limit(1) before execution. If not provided, database connectivity will not be checked.

Returns:

session – The database session proxy. This manages a separate session per thread and therefore should be thread-safe.

Return type:

sqlalchemy.orm.scoping.scoped_session

Raises:

ValueError – A password was provided but the connection URL has no username.