added DatabaseSetupError
This commit is contained in:
@@ -17,6 +17,10 @@ NAMING_CONVENTION = {
|
||||
}
|
||||
|
||||
|
||||
class DatabaseSetupError(RuntimeError):
|
||||
"""Raised when database configuration is missing or invalid."""
|
||||
|
||||
|
||||
def get_connection_info(name: str) -> tuple[str, str, str, str, str | None]:
|
||||
"""Get connection info from environment variables."""
|
||||
database = getenv(f"{name}_DB")
|
||||
@@ -27,11 +31,18 @@ def get_connection_info(name: str) -> tuple[str, str, str, str, str | None]:
|
||||
|
||||
if None in (database, host, port, username):
|
||||
error = f"Missing environment variables for Postgres connection.\n{database=}\n{host=}\n{port=}\n{username=}\n"
|
||||
raise ValueError(error)
|
||||
return cast("tuple[str, str, str, str, str | None]", (database, host, port, username, password))
|
||||
raise DatabaseSetupError(error)
|
||||
return cast(
|
||||
"tuple[str, str, str, str, str | None]",
|
||||
(database, host, port, username, password),
|
||||
)
|
||||
|
||||
|
||||
def get_postgres_engine(*, name: str = "POSTGRES", pool_pre_ping: bool = True) -> Engine:
|
||||
def get_postgres_engine(
|
||||
*,
|
||||
name: str = "POSTGRES",
|
||||
pool_pre_ping: bool = True,
|
||||
) -> Engine:
|
||||
"""Create a SQLAlchemy engine from environment variables."""
|
||||
database, host, port, username, password = get_connection_info(name)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user