forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.py
More file actions
81 lines (63 loc) · 2.25 KB
/
Copy pathconnection.py
File metadata and controls
81 lines (63 loc) · 2.25 KB
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
"""Database connection management."""
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
from sqlalchemy.orm import declarative_base
from sqlalchemy import text
from typing import Optional
import asyncio
from app.config import get_settings
Base = declarative_base()
class DatabaseManager:
"""Manages database connections."""
_instance: Optional['DatabaseManager'] = None
_lock = asyncio.Lock()
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
cls._instance._initialized = False
return cls._instance
def __init__(self):
if self._initialized:
return
settings = get_settings()
# Convert sync connection string to async
db_url = settings.db_connection_string
if db_url.startswith("postgresql://"):
db_url = db_url.replace("postgresql://", "postgresql+asyncpg://", 1)
self.engine = create_async_engine(
db_url,
pool_size=settings.db_pool_size,
max_overflow=20,
pool_pre_ping=True,
echo=False
)
self.async_session = async_sessionmaker(
self.engine,
class_=AsyncSession,
expire_on_commit=False
)
self._initialized = True
async def get_session(self) -> AsyncSession:
"""Get a database session."""
async with self.async_session() as session:
yield session
async def health_check(self) -> bool:
"""Check database connectivity."""
try:
async with self.async_session() as session:
result = await session.execute(text("SELECT 1"))
return result.scalar() == 1
except Exception:
return False
async def close(self):
"""Close database connections."""
if self.engine:
await self.engine.dispose()
# Global instance
db_manager = DatabaseManager()
async def get_db_session():
"""Dependency for FastAPI to get database session."""
async with db_manager.async_session() as session:
try:
yield session
finally:
await session.close()