feat(db): 新增数据库合法性检查脚本(#957) - #1002
Conversation
新增只读 management command check_db_integrity,汇总模型中未表达的隐性约束并 输出违规记录(不修改数据):C1 同一人+小组+学年不可同时拥有在职 ANNUAL 与单学期 职务;C2 同一人+小组+学年+学期不应出现重复 Position(Yuanpei-Intelligence#968 同类);C3 同一人+学年 选书院课程不应同时拥有在职 ANNUAL 职务。支持 --verbose 与 --output。 Files changed: - dm/management/commands/check_db_integrity.py
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: bfdf3b8a8e
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| for d in dups: | ||
| rows = Position.objects.filter( |
There was a problem hiding this comment.
Replace the per-group duplicate lookup
When historical corruption contains many duplicate (person, org, year, semester) groups, this loop evaluates one additional Position query for every group before issuing the final id__in query. Since this command is specifically intended to scan dirty databases, the resulting N+1 query pattern can make the integrity check prohibitively slow; return the duplicate rows using a correlated subquery/window expression or another bounded-query approach instead.
AGENTS.md reference: AGENTS.md:L413-L417
Useful? React with 👍 / 👎.
| self.stdout.write(self.style.SUCCESS(header)) | ||
| lines.append(header) | ||
| if verbose: | ||
| for p in qs[:200]: |
There was a problem hiding this comment.
Honor the verbose option's all-record contract
When a check finds more than 200 violations, --verbose promises to print every violating record but this slice silently emits only the first 200; the --output report is truncated the same way. This can leave operators unaware of records that still require remediation, so either emit the complete queryset or explicitly expose and report a configurable limit.
Useful? React with 👍 / 👎.
新增 dm 管理命令 check_db_integrity,检查如「同一人+学年同时选书院课与在职 ANNUAL 职务」等一致性规则。基于 upstream/develop 重建。