feat: 添加 SQLAlchemy 模型 + Alembic 数据库迁移

This commit is contained in:
2026-05-09 13:35:08 +08:00
parent 1cdd0bcab1
commit a994dc5f53
9 changed files with 513 additions and 0 deletions
+11
View File
@@ -1,7 +1,17 @@
import logging
from contextlib import asynccontextmanager
from fastapi import FastAPI
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
from app.config import settings
from app.models.announcement import Base
engine = create_async_engine(settings.database_url, echo=settings.debug)
async_session = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
async def get_db() -> AsyncSession:
async with async_session() as session:
yield session
@asynccontextmanager
@@ -11,6 +21,7 @@ async def lifespan(app: FastAPI):
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
)
yield
await engine.dispose()
app = FastAPI(