Files
hotime/.cursor/plans/修复路径和测试数据_af161d0a.plan.md
T
hoteas b43f968b6c feat(db): 添加对达梦数据库的支持
- 在应用程序中新增对达梦数据库(DM)的配置和连接支持
- 实现 SetDmDB 函数以配置达梦数据库连接
- 更新数据库操作逻辑,支持达梦特有的 SQL 语法和功能
- 在相关文件中添加达梦数据库的处理逻辑,包括表创建、数据插入和查询
- 更新 go.mod 和 go.sum 文件以引入达梦数据库驱动
- 增强文档,详细说明达梦数据库的配置和使用方法
2026-03-20 10:46:51 +08:00

3.7 KiB
Raw Blame History

name, overview, todos, isProject
name overview todos isProject
修复路径和测试数据 修复测试时 swagger/config 生成到错误目录的问题(根因是 Go test CWD 在 app/ 下),并为 DM 和 MySQL 两个测试数据库创建完善的测试数据表和种子数据。
id content status
fix-cwd 修复 app_test.go 中的工作目录问题:添加 os.Chdir("..") 并修改 config path completed
id content status
cleanup-wrong-dirs 清理错误生成的 example/app/tpt/ 和 example/app/config/ 目录 completed
id content status
setup-mysql 创建 setup_mysql.goMySQL DDL 建表逻辑(与 setup_dm.go 对应) completed
id content status
extract-seed 提取通用种子数据函数 seedTestDataDM 和 MySQL 共用 completed
id content status
update-testmain 更新 app_test.go 增加 SetupMySQLDatabase 调用 completed
id content status
sql-scripts 在 example/sql/ 下提供 MySQL 和 DM 的原始 DDL+DML SQL 脚本 completed
id content status
verify-tests 运行测试验证 swagger 输出到正确位置,且 DM 测试全部通过 completed
false

修复 Swagger 路径问题 + 完善双数据库测试数据

问题 1Swagger 和 config 生成到错误目录

根因go test ./app/... 时,Go test 将工作目录设为包目录 example/app/。配置文件中 "tpt": "tpt"codeConfig 中的 "config/admin.json" 等相对路径,都从 example/app/ 解析,导致文件生成到 example/app/tpt/example/app/config/ 而非预期的 example/tpt/example/config/

修复方案:在 example/app/app_test.goTestMain 中,NewTestApp 之前先 os.Chdir(".."),将工作目录切回 example/,然后将配置路径从 "../config/config.json" 改为 "config/config.json"

func TestMain(m *testing.M) {
    os.Chdir("..") // 切回 example/ 目录,使 tpt、config 等相对路径正确解析
    testApp = NewTestApp("config/config.json", ...)
    ...
}

同时清理已错误生成的 example/app/tpt/example/app/config/ 目录。

问题 2:双数据库测试数据

现有 example/app/setup_dm.go 仅处理 DM。需要为 MySQL 创建类似的初始化逻辑,并使种子数据逻辑可复用。

数据库结构(DM 和 MySQL 共用同一套表):

  • admin — 管理员表(id, name, phone, state, password, role_id, title, create_time, modify_time
  • ctg — 分类表(id, name, state, create_time, modify_time
  • article — 文章表(id, title, author, content, state, click_num, sort, img, ctg_id, admin_id, create_time, modify_time
  • test_batch — 批量测试表(id, name, title, state, create_time
  • cached — 老版本缓存表(id, key, value, endtime, time
  • hotime_cache — 新版缓存表(id, key, value, end_time, state, create_time, modify_time

方案:

  1. 创建 example/app/setup_mysql.go:包含 MySQL DDL 建表和种子数据灌入,类似 setup_dm.go 的结构
  2. example/app/app_test.goTestMain 中增加 SetupMySQLDatabase(&testApp.Db) 调用(根据 db.Type 自动判断是否执行)
  3. 种子数据使用 ORM 的 db.Insert() 方法,与数据库类型无关,可直接复用现有 setup_dm.goseedDMData 的逻辑,提取为通用的 seedTestData 函数
  4. 同时提供 MySQL 和 DM 的原始 DDL/DML SQL 脚本,放在 example/sql/ 目录下,方便手动执行

config.json 切换方式:当前 MySQL 的 key 是 "mysql-"(带尾缀表示禁用),DM 的 key 是 "dm"(活跃)。切换数据库只需互换后缀:"mysql" / "dm-"