Database

class bedrock_server_manager.db.database.Database(db_url: str | None = None)

Bases: object

Manages database connections and sessions.

db_url

The database connection URL.

Type:

str

engine

The SQLAlchemy engine.

Type:

Engine

SessionLocal

The session factory.

Type:

sessionmaker

_tables_created

Flag indicating if tables have been created.

Type:

bool

__init__(db_url: str | None = None)

Initializes the Database instance.

Parameters:

db_url (str, optional) – The database connection URL. If not provided, it will be loaded from the application configuration.

get_database_url()

Gets the database url from config.

Returns:

The database URL.

Return type:

str

Raises:

RuntimeError – If ‘db_url’ is missing from the configuration.

initialize()

Initializes the database engine and session.

Creates the SQLAlchemy engine and the session factory. This method is idempotent.

session_manager()

Context manager for database sessions.

Yields:

Session – A database session.

close()

Closes the database connection engine.