Skip to content

fix: 补齐各业务模型备注字段的长度校验 - #106

Open
xy200303 wants to merge 1 commit into
insistence:masterfrom
xy200303:fix/schedule-run-once
Open

xy200303 wants to merge 1 commit into
insistence:masterfrom
xy200303:fix/schedule-run-once

Conversation

@xy200303

@xy200303 xy200303 commented Jun 6, 2026 •

Copy link
Copy Markdown
Contributor

问题

sys_user 等业务表的 remark 列是 varchar(500),sys_notice.remark 是 varchar(255),但对应的 pydantic 模型只用 Field 声明了类型,没有任何长度约束。同一个模型里 user_name、nick_name、email、phonenumber 都已用 @Size 装饰器限长,只有 remark 漏了。

后果:超过列长的备注会一路走到数据库层——严格模式下直接报错,非严格模式(如默认的 MySQL)下被静默截断,前端拿不到任何提示。这正是 issue #50 反馈的问题。

改动

为 9 个带 remark 的业务模型补上与列长一致的 @Size 校验,并加入各自的 validate_fields()(服务层实际调用的校验入口):

模型 上限 依据
UserModel、RoleModel、PostModel、MenuModel、ConfigModel、JobModel 500 对应列均为 varchar(500)
DictTypeModel、DictDataModel 500 sys_dict_type / sys_dict_data 均为 varchar(500)
NoticeModel 255 sys_notice.remark 实际是 varchar(255)

sys_user 之外的表也一起补,是因为这是同一个缺陷模式,而 issue #50 标题里正是"字段缺少长度校验";只补用户表会留下同样的洞。各模型上限都取自已发布的建表 SQL,未凭猜测取值。

验证

新增 tests/module_admin/entity/test_vo_remark_length.py,对 9 个模型逐一断言:列长通过、列长 +1 被拒、None 通过,并覆盖 validate_fields() 入口。

  • red → green:新测试在未修复代码上 27 项失败,修复后 36 项全部通过
  • 无回归:pytest tests/module_admin 修复前后失败集合完全一致
    • 基线:8 failed / 199 passed / 1 skipped / 10 errors
    • 修复后:8 failed / 235 passed / 1 skipped / 10 errors(多出的 36 项即新测试)
    • 这些既有失败/错误是本机环境问题(缺 Redis / PostgreSQL 等),与本次改动无关

说明

本 PR 此前的内容是 "isolate run-once scheduler jobs"(修复 #31)。该修复已被 #127 的调度器重构取代——SchedulerUtil 与 get_scheduler.py 已不存在,run-once 现在通过 JobRuntimeDao.create_request + SchedulerJobs.execute_once(id=f'_manual:{execution_id}' 独立任务)实现,不再替换既有 cron 任务,原来的 bug 已不复存在。

因此我把这个 PR 重新定位到上面这个仍然存在的缺陷上,复用了原来的分支与 PR 编号,避免再开一个新 PR。相关讨论见下方评论。

Closes #50

@xy200303

Copy link
Copy Markdown
Contributor Author

I think this one can be closed rather than merged — the bug it fixes no longer exists on main.

main moved this whole area in #127 (feat: 新增跨时区支持并完善定时任务调度), which deleted ruoyi-fastapi-backend/config/get_scheduler.py and split it into the config/scheduler/ package. Concretely:

  • SchedulerUtil no longer exists anywhere in the repo.
  • The call site this PR patches, execute_job_once_services, no longer calls remove_scheduler_job before dispatching. It now does JobRuntimeDao.create_request(...) → SchedulerManager.request_execution_dispatch(), then commits.
  • Run-once dispatch registers an independent scheduler job with its own id: SchedulerJobs.execute_once uses id=f'_manual:{execution_id}' with TaskDateTrigger(...) and replace_existing=True. Because the id is namespaced, it cannot replace the recurring cron job, which is exactly what this PR set out to guarantee.
  • The temporary run-once job also cannot be reaped by the DB sync pass, since it is a distinct _manual: entry rather than the job_id itself.

So the "run-once replaces the existing cron job" behaviour this PR targets is gone, and the accompanying test target (get_scheduler.py) no longer exists either. If per-run isolation still has a gap under the new scheduler package, I'd rather re-scope a fresh change against config/scheduler/ than carry this diff forward.

Happy to close it if you agree.

@xy200303
xy200303 force-pushed the fix/schedule-run-once branch from 72bafff to 66e48ff Compare September 15, 2026 09:05
sys_user 等表的 remark 列为 varchar(500),sys_notice 为 varchar(255),但对应的
pydantic 模型只用 Field 声明了类型,没有任何长度约束,而同一模型里 user_name、
nick_name、email、phonenumber 等都已有 @SiZe 装饰器。结果是超过列长的备注会一路
走到数据库层:严格模式下报错,非严格模式下被静默截断,前端拿不到任何提示。

为 9 个带 remark 的业务模型补上与列长一致的 @SiZe 校验,并加入各自的
validate_fields()(服务层调用的入口):

- UserModel、RoleModel、PostModel、MenuModel、ConfigModel、JobModel 上限 500
- DictTypeModel、DictDataModel 上限 500
- NoticeModel 上限 255(sys_notice.remark 实际是 varchar(255))

新增 tests/module_admin/entity/test_vo_remark_length.py:对每个模型断言
列长通过、列长 +1 被拒、None 通过,并覆盖 validate_fields() 入口。

验证:
- 新测试在修复前 27 项失败、修复后 36 项通过(red -> green)
- tests/module_admin 全量:修复前后失败/错误集合完全一致
  (基线 8 failed / 199 passed / 1 skipped / 10 errors;
   修复后 8 failed / 235 passed / 1 skipped / 10 errors,多出的 36 项即新测试)
  这些失败与错误是仓库在本机环境下的既有问题(缺 Redis/PostgreSQL 等),与本次改动无关。
@xy200303 xy200303 changed the title fix: isolate run-once scheduler jobs fix: 补齐各业务模型备注字段的长度校验 Sep 15, 2026
@xy200303

Copy link
Copy Markdown
Contributor Author

这个 PR 我重新定位了,说明一下原因和现在的内容。

为什么改内容

原内容(修复 #31 的 run-once 调度问题)已经被 #127 的调度器重构取代了。核实结果:

  • config/get_scheduler.py 与 SchedulerUtil 在 main 上均已不存在,整个调度器被拆成 config/scheduler/ 包。
  • run-once 现在走 JobRuntimeDao.create_request(...) → SchedulerManager.request_execution_dispatch(),由 SchedulerJobs.execute_once 用独立 id f'_manual:{execution_id}' 注册 TaskDateTrigger 任务,并带 replace_existing=True。
  • 因为 id 带了 _manual: 前缀,它不可能替换掉周期性的 cron 任务;execute_job_once_services 也不再调用 remove_scheduler_job。原 bug 已不复存在,原测试目标文件也没了。

所以继续推那份 diff 没有意义。我没有关掉这个 PR 重开一个,而是把分支换成了下面这个当前仍然存在的缺陷,复用原来的 PR 编号,免得给你的 PR 列表再添一条。

现在的内容

remark 字段缺少长度校验——即 issue #50。sys_user 等表该列是 varchar(500)、sys_notice 是 varchar(255),但模型里只声明了类型没有限长;同一模型的 user_name/nick_name/email/phonenumber 都已有 @Size,只有 remark 漏了。超长备注会走到数据库层:严格模式报错,非严格模式被静默截断。

改动是给 9 个带 remark 的业务模型补上与列长一致的 @Size,并挂进各自的 validate_fields()。各上限都取自 sql/ruoyi-fastapi-pg.sql 里的建表语句,没有猜。

验证

  • 新增 tests/module_admin/entity/test_vo_remark_length.py:每个模型断言列长通过 / 列长+1 被拒 / None 通过,并覆盖 validate_fields() 入口。
  • red → green:新测试在未修复代码上 27 项失败,修复后 36 项全部通过。
  • 无回归:pytest tests/module_admin 修复前后失败集合完全一致(8 failed / 1 skipped / 10 errors 为本机环境问题,与改动无关;通过数由 199 增至 235,多出的正是新测试)。

如果你更希望这个 PR 保持原来的主题、或者直接关掉它,告诉我一声,我按你的意思处理。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

新增用户的备注字段缺少长度校验

1 participant