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

@@ -5,9 +5,10 @@
# 容器内执行 psql 的固定前缀(postgres/postgres/postgres,见 docker-compose.yaml)
PSQL = docker compose exec -T postgres psql -U postgres -d postgres
UV = UV_CACHE_DIR=/private/tmp/ai-video-uv-cache uv run --with-requirements requirements.txt
.DEFAULT_GOAL := help
.PHONY: help up down restart logs api-logs db db-list db-up db-schema db-seed-interface-definitions db-init db-seed db-seed-model-resources db-seed-assistants db-clear db-reset db-drop-schema db-reset-hard
.PHONY: help up down restart logs api-logs db db-list db-up db-migrate db-revision db-current db-sync-interface-definitions db-init db-seed db-seed-model-resources db-seed-assistants db-clear db-reset db-drop-schema db-reset-hard
help: ## 列出所有可用目标
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) \
@@ -40,13 +41,19 @@ db-list: ## 列出模型资源与助手
db-up: ## 仅启动 postgres
docker compose up -d postgres
db-schema: ## 用纯 SQL 建表/补齐开发库表结构
$(PSQL) < backend/db/schema.sql
db-migrate: ## 执行 Alembic 迁移到最新版
cd backend && $(UV) alembic upgrade head
db-seed-interface-definitions: ## 灌入接口定义种子(幂等)
$(PSQL) < backend/db/seed_interface_definitions.sql
db-revision: ## 自动生成 Alembic 迁移: make db-revision msg="add xxx"
cd backend && $(UV) alembic revision --autogenerate -m "$(msg)"
db-init: db-schema db-seed-interface-definitions ## 用纯 SQL 建表并同步接口定义
db-current: ## 查看当前数据库迁移版本
cd backend && $(UV) alembic current
db-sync-interface-definitions: ## 从 interface_catalog.py 同步接口定义(幂等)
cd backend && $(UV) python -m db.sync_interface_definitions
db-init: db-migrate db-sync-interface-definitions ## 用 Alembic 建表并同步接口定义
db-seed-model-resources: ## 灌入 12 条模型资源种子(幂等)
$(PSQL) < backend/db/seed_model_resources.sql