fix: 处理历史记录目录创建时旧文件冲突#236
Conversation
There was a problem hiding this comment.
Pull request overview
该 PR 修复了创建历史记录目录(如 data/history/roll_call_history)时,若同名路径存在旧版“文件”而非“目录”导致 mkdir(exist_ok=True) 仍抛出 FileExistsError 的升级兼容问题。
Changes:
- 在
get_history_file_path中检测到历史记录路径存在且非目录时,先记录日志并删除旧文件。 - 随后再执行
mkdir(parents=True, exist_ok=True),确保目录创建成功。
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| history_dir = get_path(f"data/history/{history_type}_history") | ||
| if history_dir.exists() and not history_dir.is_dir(): | ||
| logger.warning(f"检测到旧版历史记录文件,正在删除以创建目录: {history_dir}") | ||
| history_dir.unlink() |
| if history_dir.exists() and not history_dir.is_dir(): | ||
| logger.warning(f"检测到旧版历史记录文件,正在删除以创建目录: {history_dir}") | ||
| history_dir.unlink() |
| if history_dir.exists() and not history_dir.is_dir(): | ||
| logger.warning(f"检测到旧版历史记录文件,正在删除以创建目录: {history_dir}") | ||
| history_dir.unlink() |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
|
@copilot Review the code at the location below. A potential bug has been identified by an AI Location: app/common/history/file_utils.py#L72-L74 Potential issue: The |
Agent-Logs-Url: https://github.com/SECTL/SecRandom/sessions/6f6ab0cc-130c-465b-81d9-e35ad9b0df2e Co-authored-by: WSXYT <102407247+WSXYT@users.noreply.github.com>
已确认这是有效问题,并已修复(提交 |
| except PermissionError as e: | ||
| logger.error(f"删除旧版历史记录文件失败(权限不足): {history_dir}, 错误: {e}") | ||
| except OSError as e: | ||
| logger.error(f"删除旧版历史记录文件失败: {history_dir}, 错误: {e}") | ||
| try: | ||
| history_dir.mkdir(parents=True, exist_ok=True) | ||
| except FileExistsError: | ||
| logger.error(f"创建历史记录目录失败,目标仍为文件: {history_dir}") | ||
| except OSError as e: | ||
| logger.error(f"创建历史记录目录失败: {history_dir}, 错误: {e}") |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
|
@copilot Review the code at the location below. A potential bug has been identified by an AI Location: app/common/history/file_utils.py#L76-L85 Potential issue: When |
Agent-Logs-Url: https://github.com/SECTL/SecRandom/sessions/9f2f5591-dc71-40c7-b4df-bb8e08e0105a Co-authored-by: WSXYT <102407247+WSXYT@users.noreply.github.com>
已确认是有效问题,并在提交 |
修复了在创建历史记录目录(例如
roll_call_history)时,如果该路径下存在旧版文件而非目录,会导致FileExistsError的问题。pathlib.Path.mkdir(exist_ok=True)仅在目标路径已存在且为目录时才不会报错,但无法处理同名文件的情况。问题原因:
旧版本应用将历史记录存储为单个文件,而新版本将其改为按班级存储在同名目录下。用户从旧版本升级后,旧文件会阻碍新版本创建目录。
解决方案:
在
app/common/history/file_utils.py的get_history_file_path函数中,在调用mkdir之前,增加了一个检查。如果目标路径已存在但不是目录(即为旧版文件),则先删除该文件,然后日志记录此操作,再尝试创建目录。这确保了平滑升级,避免了FileExistsError。Fixes SECRANDOM-9S