Skip to content

refactor(database): migrate to SQLAlchemy 2.0 async API#30

Closed
akaBoyLovesToCode wants to merge 1 commit intoembyplus:mainfrom
akaBoyLovesToCode:sql20
Closed

refactor(database): migrate to SQLAlchemy 2.0 async API#30
akaBoyLovesToCode wants to merge 1 commit intoembyplus:mainfrom
akaBoyLovesToCode:sql20

Conversation

@akaBoyLovesToCode
Copy link
Contributor

@akaBoyLovesToCode akaBoyLovesToCode commented Feb 23, 2025

Overview

This PR migrates the database creation logic to SQLAlchemy 2.0's asynchronous API. The main changes include using the new API for executing raw SQL queries and adjusting the transaction management to align with SQLAlchemy 2.0 best practices.

Motivation

SQLAlchemy 2.0 introduces several enhancements for asynchronous operations with a more intuitive and concise syntax. Adopting these changes improves code clarity and ensures future compatibility while reducing upgrade risks.

Changes

  • Use create_async_engine to create the asynchronous engine, renaming the variable to engine for clarity.
  • Replace conn.execute(text(query)) with conn.exec_driver_sql(query) for executing raw SQL.
  • Adopt the async transaction context with async with engine.begin() as conn:.
  • Ensure proper resource cleanup using await engine.dispose().

Testing

Local tests confirm that the database creation functionality works as expected without any side effects.

References

Summary by Sourcery

增强功能:

  • 将数据库创建逻辑迁移到 SQLAlchemy 2.0 的异步 API,以提高代码清晰度和未来兼容性。
Original summary in English

Summary by Sourcery

Enhancements:

  • Migrate database creation logic to SQLAlchemy 2.0's asynchronous API for improved code clarity and future compatibility.

- Replace `conn.execute(text(query))` with `conn.exec_driver_sql(query)` for raw SQL execution.
- Use the new async transaction context (`async with engine.begin() as conn:`) per SQLAlchemy 2.0.
- Update engine variable naming and disposal with `await engine.dispose()`.
@sourcery-ai
Copy link

sourcery-ai bot commented Feb 23, 2025

审核指南由 Sourcery 提供

此拉取请求重构了数据库创建逻辑,以利用 SQLAlchemy 2.0 的异步 API,增强代码清晰度并确保未来的兼容性。关键更改包括采用新的异步引擎创建、更新原始 SQL 执行方法,并将事务管理与最佳实践对齐。

异步数据库创建的序列图

sequenceDiagram
    actor User
    participant App
    participant SQLAlchemy
    User->>App: 启动数据库创建
    App->>SQLAlchemy: create_async_engine()
    App->>SQLAlchemy: engine.begin()
    SQLAlchemy-->>App: 连接已建立
    App->>SQLAlchemy: exec_driver_sql(query)
    SQLAlchemy-->>App: 执行 SQL
    App->>SQLAlchemy: engine.dispose()
    SQLAlchemy-->>App: 资源已清理
    App-->>User: 数据库创建完成
Loading

更新数据库引擎使用的类图

classDiagram
    class App {
        +create_database_if_not_exists()
        +_init_db()
    }
    class SQLAlchemy {
        +create_async_engine()
        +begin()
        +exec_driver_sql()
        +dispose()
    }
    App --> SQLAlchemy : 使用
Loading

文件级更改

更改 详情 文件
迁移到 SQLAlchemy 2.0 的异步 API 进行数据库操作。
  • create_async_engine 替换同步引擎创建,并将变量重命名为 engine 以提高清晰度。
  • 将原始 SQL 执行方法从 conn.execute(text(query)) 更新为 conn.exec_driver_sql(query)
  • 采用异步事务上下文 async with engine.begin() as conn:
  • 通过使用 await engine.dispose() 确保正确的资源清理。
app.py

提示和命令

与 Sourcery 互动

  • 触发新审核: 在拉取请求上评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审核评论。
  • 从审核评论生成 GitHub 问题: 通过回复审核评论请求 Sourcery 从中创建问题。您也可以在审核评论中回复 @sourcery-ai issue 来从中创建问题。
  • 生成拉取请求标题: 在拉取请求标题的任何地方写 @sourcery-ai 以随时生成标题。您也可以在拉取请求上评论 @sourcery-ai title 以随时(重新)生成标题。
  • 生成拉取请求摘要: 在拉取请求正文的任何地方写 @sourcery-ai summary 以随时在您想要的地方生成 PR 摘要。您也可以在拉取请求上评论 @sourcery-ai summary 以随时(重新)生成摘要。
  • 生成审核指南: 在拉取请求上评论 @sourcery-ai guide 以随时(重新)生成审核指南。
  • 解决所有 Sourcery 评论: 在拉取请求上评论 @sourcery-ai resolve 以解决所有 Sourcery 评论。如果您已经解决了所有评论并且不想再看到它们,这将非常有用。
  • 取消所有 Sourcery 审核: 在拉取请求上评论 @sourcery-ai dismiss 以取消所有现有的 Sourcery 审核。如果您想从新的审核开始,这将特别有用 - 不要忘记评论 @sourcery-ai review 以触发新审核!
  • 为问题生成行动计划: 在问题上评论 @sourcery-ai plan 以为其生成行动计划。

自定义您的体验

访问您的仪表板以:

  • 启用或禁用审核功能,例如 Sourcery 生成的拉取请求摘要、审核指南等。
  • 更改审核语言。
  • 添加、删除或编辑自定义审核说明。
  • 调整其他审核设置。

获取帮助

Original review guide in English

Reviewer's Guide by Sourcery

This pull request refactors the database creation logic to utilize SQLAlchemy 2.0's asynchronous API, enhancing code clarity and ensuring future compatibility. Key changes include adopting the new async engine creation, updating raw SQL execution methods, and aligning transaction management with best practices.

Sequence diagram for asynchronous database creation

sequenceDiagram
    actor User
    participant App
    participant SQLAlchemy
    User->>App: Initiate database creation
    App->>SQLAlchemy: create_async_engine()
    App->>SQLAlchemy: engine.begin()
    SQLAlchemy-->>App: Connection established
    App->>SQLAlchemy: exec_driver_sql(query)
    SQLAlchemy-->>App: Execute SQL
    App->>SQLAlchemy: engine.dispose()
    SQLAlchemy-->>App: Resources cleaned up
    App-->>User: Database creation complete
Loading

Class diagram for updated database engine usage

classDiagram
    class App {
        +create_database_if_not_exists()
        +_init_db()
    }
    class SQLAlchemy {
        +create_async_engine()
        +begin()
        +exec_driver_sql()
        +dispose()
    }
    App --> SQLAlchemy : uses
Loading

File-Level Changes

Change Details Files
Migrate to SQLAlchemy 2.0's asynchronous API for database operations.
  • Replaced the synchronous engine creation with create_async_engine and renamed the variable to engine for clarity.
  • Updated the raw SQL execution method from conn.execute(text(query)) to conn.exec_driver_sql(query).
  • Adopted the async transaction context using async with engine.begin() as conn:.
  • Ensured proper resource cleanup by using await engine.dispose().
app.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

嘿 @Qubbby - 我已经审查了你的更改,它们看起来很棒!

这是我在审查期间查看的内容
  • 🟢 一般问题:一切看起来都很好
  • 🟢 安全性:一切看起来都很好
  • 🟢 测试:一切看起来都很好
  • 🟢 复杂性:一切看起来都很好
  • 🟢 文档:一切看起来都很好

Sourcery 对开源项目免费 - 如果你喜欢我们的审查,请考虑分享它们 ✨
帮助我变得更有用!请点击每条评论上的 👍 或 👎,我将使用反馈来改进你的审查。
Original comment in English

Hey @Qubbby - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

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