feat(backend): add automatic DB migrations

Add a lightweight migration runner with schema_migrations tracking, run pending migrations during backend startup before the scheduler, and keep a manual backend-migrate entrypoint.

The change also moves the existing lockout and task-thread-ID schema steps into shared migration modules, updates docs, and archives the OpenSpec change.
This commit is contained in:
2026-05-05 01:36:58 +08:00
parent e243dccfd7
commit 3ab845798d
21 changed files with 911 additions and 145 deletions
+9
View File
@@ -8,6 +8,7 @@ import logging
from pathlib import Path
from backend.config import settings
from backend.migrations import run_pending_migrations
from backend.models import init_db
from backend.exceptions import BaseAPIException
from backend.schemas.response import ErrorResponse, ErrorDetail
@@ -37,6 +38,14 @@ async def lifespan(app: FastAPI):
init_db()
logger.info("数据库初始化完成")
logger.info("正在执行数据库迁移...")
migration_result = run_pending_migrations()
logger.info(
"数据库迁移完成:applied=%s skipped=%s",
len(migration_result.applied),
len(migration_result.skipped),
)
# 确保必要的目录存在
settings.SESSION_DIR.mkdir(parents=True, exist_ok=True)
(settings.BASE_DIR / "data").mkdir(parents=True, exist_ok=True)