Files
image-suite-studio/server/db.py
T
2026-08-16 17:32:43 +08:00

58 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""数据库:SQLiteaiosqlite+ SQLAlchemy async。"""
from __future__ import annotations
from collections.abc import AsyncGenerator
from sqlalchemy import text
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from sqlalchemy.orm import DeclarativeBase
from config import get_settings
class Base(DeclarativeBase):
pass
_engine = None
_session_factory: async_sessionmaker[AsyncSession] | None = None
def get_engine():
global _engine, _session_factory
if _engine is None:
settings = get_settings()
db_path = f"{settings.data_dir}/app.db"
_engine = create_async_engine(f"sqlite+aiosqlite:///{db_path}", echo=False)
_session_factory = async_sessionmaker(_engine, expire_on_commit=False)
return _engine
def get_session_factory() -> async_sessionmaker[AsyncSession]:
get_engine()
assert _session_factory is not None
return _session_factory
async def get_db() -> AsyncGenerator[AsyncSession, None]:
async with get_session_factory()() as session:
yield session
async def _migrate(conn) -> None:
"""给旧库补充新增列(create_all 不会修改已存在的表)。"""
rows = await conn.execute(text("PRAGMA table_info(suites)"))
cols = {row[1] for row in rows}
if "model" not in cols:
await conn.execute(text("ALTER TABLE suites ADD COLUMN model VARCHAR(64)"))
async def init_db() -> None:
"""启动时建表 + 轻量列迁移(MVP 不引 Alembic)。"""
import models # noqa: F401 确保模型注册
engine = get_engine()
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
await _migrate(conn)