-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.py
More file actions
81 lines (61 loc) · 2.31 KB
/
Copy pathbase.py
File metadata and controls
81 lines (61 loc) · 2.31 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
"""SQLAlchemy base and session management."""
from __future__ import annotations
from contextlib import asynccontextmanager
from typing import AsyncIterator
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from sqlalchemy.orm import DeclarativeBase
from minikb.config.settings import Settings, get_settings
class Base(DeclarativeBase):
"""Base class for all models."""
pass
_engine = None
_session_factory = None
def get_engine(settings: Settings | None = None) -> AsyncSession:
"""Get or create the async engine."""
global _engine
if _engine is None:
s = settings or get_settings()
_engine = create_async_engine(
s.postgres_dsn,
echo=s.env == "dev",
pool_pre_ping=True,
pool_size=10,
max_overflow=20,
)
return _engine
def get_session_factory(settings: Settings | None = None) -> async_sessionmaker[AsyncSession]:
"""Get or create the session factory."""
global _session_factory
if _session_factory is None:
engine = get_engine(settings)
_session_factory = async_sessionmaker(engine, expire_on_commit=False)
return _session_factory
async def get_session() -> AsyncIterator[AsyncSession]:
"""Yield a DB session for FastAPI Depends (or manual async-for / context use)."""
factory = get_session_factory()
async with factory() as session:
try:
yield session
await session.commit()
except Exception:
await session.rollback()
raise
@asynccontextmanager
async def session_scope() -> AsyncIterator[AsyncSession]:
"""Async context manager wrapper around get_session()."""
async for session in get_session():
yield session
async def init_db(settings: Settings | None = None) -> None:
"""Create all tables (for development only)."""
from sqlalchemy import text
engine = get_engine(settings)
async with engine.begin() as conn:
await conn.execute(text("CREATE EXTENSION IF NOT EXISTS vector"))
await conn.run_sync(Base.metadata.create_all)
async def close_db() -> None:
"""Close the database engine."""
global _engine, _session_factory
if _engine is not None:
await _engine.dispose()
_engine = None
_session_factory = None