File size: 9,140 Bytes
a274daf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
"""
Database configuration for BackgroundFX Pro.
Handles PostgreSQL, MongoDB, and other database connections.
"""
from dataclasses import dataclass, field
from typing import Optional, Dict, Any
from sqlalchemy import create_engine, Engine
from sqlalchemy.orm import sessionmaker, Session
from sqlalchemy.pool import QueuePool, NullPool
import pymongo
from pymongo import MongoClient
import logging
logger = logging.getLogger(__name__)
@dataclass
class DatabaseConfig:
"""Database configuration settings."""
# PostgreSQL settings
postgres_url: str = "postgresql://user:password@localhost/backgroundfx"
postgres_pool_size: int = 20
postgres_max_overflow: int = 40
postgres_pool_timeout: int = 30
postgres_pool_recycle: int = 3600
postgres_echo: bool = False
postgres_echo_pool: bool = False
# MongoDB settings
mongodb_url: str = "mongodb://localhost:27017/backgroundfx"
mongodb_database: str = "backgroundfx"
mongodb_max_pool_size: int = 50
mongodb_min_pool_size: int = 10
mongodb_max_idle_time: int = 60000 # milliseconds
mongodb_server_selection_timeout: int = 30000 # milliseconds
# Connection retry settings
max_retries: int = 3
retry_delay: int = 1 # seconds
# Query settings
query_timeout: int = 30 # seconds
slow_query_threshold: float = 1.0 # seconds
log_slow_queries: bool = True
# Migration settings
auto_migrate: bool = False
migration_directory: str = "migrations"
class PostgreSQLManager:
"""Manages PostgreSQL database connections."""
def __init__(self, config: DatabaseConfig):
self.config = config
self._engine: Optional[Engine] = None
self._session_factory: Optional[sessionmaker] = None
@property
def engine(self) -> Engine:
"""Get or create database engine."""
if self._engine is None:
self._engine = self._create_engine()
return self._engine
@property
def session_factory(self) -> sessionmaker:
"""Get or create session factory."""
if self._session_factory is None:
self._session_factory = sessionmaker(
bind=self.engine,
expire_on_commit=False
)
return self._session_factory
def _create_engine(self) -> Engine:
"""Create SQLAlchemy engine with connection pooling."""
# Determine pool class based on environment
if self.config.postgres_pool_size == 0:
poolclass = NullPool
pool_kwargs = {}
else:
poolclass = QueuePool
pool_kwargs = {
'pool_size': self.config.postgres_pool_size,
'max_overflow': self.config.postgres_max_overflow,
'pool_timeout': self.config.postgres_pool_timeout,
'pool_recycle': self.config.postgres_pool_recycle,
}
engine = create_engine(
self.config.postgres_url,
poolclass=poolclass,
echo=self.config.postgres_echo,
echo_pool=self.config.postgres_echo_pool,
pool_pre_ping=True, # Verify connections before using
**pool_kwargs
)
logger.info(f"PostgreSQL engine created with pool size: {self.config.postgres_pool_size}")
return engine
def get_session(self) -> Session:
"""Get a new database session."""
return self.session_factory()
def healthcheck(self) -> bool:
"""Check database health."""
try:
with self.engine.connect() as conn:
result = conn.execute("SELECT 1")
return result.scalar() == 1
except Exception as e:
logger.error(f"PostgreSQL healthcheck failed: {e}")
return False
def close(self):
"""Close all database connections."""
if self._engine:
self._engine.dispose()
logger.info("PostgreSQL connections closed")
class MongoDBManager:
"""Manages MongoDB connections."""
def __init__(self, config: DatabaseConfig):
self.config = config
self._client: Optional[MongoClient] = None
self._database = None
@property
def client(self) -> MongoClient:
"""Get or create MongoDB client."""
if self._client is None:
self._client = self._create_client()
return self._client
@property
def database(self):
"""Get MongoDB database."""
if self._database is None:
self._database = self.client[self.config.mongodb_database]
return self._database
def _create_client(self) -> MongoClient:
"""Create MongoDB client with connection pooling."""
client = MongoClient(
self.config.mongodb_url,
maxPoolSize=self.config.mongodb_max_pool_size,
minPoolSize=self.config.mongodb_min_pool_size,
maxIdleTimeMS=self.config.mongodb_max_idle_time,
serverSelectionTimeoutMS=self.config.mongodb_server_selection_timeout,
retryWrites=True,
retryReads=True
)
# Verify connection
client.admin.command('ping')
logger.info(f"MongoDB client created for database: {self.config.mongodb_database}")
return client
def get_collection(self, name: str):
"""Get a MongoDB collection."""
return self.database[name]
def healthcheck(self) -> bool:
"""Check MongoDB health."""
try:
self.client.admin.command('ping')
return True
except Exception as e:
logger.error(f"MongoDB healthcheck failed: {e}")
return False
def close(self):
"""Close MongoDB connection."""
if self._client:
self._client.close()
logger.info("MongoDB connection closed")
class DatabaseManager:
"""Central database manager for all database connections."""
def __init__(self, config: DatabaseConfig):
self.config = config
self.postgres = PostgreSQLManager(config)
self.mongodb = MongoDBManager(config)
def initialize(self):
"""Initialize all database connections."""
logger.info("Initializing database connections...")
# Initialize PostgreSQL
if self.postgres.healthcheck():
logger.info("PostgreSQL connection established")
else:
logger.error("Failed to connect to PostgreSQL")
# Initialize MongoDB
if self.mongodb.healthcheck():
logger.info("MongoDB connection established")
else:
logger.error("Failed to connect to MongoDB")
def healthcheck(self) -> Dict[str, bool]:
"""Check health of all databases."""
return {
'postgresql': self.postgres.healthcheck(),
'mongodb': self.mongodb.healthcheck()
}
def close_all(self):
"""Close all database connections."""
self.postgres.close()
self.mongodb.close()
logger.info("All database connections closed")
def get_stats(self) -> Dict[str, Any]:
"""Get database statistics."""
stats = {}
# PostgreSQL stats
if self.postgres._engine:
pool = self.postgres.engine.pool
stats['postgresql'] = {
'size': pool.size() if hasattr(pool, 'size') else None,
'checked_in': pool.checkedin() if hasattr(pool, 'checkedin') else None,
'overflow': pool.overflow() if hasattr(pool, 'overflow') else None,
'total': pool.total() if hasattr(pool, 'total') else None
}
# MongoDB stats
if self.mongodb._client:
stats['mongodb'] = self.mongodb.client.server_info()
return stats
def create_database_config(settings: Any) -> DatabaseConfig:
"""
Create database configuration from settings.
Args:
settings: Application settings
Returns:
DatabaseConfig instance
"""
return DatabaseConfig(
postgres_url=settings.database_url,
postgres_pool_size=settings.database_pool_size,
postgres_max_overflow=settings.database_max_overflow,
postgres_echo=settings.database_echo,
mongodb_url=settings.mongodb_url,
mongodb_database=settings.mongodb_database
)
# Global database manager instance
db_manager: Optional[DatabaseManager] = None
def initialize_databases(settings: Any):
"""
Initialize global database manager.
Args:
settings: Application settings
"""
global db_manager
config = create_database_config(settings)
db_manager = DatabaseManager(config)
db_manager.initialize()
return db_manager
def get_db_manager() -> DatabaseManager:
"""Get global database manager instance."""
if db_manager is None:
raise RuntimeError("Database manager not initialized")
return db_manager |