Database & Migrations: - Update all Alembic migrations for PostgreSQL compatibility - Remove SQLite-specific syntax (AUTOINCREMENT, etc.) - Add database utility helpers for PostgreSQL operations - Fix services to use PostgreSQL-compatible queries Documentation: - Add comprehensive Docker deployment guide - Add production deployment documentation - Add infrastructure architecture documentation - Update database setup guide for PostgreSQL-only - Expand troubleshooting guide Architecture & Validation: - Add migration.yaml rules for SQL compatibility checking - Enhance validate_architecture.py with migration validation - Update architecture rules to validate Alembic migrations Development: - Fix duplicate install-all target in Makefile - Add Celery/Redis validation to install.py script - Add docker-compose.test.yml for CI testing - Add squash_migrations.py utility script - Update tests for PostgreSQL compatibility - Improve test fixtures in conftest.py Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
57 lines
1.3 KiB
Python
57 lines
1.3 KiB
Python
# app/core/database.py
|
|
"""
|
|
Database configuration and session management.
|
|
|
|
This module provides classes and functions for:
|
|
- PostgreSQL database engine creation and configuration
|
|
- Session management with connection pooling
|
|
- Database dependency for FastAPI routes
|
|
|
|
Note: This project uses PostgreSQL only. SQLite is not supported.
|
|
"""
|
|
|
|
import logging
|
|
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import declarative_base, sessionmaker
|
|
from sqlalchemy.pool import QueuePool
|
|
|
|
from .config import settings, validate_database_url
|
|
|
|
# Validate database URL on import
|
|
validate_database_url()
|
|
|
|
# Create PostgreSQL engine with connection pooling
|
|
engine = create_engine(
|
|
settings.database_url,
|
|
poolclass=QueuePool,
|
|
pool_size=10,
|
|
max_overflow=20,
|
|
pool_pre_ping=True,
|
|
echo=False,
|
|
)
|
|
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
Base = declarative_base()
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def get_db():
|
|
"""
|
|
Database session dependency for FastAPI routes.
|
|
|
|
Yields a database session and ensures proper cleanup.
|
|
Handles exceptions and rolls back transactions on error.
|
|
"""
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
except Exception as e:
|
|
logger.error(f"Database session error: {e}")
|
|
db.rollback()
|
|
raise
|
|
finally:
|
|
db.close()
|