3.6 KiB
3.6 KiB
MCP Tools: code-review-graph
IMPORTANT: This project has a knowledge graph. ALWAYS use the code-review-graph MCP tools BEFORE using Grep/Glob/Read to explore the codebase. The graph is faster, cheaper (fewer tokens), and gives you structural context (callers, dependents, test coverage) that file scanning cannot.
When to use graph tools FIRST
- Exploring code:
semantic_search_nodesorquery_graphinstead of Grep - Understanding impact:
get_impact_radiusinstead of manually tracing imports - Code review:
detect_changes+get_review_contextinstead of reading entire files - Finding relationships:
query_graphwith callers_of/callees_of/imports_of/tests_for - Architecture questions:
get_architecture_overview+list_communities
Fall back to Grep/Glob/Read only when the graph doesn't cover what you need.
Key Tools
| Tool | Use when |
|---|---|
detect_changes |
Reviewing code changes — gives risk-scored analysis |
get_review_context |
Need source snippets for review — token-efficient |
get_impact_radius |
Understanding blast radius of a change |
get_affected_flows |
Finding which execution paths are impacted |
query_graph |
Tracing callers, callees, imports, tests, dependencies |
semantic_search_nodes |
Finding functions/classes by name or keyword |
get_architecture_overview |
Understanding high-level codebase structure |
refactor_tool |
Planning renames, finding dead code |
Workflow
- The graph auto-updates on file changes (via hooks).
- Use
detect_changesfor code review. - Use
get_affected_flowsto understand impact. - Use
query_graphpattern="tests_for" to check coverage.
数据库操作规范
PostgreSQL 序列同步规则(强制)
问题背景:项目使用 BIGSERIAL / autoIncrement 自增主键。当通过 SQL 手动 INSERT 指定 id 值时,PostgreSQL 序列不会自动跟进,导致后续 GORM 插入报 duplicate key value violates unique constraint。
触发场景:
- 测试数据脚本(如
create_gallery_test_users.go)硬编码 ID - 数据迁移脚本手动插入记录
- DBeaver / psql 手动补数据
规范要求:
-
任何手动指定 ID 的 INSERT 语句,末尾必须同步重置序列:
-- 错误示例(会导致序列不同步) INSERT INTO assets (id, name, ...) VALUES (1000, 'xxx', ...); -- 正确示例 INSERT INTO assets (id, name, ...) VALUES (1000, 'xxx', ...); SELECT setval('assets_id_seq', (SELECT MAX(id) FROM assets)); -
脚本文件规范:所有输出 SQL 的 Go 脚本(如
backend/scripts/*.go),必须在生成的 SQL 末尾包含序列重置语句:fmt.Printf(`SELECT setval('%s_id_seq', (SELECT MAX(id) FROM %s));\n`, tableName, tableName) -
新表创建时:预留足够的序列起始值给测试数据:
CREATE SEQUENCE assets_id_seq START WITH 10000; -
定期检查:环境部署后执行以下 SQL 确认序列健康:
SELECT schemaname, sequencename, last_value, (SELECT MAX(id) FROM assets) AS table_max_id, last_value >= (SELECT MAX(id) FROM assets) AS is_healthy FROM pg_sequences WHERE sequencename = 'assets_id_seq';
受影响表(使用 autoIncrement 主键):
assetsasset_registryusersstarsactivity_assetscollection_assetsmaterialsexhibitionsgalleries- 以及其他所有
id BIGSERIAL PRIMARY KEY的表
违规后果:生产环境报 duplicate key 导致用户铸造/创建失败,需紧急修复序列。