initialize_database

async safir.database.initialize_database(url: str, password: Optional[str], logger: structlog.stdlib.BoundLogger, *, schema: sqlalchemy.sql.schema.MetaData, reset: bool = False) sqlalchemy.ext.asyncio.engine.AsyncEngine

Create and initialize a new database.

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

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

  • logger (structlog.stdlib.BoundLogger) – Logger used to report problems

  • schema (sqlalchemy.sql.schema.MetaData) – Metadata for the database schema. Generally this will be Base.metadata where Base is the declarative base used as the base class for all ORM table definitions. The caller must ensure that all table definitions have been imported by Python before calling this function, or parts of the schema will be missing.

  • reset (bool, optional) – If set to True, drop all tables and reprovision the database. Useful when running tests with an external database. Default is False.

Returns

engine – Database engine for the initialized database. This may be used by the caller to perform any additional necessary database initialization not included in the schema, such as adding default table rows. The engine must then be closed with await engine.dispose().

Return type

sqlalchemy.ext.asyncio.AsyncEngine

Raises

DatabaseInitializationError – After five attempts, the database still could not be initialized. This is normally due to some connectivity issue to the database.