database

Database abstraction layer for Bedrock Server Manager.

This module provides the Database class, which handles the connection to the database (SQLite, PostgreSQL, etc.) using SQLAlchemy. It manages session creation and lifecycle asynchronously.

class bedrock_server_manager.db.database.Database(db_url: str)

Manage asynchronous database connections and sessions.

Variables:
  • db_url – The database connection URL.

  • engine – The SQLAlchemy async engine.

  • SessionLocal – The async session factory.

  • _tables_created – Flag indicating if tables have been created.

async check_health() → bool

Verify a database round trip without changing stored data.

initialize() → None

Initialize the async database engine and session factory. This method is idempotent.

async upgrade_schema() → None

Runs pending Alembic database migrations up to head asynchronously.

async downgrade_schema(revision: str = '-1') → None

Runs Alembic database migrations downgrade asynchronously.

session_manager() → AsyncGenerator[AsyncSession, None]

Async context manager for database sessions.

Yields:

AsyncSession – An async database session.

close() → None

Closes the database connection engine synchronously if possible.

async shutdown() → None

Gracefully closes the database connection engine asynchronously.

async bedrock_server_manager.db.database.run_migrations_upgrade(db: Database) → None

Runs pending Alembic database migrations up to head asynchronously.

async bedrock_server_manager.db.database.run_migrations_downgrade(db: Database, revision: str = '-1') → None

Runs Alembic database migrations downgrade asynchronously.