Enhance Makefile and database schema for improved management

- Add new database management targets: db-up, db-schema, db-seed-interface-definitions, and db-init to streamline database setup and seeding processes.
- Update db-seed and db-reset dependencies to include new initialization steps for better data integrity.
- Introduce new SQL files for schema definition and interface definitions seeding, ensuring a consistent database structure.
- Refactor existing seed scripts to align with new dependencies and improve clarity in database operations.
This commit is contained in:
Xin Wang
2026-07-07 22:08:42 +08:00
parent 6e4c50eee6
commit 2fb7399953
4 changed files with 226 additions and 7 deletions

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-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-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
help: ## 列出所有可用目标
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) \
@@ -37,22 +37,31 @@ 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;"
db-up: ## 仅启动 postgres
docker compose up -d postgres
db-schema: ## 用纯 SQL 建表/补齐开发库表结构
$(PSQL) < backend/db/schema.sql
db-seed-interface-definitions: ## 灌入接口定义种子(幂等)
$(PSQL) < backend/db/seed_interface_definitions.sql
db-init: db-schema db-seed-interface-definitions ## 用纯 SQL 建表并同步接口定义
db-seed-model-resources: ## 灌入 12 条模型资源种子(幂等)
$(PSQL) < backend/db/seed_model_resources.sql
db-seed-assistants: ## 灌入 知识库 + 助手 种子(幂等;依赖模型资源已就绪)
$(PSQL) < backend/db/seed_assistants.sql
db-seed: db-seed-model-resources db-seed-assistants ## 全量灌种子(模型资源→知识库→助手,幂等,可重复执行)
db-seed: db-init 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-clear db-seed ## 清空后重新灌全部种子
db-reset: db-up db-clear db-seed ## 启动库、建表/清空后重新灌全部种子
db-drop-schema: ## 删除并重建 public schema(开发用:表结构变更后会清空全部数据)
@$(PSQL) -c "DROP SCHEMA IF EXISTS public CASCADE; CREATE SCHEMA public;"
db-reset-hard: db-drop-schema restart ## 重建 schema,重启 api 让 init_db 建表,再灌种子
sleep 3
$(MAKE) db-seed
db-reset-hard: db-up db-drop-schema db-seed ## 启动库、重建 schema,建表,再灌种子