Maxim Snesarev 33d6bb7ebd Refactor AMO CRM Data Collection Service to use PostgreSQL
- Updated database configuration to switch from SQLite to PostgreSQL, including changes to alembic.ini, Docker Compose, and environment settings.
- Refactored application code to utilize PostgreSQL database adapters, ensuring compatibility with the new database structure.
- Enhanced API routes and data handling to support the new database, including adjustments in data models and query logic.
- Introduced new job processing mechanisms for full synchronization of AMO CRM entities, leveraging FastStream for background tasks.
- Improved logging and error handling across the application to facilitate better monitoring and debugging.
- Removed obsolete SQLite adapter files and migrations, streamlining the project structure for PostgreSQL integration.
2025-11-05 00:38:37 +03:00

43 lines
1.3 KiB
Python

from typing import Generator
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, Session
from sqlalchemy.pool import QueuePool
from utils.config import settings
# Create PostgreSQL engine with proper configuration
engine = create_engine(
settings.DATABASE_URL,
poolclass=QueuePool,
pool_size=20, # Maximum number of connections to keep open
max_overflow=10, # Maximum number of connections that can be created beyond pool_size
pool_timeout=30, # Timeout for getting connection from pool
pool_pre_ping=True, # Enable connection health checks
echo=settings.LOG_LEVEL == "DEBUG", # Log SQL queries in debug mode
)
# Create session factory
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Create base class for models
Base = declarative_base()
def get_db() -> Generator[Session, None, None]:
"""Dependency to get database session"""
db = SessionLocal()
try:
yield db
finally:
db.close()
async def init_db() -> None:
"""Initialize database tables"""
# Import all models to ensure they're registered
from adapters.postgres import models # noqa: F401
# Create all tables
Base.metadata.create_all(bind=engine)