Skip to content

feat(db): read SQLAlchemy engine configuration from config file #73

@itisnotyourenv

Description

@itisnotyourenv

Problem

SQLAlchemy engine pool parameters are currently hardcoded in src/infrastructure/db/factory.py:

def create_engine(
    db_config: PostgresConfig,
    pool_size: int = 30,
    pool_timeout: int = 30,
    pool_recycle: int = 3600,
    max_overflow: int = 20,
) -> AsyncEngine:

These values cannot be changed without modifying the source code, which makes it impossible to tune the connection pool per environment (dev, staging, production).

Current State

PostgresConfig in src/infrastructure/config.py only exposes:

  • Connection parameters (host, port, user, password, db)
  • echo flag

Hardcoded in create_engine:

Parameter Hardcoded Value
pool_size 30
pool_timeout 30
pool_recycle 3600
max_overflow 20

Not configurable at all:

  • pool_pre_ping (recommended for detecting stale connections)
  • echo_pool (useful for debugging connection pool behavior)

Proposed Solution

  1. Add engine pool fields to PostgresConfig in src/infrastructure/config.py with sensible defaults:
class PostgresConfig(BaseModel):
    host: str
    port: int
    user: str
    password: str
    db: str
    echo: bool = False

    # Engine pool configuration
    pool_size: int = 30
    pool_timeout: int = 30
    pool_recycle: int = 3600
    max_overflow: int = 20
    pool_pre_ping: bool = True
    echo_pool: bool = False
  1. Update create_engine in src/infrastructure/db/factory.py to read from config instead of function defaults.

  2. Update config-example.yaml with the new postgres fields (commented out with default values).

Benefits

  • Environment-specific pool tuning without code changes
  • Production can use larger pools, dev/test can use smaller ones
  • pool_pre_ping enabled by default improves connection reliability
  • Consistent with how other settings (log level, auth, etc.) are already managed

Files to Change

  • src/infrastructure/config.py
  • src/infrastructure/db/factory.py
  • config-example.yaml

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions