Skip to content

database_setup

Database setup service for bot initialization.

Classes:

Classes

DatabaseSetupService

Python
DatabaseSetupService(db_service: DatabaseService)

Bases: BaseSetupService

Handles complete database initialization during bot setup.

Initialize the database setup service.

Parameters:

  • db_service (DatabaseService) –

    The database service instance to use for connections.

Methods:

  • safe_setup

    Execute setup with standardized error handling and tracing.

  • setup

    Set up and validate the database connection and run migrations.

Functions

_find_project_root
Python
_find_project_root() -> Path

Find the project root by looking for alembic.ini.

Returns:

  • Path

    The project root directory containing alembic.ini.

safe_setup async
Python
safe_setup() -> bool

Execute setup with standardized error handling and tracing.

Returns:

  • True if setup succeeded, False if it failed
_build_alembic_config
Python
_build_alembic_config() -> Config

Build Alembic configuration with suppressed stdout output.

Returns:

  • Config

    The configured Alembic Config object.

Notes

Most configuration is read from alembic.ini. Only the database URL is set programmatically as it comes from environment variables.

_log_step
Python
_log_step(step: str, status: str = 'info') -> None

Log a setup step with consistent formatting.

_upgrade_head_if_needed async
Python
_upgrade_head_if_needed() -> None

Run Alembic upgrade to head on startup.

This call is idempotent and safe to run on startup. If database is unavailable, migrations are skipped with a warning. Runs migration synchronously with a short timeout.

Note

Unlike other setup steps, this method does not raise exceptions on failure. If migrations cannot run (e.g., database unavailable), it logs a warning and continues, allowing the bot to start without blocking on migrations.

setup async
Python
setup() -> None

Set up and validate the database connection and run migrations.

Raises:

_create_tables async
Python
_create_tables() -> None

Create database tables if they don't exist.

_validate_schema async
Python
_validate_schema() -> None

Validate that the database schema matches model definitions.