Skip to content

feat: 数据库ORM重构和Alembic迁移支持#151

Merged
TommrraraSnow merged 5 commits intomainfrom
feat/database-orm-refactor
Oct 7, 2025
Merged

feat: 数据库ORM重构和Alembic迁移支持#151
TommrraraSnow merged 5 commits intomainfrom
feat/database-orm-refactor

Conversation

@TommrraraSnow
Copy link
Member

概述

  • 重构数据库层,引入SQLAlchemy ORM模型
  • 添加Alembic数据库迁移工具支持
  • 统一各数据库模块的实现方式

主要变更

  • 新增 orm.py 定义统一的SQLAlchemy数据模型
  • 添加完整的Alembic迁移配置和初始schema
  • 重构所有数据库服务模块使用ORM模式
  • 更新依赖配置以支持新的数据库组件

测试计划

  • 验证数据库迁移功能正常工作
  • 测试各数据库服务模块的CRUD操作
  • 确认API服务与新数据库层的集成
  • 验证sim_controller的数据库调用

🤖 Generated with Claude Code

- 新增SQLAlchemy ORM模型定义
- 添加Alembic数据库迁移配置
- 重构各数据库模块以使用ORM
- 更新依赖配置支持新数据库组件
- 修复sim_controller中的数据库导入问题

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@github-actions
Copy link

github-actions bot commented Oct 7, 2025

🤖 Hi @TommrraraSnow, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📋 Review Summary

This pull request is a significant and well-executed refactoring of the database layer, migrating from aiosqlite to SQLAlchemy ORM with Alembic for migrations. The changes are clean and follow modern asynchronous Python practices. The introduction of an ORM will greatly improve maintainability and reduce the likelihood of SQL injection vulnerabilities.

🔍 General Feedback

  • The implementation of the SQLAlchemy ORM and session management is solid.
  • The use of asyncio with the database layer is handled correctly.
  • Exception handling is present, but could be more specific in some file-handling cases to provide better error diagnostics.
  • One critical logical error was found in the export_apl_config function which could lead to a runtime error.
  • Overall, this is a high-quality contribution that modernizes the application's data persistence layer.

TommrraraSnow and others added 4 commits October 7, 2025 13:23
- 修复export_apl_config函数中tomli_w.dump使用错误,确保正确写入文件对象
- 确保导出文件时目标目录存在,避免文件路径错误
- 改进所有文件操作的异常处理,提供更详细的错误诊断信息:
  - FileNotFoundError: 文件未找到
  - PermissionError: 权限不足
  - UnicodeDecodeError: 文件编码错误
  - OSError: 文件系统错误
  - ValueError: TOML编码错误
- 改进错误消息的中文提示,提供更清晰的错误信息
- 所有APL相关测试已通过验证

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
重构APLDatabase类的异常处理策略:

**Service层改进**:
- 使用卫语句进行参数验证和基本检查
- 移除冗余的异常捕获,让错误自然传播
- 简化方法逻辑,提高代码可读性

**其他层改进**:
- 移除异步私有方法和工具方法中的异常捕获
- 让数据库和文件系统错误自然传播到调用者
- 清理未使用的导入和代码

**具体修改**:
- get_apl_config, create_apl_config, update_apl_config, delete_apl_config
- export_apl_config, import_apl_config, get_apl_file_content
- create_apl_file, update_apl_file, delete_apl_file
- _get_apl_from_dir 方法
- 移除 SQLAlchemyError 导入

**优点**:
- 错误处理更加清晰和一致
- 减少了代码复杂度
- 调用者可以根据需要处理特定异常
- 遵循"快速失败"原则

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@TommrraraSnow TommrraraSnow merged commit a28049a into main Oct 7, 2025
4 checks passed
@TommrraraSnow TommrraraSnow deleted the feat/database-orm-refactor branch October 7, 2025 05:58
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