Refactor Makefile to streamline database commands

- Remove db-init target from db-seed and db-reset dependencies to simplify the seed process.
- Update .PHONY targets for clarity and maintainability.
- Enhance comments for better understanding of database operations.
This commit is contained in:
Xin Wang
2026-07-07 21:52:40 +08:00
parent 5f71bf1681
commit f00d50dc81

View File

@@ -7,7 +7,7 @@
PSQL = docker compose exec -T postgres psql -U postgres -d postgres
.DEFAULT_GOAL := help
.PHONY: help up down restart logs api-logs db db-list db-init db-seed db-seed-model-resources db-seed-assistants db-clear db-reset
.PHONY: help up down restart logs api-logs db db-list db-seed db-seed-model-resources db-seed-assistants db-clear db-reset
help: ## 列出所有可用目标
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) \
@@ -33,11 +33,6 @@ api-logs: ## 只看后端日志
db: ## 进入交互式 psql
docker compose exec postgres psql -U postgres -d postgres
db-init: ## 启动 API 并等待建表完成(表由 init_db/create_all 在 API 启动时创建)
docker compose up -d postgres api
@echo "Waiting for API health (schema init)..."
@until docker compose exec -T api python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')" 2>/dev/null; do sleep 1; done
db-list: ## 列出模型资源与助手
@$(PSQL) -c "SELECT id, name, capability, interface_type, is_default FROM model_resources ORDER BY id;"
@$(PSQL) -c "SELECT id, name, type FROM assistants ORDER BY id;"
@@ -48,9 +43,9 @@ db-seed-model-resources: ## 灌入 12 条模型资源种子(幂等)
db-seed-assistants: ## 灌入 知识库 + 助手 种子(幂等;依赖模型资源已就绪)
$(PSQL) < backend/db/seed_assistants.sql
db-seed: db-init db-seed-model-resources db-seed-assistants ## 全量灌种子(模型资源→知识库→助手,幂等,可重复执行)
db-seed: db-seed-model-resources db-seed-assistants ## 全量灌种子(模型资源→知识库→助手,幂等,可重复执行)
db-clear: ## 清空 助手/知识库/模型资源(按依赖顺序;表不存在时跳过)
@$(PSQL) -c "DO \$$\$$ BEGIN IF EXISTS (SELECT FROM pg_tables WHERE schemaname = 'public' AND tablename = 'model_resources') THEN TRUNCATE assistant_model_bindings, assistants, knowledge_bases, model_resources CASCADE; END IF; END \$$\$$;"
db-reset: db-init db-clear db-seed ## 清空后重新灌全部种子(空库时仅建表+灌种)
db-reset: db-clear db-seed ## 清空后重新灌全部种子