Implement Alembic for database migrations and update FastAPI initialization

- Integrate Alembic for managing database schema migrations, replacing the previous SQL schema management.
- Update the FastAPI application to synchronize interface definitions at startup.
- Modify the Docker Compose command to run Alembic migrations before starting the API.
- Enhance the Makefile with new commands for database migration and revision management.
- Remove outdated SQL schema and seed files, transitioning to a more dynamic migration approach.
- Add initial migration scripts and configuration for Alembic, ensuring a structured database evolution.
This commit is contained in:
Xin Wang
2026-07-09 16:29:10 +08:00
parent 54329aa516
commit 03b532dd09
16 changed files with 325 additions and 247 deletions

View File

@@ -98,6 +98,9 @@ uv pip install fastapi "uvicorn[standard]" sqlalchemy asyncpg greenlet python-do
cp .env.example .env # CRUD 阶段只需 DATABASE_URL;语音再填模型 key
# 起 Postgres:在 ai-video/ 下 docker compose up -d postgres
cd ..
make db-migrate # 首次或表结构变更后执行 Alembic 迁移
cd backend
uv run --with-requirements requirements.txt uvicorn app:app --reload --port 8000
```
@@ -106,6 +109,21 @@ uv run --with-requirements requirements.txt uvicorn app:app --reload --port 8000
>
> 交互式 API 文档:启动后访问 http://localhost:8000/docs(手动戳 CRUD、定 schema 用)。
## 数据库迁移
表结构由 Alembic 管理SQLAlchemy 模型在 `db/models.py`,迁移文件在
`migrations/versions/`
```bash
cd ai-video
make db-migrate # 升级到最新版
make db-revision msg="add xxx" # 根据模型差异生成迁移
make db-current # 查看当前库版本
```
`interface_definitions` 的默认数据由 `services/interface_catalog.py` 维护,并通过
启动时同步或 `make db-sync-interface-definitions` 写入数据库;模型资源、知识库和助手示例数据继续走 `db/seed_*.sql`
## Docker(ai-video/docker-compose.yaml)—— 调试主路径
api 服务挂了源码 + `--reload`,前端用 npm dev + HMR,改代码都即时生效。