Spaces:
Paused
Paused
Upload database.py
Browse files- app/db/database.py +59 -68
app/db/database.py
CHANGED
|
@@ -1,68 +1,59 @@
|
|
| 1 |
-
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
|
| 2 |
-
from sqlalchemy.orm import declarative_base
|
| 3 |
-
from ..core.config import settings
|
| 4 |
-
import contextlib
|
| 5 |
-
|
| 6 |
-
# Create async engine with pgbouncer configuration
|
| 7 |
-
async_engine = create_async_engine(
|
| 8 |
-
"postgresql+asyncpg://postgres.mqyrkmsdgugdhxiucukb:Lovyelias5584.@
|
| 9 |
-
echo=True,
|
| 10 |
-
future=True,
|
| 11 |
-
pool_pre_ping=True,
|
| 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 |
-
finally:
|
| 61 |
-
await session.close()
|
| 62 |
-
|
| 63 |
-
async def get_session(self):
|
| 64 |
-
"""Get a session for manual management"""
|
| 65 |
-
return self._session_factory()
|
| 66 |
-
|
| 67 |
-
# Create singleton instance for database access
|
| 68 |
-
db = Database()
|
|
|
|
| 1 |
+
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
|
| 2 |
+
from sqlalchemy.orm import declarative_base
|
| 3 |
+
from ..core.config import settings
|
| 4 |
+
import contextlib
|
| 5 |
+
|
| 6 |
+
# Create async engine with pgbouncer configuration
|
| 7 |
+
async_engine = create_async_engine(
|
| 8 |
+
"postgresql+asyncpg://postgres.mqyrkmsdgugdhxiucukb:Lovyelias5584.@aws-0-eu-central-1.pooler.supabase.com:6543/postgres",
|
| 9 |
+
echo=True,
|
| 10 |
+
future=True,
|
| 11 |
+
pool_pre_ping=True,
|
| 12 |
+
connect_args={
|
| 13 |
+
"statement_cache_size": 0, # Required for pgbouncer
|
| 14 |
+
"prepared_statement_cache_size": 0 # Disable prepared statement cache
|
| 15 |
+
}
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
AsyncSessionLocal = async_sessionmaker(
|
| 20 |
+
bind=async_engine,
|
| 21 |
+
class_=AsyncSession,
|
| 22 |
+
expire_on_commit=False
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
Base = declarative_base()
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
async def get_db():
|
| 30 |
+
async with AsyncSessionLocal() as session:
|
| 31 |
+
try:
|
| 32 |
+
yield session
|
| 33 |
+
finally:
|
| 34 |
+
await session.close()
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
class Database:
|
| 38 |
+
def __init__(self):
|
| 39 |
+
self._session_factory = AsyncSessionLocal
|
| 40 |
+
|
| 41 |
+
@contextlib.asynccontextmanager
|
| 42 |
+
async def session(self):
|
| 43 |
+
"""Get a database session with automatic commit/rollback"""
|
| 44 |
+
session = self._session_factory()
|
| 45 |
+
try:
|
| 46 |
+
yield session
|
| 47 |
+
await session.commit()
|
| 48 |
+
except:
|
| 49 |
+
await session.rollback()
|
| 50 |
+
raise
|
| 51 |
+
finally:
|
| 52 |
+
await session.close()
|
| 53 |
+
|
| 54 |
+
async def get_session(self):
|
| 55 |
+
"""Get a session for manual management"""
|
| 56 |
+
return self._session_factory()
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
db = Database()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|