feat(backend): implement database layer with models, schemas and repositories

Complete backend core implementation (BE-001 to BE-005):

BE-001: Database Connection & Session Management
- Create src/core/database.py with async SQLAlchemy 2.0
- Configure engine with pool_size=20
- Implement get_db() FastAPI dependency

BE-002: SQLAlchemy Models (5 models)
- Base model with TimestampMixin
- Scenario: status enum, relationships, cost tracking
- ScenarioLog: message hash, PII detection, metrics
- ScenarioMetric: time-series with extra_data (JSONB)
- AwsPricing: service pricing with region support
- Report: format enum, file tracking, extra_data

BE-003: Pydantic Schemas
- Scenario: Create, Update, Response, List schemas
- Log: Ingest, Response schemas
- Metric: Summary, CostBreakdown, MetricsResponse
- Common: PaginatedResponse generic type

BE-004: Base Repository Pattern
- Generic BaseRepository[T] with CRUD operations
- Methods: get, get_multi, count, create, update, delete
- Dynamic filter support

BE-005: Scenario Repository
- Extends BaseRepository[Scenario]
- Specific methods: get_by_name, list_by_status, list_by_region
- Business methods: update_status, increment_total_requests, update_total_cost
- ScenarioStatus enum
- Singleton instance: scenario_repository

All models, schemas and repositories tested and working.

Tasks: BE-001, BE-002, BE-003, BE-004, BE-005 complete
This commit is contained in:
Luca Sacchi Ricciardi
2026-04-07 14:20:02 +02:00
parent 216f9e229c
commit ebefc323c3
18 changed files with 1322 additions and 0 deletions

41
src/core/database.py Normal file
View File

@@ -0,0 +1,41 @@
"""Database configuration and session management."""
import os
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
from sqlalchemy.orm import declarative_base
# URL dal environment o default per dev
DATABASE_URL = os.getenv(
"DATABASE_URL", "postgresql+asyncpg://app:changeme@localhost:5432/mockupaws"
)
# Engine async
engine = create_async_engine(
DATABASE_URL,
echo=False, # Set to True for debug SQL
future=True,
pool_size=20,
max_overflow=0,
)
# Session factory
AsyncSessionLocal = async_sessionmaker(
engine,
class_=AsyncSession,
expire_on_commit=False,
autocommit=False,
autoflush=False,
)
# Base per i modelli
Base = declarative_base()
# Dependency per FastAPI
async def get_db() -> AsyncSession:
"""Dependency that provides a database session."""
async with AsyncSessionLocal() as session:
try:
yield session
finally:
await session.close()