feat(g3fo-db-ops): replace MCP MySQL with Python db_ops tool, add connection timeout

Made-with: Cursor
This commit is contained in:
2026-03-11 09:27:19 +08:00
parent 8e2afda170
commit 23df8ffd75
5 changed files with 634 additions and 26 deletions
+93 -14
View File
@@ -1,6 +1,6 @@
---
name: g3fo-db-ops
description: Comprehensive database operations guide for G3FO project. Use when updating G3FO database data, creating tables, modifying table structures, inserting system menus/routes, or managing database changes that require SQL generation, execution, and Git commit workflows. Includes automated workflows for i18n updates, version management, file naming conventions, and duplicate sequence number detection.
description: Comprehensive database operations guide for G3FO project. Use when updating G3FO database data, creating tables, modifying table structures, inserting system menus/routes, or managing database changes that require SQL generation, execution, and Git commit workflows. Includes automated workflows for i18n updates, version management, file naming conventions, and duplicate sequence number detection. Uses Python db_ops tool (not MCP MySQL) for database operations.
---
# G3FO Database Operations
@@ -9,6 +9,85 @@ Guide for performing database operations in the G3FO project, including automate
**Detailed documentation**: See `references/DATABASE_UPDATE_AUTOMATION_GUIDE.md` for complete automation guide.
## Database Tool (db_ops.py)
**本技能不再使用 MCP MySQL**,改用 Python 程序 `scripts/db_ops.py` 执行所有数据库操作。
### 安装依赖
```bash
pip install -r .claude/skills/g3fo-db-ops/scripts/requirements.txt
```
### 默认连接配置
与 MCP user-mysql 一致,未指定时自动使用:
| 参数 | 默认值 |
|------|--------|
| host | 192.168.3.233 |
| port | 3306 |
| user | root |
| password | afe123456 |
| database | g3fo_base |
**切换数据库**:系统表用 `g3fo_base`,交易相关用 `g3fo_trade`。通过 `--database g3fo_trade` 或环境变量 `MYSQL_DATABASE` 覆盖。
### 覆盖连接方式
1. **命令行参数**:`--host`, `--port`, `--user`, `--password`, `--database`
2. **环境变量**:`MYSQL_HOST`, `MYSQL_PORT`, `MYSQL_USER`, `MYSQL_PASSWORD`, `MYSQL_DATABASE`
### 命令列表
| 命令 | 说明 |
|------|------|
| `query` | 执行 SELECT 查询 |
| `execute` | 执行 INSERT/UPDATE/DELETE |
| `list_tables` | 列举数据库所有表 |
| `describe_table` | 获取表结构(列信息、表注释) |
| `call_procedure` | 执行存储过程 |
| `batch_execute` | 批量执行 SQL(支持文件、DELIMITER) |
| `get_table_comment` | 获取表注释 |
| `set_table_comment` | 修改表注释 |
| `get_column_comment` | 获取列注释 |
| `set_column_comment` | 修改列注释 |
| `test_connection` | 测试连接 |
### 使用示例
```bash
# 从 skill 根目录或 server 根目录执行,base_dir 为 .claude/skills/g3fo-db-ops 所在路径
# 测试连接
python scripts/db_ops.py test_connection
python scripts/db_ops.py --database g3fo_trade test_connection
# 查询
python scripts/db_ops.py query "SELECT * FROM m_system_code LIMIT 5"
# 执行增删改
python scripts/db_ops.py execute "REPLACE INTO g3fo_base.m_system_code (...) VALUES (...)"
# 列举所有表
python scripts/db_ops.py list_tables
python scripts/db_ops.py --database g3fo_trade list_tables
# 获取表结构
python scripts/db_ops.py describe_table m_system_code
# 执行存储过程
python scripts/db_ops.py call_procedure InsertSystemMenu --params "菜单备注" "menu-name" "title_remark" "ADMIN"
# 批量执行 SQL 文件
python scripts/db_ops.py batch_execute --file path/to/script.sql
# 修改表注释
python scripts/db_ops.py set_table_comment m_system_code "系统编码表"
```
**在 Cursor 中调用时**:使用 `run_terminal_cmd` 或 Shell 工具执行上述命令,工作目录为 `d:\AFE Git\G3SF\G3FO\server`,脚本路径为 `.claude/skills/g3fo-db-ops/scripts/db_ops.py`。
## Core Principles
1. **Use existing templates**: Prioritize templates and tools in `g3fo-db/common_sql/`
@@ -72,16 +151,16 @@ Display generated SQL and ask:
### 4. SQL Execution Phase
If user confirms:
1. Connect to database using MySQL MCP
- **Default database connection** (automatically use when MySQL MCP or database connection is needed):
- host: 192.168.3.233
- user: root
- password: afe123456
- database: g3fo_base (for system tables like m_system_code, m_system_i18n, m_system_error_message, etc.)
- database: g3fo_trade (for trade-related tables)
2. Execute SQL
3. Verify results
4. If failed, report error and stop
1. Use **db_ops.py** to execute SQL (run in terminal):
- **Default connection** (see "Database Tool" section above): host 192.168.3.233, user root, password afe123456
- System tables: `--database g3fo_base`
- Trade tables: `--database g3fo_trade`
- Override via `--host`, `--port`, `--user`, `--password`, `--database` if user provides different connection info
2. For single SELECT: `python .claude/skills/g3fo-db-ops/scripts/db_ops.py query "SQL"`
3. For INSERT/UPDATE/DELETE: `python .claude/skills/g3fo-db-ops/scripts/db_ops.py execute "SQL"`
4. For batch SQL or file: `python .claude/skills/g3fo-db-ops/scripts/db_ops.py batch_execute --file path/to/file.sql`
5. Verify results (check JSON output for `"success": true`)
6. If failed, report error and stop
### 5. Git Commit Phase
@@ -195,9 +274,9 @@ If confirmed, **execute in this order** to ensure remote repository and local da
**Execution and commit rules:**
1. **SQL execution phase**:
- MySQL MCP doesn't support `DELIMITER` for stored procedures
- Can use direct `INSERT INTO` or `REPLACE INTO` to insert same data
- Ensure data matches stored procedure output exactly
- Use **db_ops.py** `batch_execute --file` for SQL files with `DELIMITER` (script handles it)
- Or use `call_procedure` for stored procedure calls
- Or use `execute` with direct `INSERT INTO` / `REPLACE INTO` if data matches procedure output
2. **Git commit phase**:
- **Must use complete SQL from stored procedure file**