- Add lifespan context manager to app/main.py with startup/shutdown hooks - Wire startup: init_db(), init_redis(), CardService.load_all() - Wire shutdown: close_db(), close_redis() - Add /health/ready endpoint for readiness checks - Add CORS middleware with configurable origins - Disable docs in production (only available in dev) - Export get_session_dependency from app/db/__init__.py for FastAPI DI - Add game_cache_ttl_seconds to Settings (configurable, was hardcoded) - Fix datetime.utcnow() deprecation (4 occurrences) -> datetime.now(UTC) - Update test to match S3 image URL (was placeholder CDN) All 974 tests passing.
50 lines
1.2 KiB
Python
50 lines
1.2 KiB
Python
"""Database module for Mantimon TCG.
|
|
|
|
This module provides the database infrastructure including:
|
|
- Async SQLAlchemy engine and session management
|
|
- Base model class with common columns
|
|
- Redis connection utilities
|
|
- All database models
|
|
|
|
Usage:
|
|
from app.db import get_session, Base
|
|
from app.db.models import User, Deck, Collection
|
|
|
|
async with get_session() as session:
|
|
user = await session.get(User, user_id)
|
|
|
|
# For FastAPI dependency injection:
|
|
from app.db import get_session_dependency
|
|
from fastapi import Depends
|
|
|
|
@app.get("/users/{user_id}")
|
|
async def get_user(session: AsyncSession = Depends(get_session_dependency)):
|
|
...
|
|
|
|
Exports:
|
|
- get_session: Async context manager for database sessions
|
|
- get_session_dependency: FastAPI dependency for sessions
|
|
- get_engine: Get the async engine instance
|
|
- Base: Declarative base class for models
|
|
- init_db: Initialize database (create tables)
|
|
- close_db: Close database connections
|
|
"""
|
|
|
|
from app.db.base import Base
|
|
from app.db.session import (
|
|
close_db,
|
|
get_engine,
|
|
get_session,
|
|
get_session_dependency,
|
|
init_db,
|
|
)
|
|
|
|
__all__ = [
|
|
"Base",
|
|
"get_session",
|
|
"get_session_dependency",
|
|
"get_engine",
|
|
"init_db",
|
|
"close_db",
|
|
]
|