Spaces:
Paused
Paused
| """ | |
| 数据库连接模块 | |
| SQLite 异步连接配置和会话管理 | |
| """ | |
| from sqlalchemy import text | |
| from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker | |
| from sqlalchemy.orm import declarative_base | |
| from app.config import settings | |
| # 创建异步引擎 | |
| engine = create_async_engine( | |
| settings.database_url, | |
| echo=settings.database_echo, # 控制是否打印 SQL | |
| future=True, | |
| connect_args={"timeout": 60}, # 增大连接超时 | |
| pool_size=50, # 基准连接池大小 | |
| max_overflow=100, # 最大允许溢出的连接数 | |
| pool_recycle=3600, # 每小时回收连接防止失效 | |
| pool_pre_ping=True # 每次使用连接前进行活跃性检测 | |
| ) | |
| # 创建异步会话工厂 | |
| AsyncSessionLocal = async_sessionmaker( | |
| engine, | |
| class_=AsyncSession, | |
| expire_on_commit=False, | |
| autocommit=False, | |
| autoflush=False | |
| ) | |
| # 创建 Base 类 | |
| Base = declarative_base() | |
| async def get_db() -> AsyncSession: | |
| """ | |
| 获取数据库会话 | |
| 用于 FastAPI 依赖注入 | |
| """ | |
| async with AsyncSessionLocal() as session: | |
| try: | |
| yield session | |
| finally: | |
| await session.close() | |
| async def init_db(): | |
| """ | |
| 初始化数据库 | |
| 创建所有表 | |
| """ | |
| async with engine.begin() as conn: | |
| await conn.execute(text("PRAGMA journal_mode=WAL")) | |
| await conn.run_sync(Base.metadata.create_all) | |
| async def close_db(): | |
| """ | |
| 关闭数据库连接 | |
| """ | |
| await engine.dispose() | |