chore: PostgreSQL migration compatibility and infrastructure improvements
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>
This commit is contained in:
@@ -3,45 +3,33 @@
|
||||
Database configuration and session management.
|
||||
|
||||
This module provides classes and functions for:
|
||||
- Database engine creation and configuration
|
||||
- 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, event
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import declarative_base, sessionmaker
|
||||
from sqlalchemy.pool import QueuePool
|
||||
|
||||
from .config import settings
|
||||
from .config import settings, validate_database_url
|
||||
|
||||
# Validate database URL on import
|
||||
validate_database_url()
|
||||
|
||||
def _configure_sqlite_connection(dbapi_connection, connection_record):
|
||||
"""Configure SQLite connection for better concurrency.
|
||||
|
||||
- WAL mode: Allows concurrent reads during writes
|
||||
- busy_timeout: Wait up to 30 seconds if database is locked
|
||||
- synchronous=NORMAL: Balance between safety and performance
|
||||
"""
|
||||
cursor = dbapi_connection.cursor()
|
||||
cursor.execute("PRAGMA journal_mode=WAL")
|
||||
cursor.execute("PRAGMA busy_timeout=30000")
|
||||
cursor.execute("PRAGMA synchronous=NORMAL")
|
||||
cursor.close()
|
||||
|
||||
|
||||
# Create engine with SQLite-specific configuration
|
||||
engine_kwargs = {}
|
||||
|
||||
# Add SQLite-specific settings for better concurrent access
|
||||
if settings.database_url.startswith("sqlite"):
|
||||
engine_kwargs["connect_args"] = {"check_same_thread": False}
|
||||
|
||||
engine = create_engine(settings.database_url, **engine_kwargs)
|
||||
|
||||
# Configure SQLite pragmas on connection
|
||||
if settings.database_url.startswith("sqlite"):
|
||||
event.listen(engine, "connect", _configure_sqlite_connection)
|
||||
# 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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user