Skip to content

fix:在数据库存在字段时循环报错#174

Merged
ding113 merged 2 commits into
ding113:devfrom
Silentely:fix-db-error
Nov 22, 2025
Merged

fix:在数据库存在字段时循环报错#174
ding113 merged 2 commits into
ding113:devfrom
Silentely:fix-db-error

Conversation

@Silentely

@Silentely Silentely commented Nov 21, 2025

Copy link
Copy Markdown
Contributor

现象

在数据库已存在相应字段时启动应用会循环报错,导致服务无法正常启动。

错误日志:

{"level":"error","msg":"❌ Migration failed:","err":{"message":"Failed query: ALTER TABLE \"keys\" ADD COLUMN \"limit_daily_usd\" numeric(10, 2); column \"limit_daily_usd\" of relation \"keys\" already exists"}}
image

问题原因

数据库迁移文件 0021_daily_cost_limits.sql 在执行时未检查字段是否已存在,导致:

  • 当字段已存在时,ALTER TABLE ADD COLUMN 语句失败
  • 迁移失败后应用无法正常启动
  • 重启后继续尝试执行相同的迁移,形成循环报错

解决方案

在所有 ALTER TABLE ADD COLUMN 语句中添加 IF NOT EXISTS 子句,确保:

  • 字段不存在时正常创建
  • 字段已存在时跳过创建,不报错
  • 迁移脚本具有幂等性,可安全重复执行

主要变更

  • 文件: drizzle/0021_daily_cost_limits.sql
  • 变更内容:
    • ✅ 为 keys 表的 3 个字段添加 IF NOT EXISTS
      • limit_daily_usd
      • daily_reset_time
      • daily_reset_mode
    • ✅ 为 providers 表的 3 个字段添加 IF NOT EXISTS
      • limit_daily_usd
      • daily_reset_time
      • daily_reset_mode
    • ✅ 更新注释说明防重复创建的目的

测试验证

  • 在已存在字段的数据库上测试迁移(不报错)
  • 在全新数据库上测试迁移(正常创建字段)
  • 验证应用启动成功
  • 无破坏性变更

影响范围

  • 受影响版本: 升级到包含 0021_daily_cost_limits.sql 迁移的版本
  • 风险等级: 低(仅修改迁移脚本,不影响现有逻辑)
  • 向后兼容: 完全兼容

Related

修复每日成本限额功能的数据库迁移问题。

添加了 IF NOT EXISTS 以防止重复创建报错
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @Silentely, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求旨在解决一个关键问题,即当数据库迁移脚本尝试添加已存在的列时,应用程序会持续崩溃。通过在 ALTER TABLE ADD COLUMN 语句中引入 IF NOT EXISTS 子句,此修复确保了数据库模式更改的幂等性,从而显著提高了部署和重新迁移场景下的应用程序稳定性。

Highlights

  • 修复循环报错: 解决了当数据库中已存在特定字段时,应用启动会循环报错的问题。
  • 数据库迁移脚本改进: 修改了 SQL 迁移脚本,在添加列时使用了 IF NOT EXISTS 语句,确保操作的幂等性。
  • 提升应用稳定性: 通过防止重复创建已存在字段导致的错误,增强了应用在数据库迁移过程中的稳定性。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@ding113 ding113 added bug Something isn't working size/XS Extra Small PR (< 50 lines) labels Nov 21, 2025

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

这个拉取请求很好地解决了因数据库迁移脚本非幂等性而导致的服务启动循环报错问题。通过为 ADD COLUMN 语句添加 IF NOT EXISTS 子句,确保了迁移脚本可以安全地重复执行,这是一个非常正确的修复方式。

在审查过程中,我发现了一个可以改进的地方:迁移脚本中为 keysproviders 表添加的 daily_reset_mode 列使用了 varchar(10) 类型,而 Drizzle 的 schema 定义 (src/drizzle/schema.ts) 中使用的是 PostgreSQL 的枚举类型 (pgEnum)。这种不一致可能会影响数据完整性。我在代码中留下了具体的建议,希望能帮助您统一数据库结构和应用层的 schema 定义,使代码更加健壮。

Comment thread drizzle/0021_daily_cost_limits.sql Outdated
Comment thread drizzle/0021_daily_cost_limits.sql Outdated
@Silentely
Silentely changed the base branch from main to dev November 22, 2025 02:17
@ding113
ding113 merged commit 86f7615 into ding113:dev Nov 22, 2025
3 of 5 checks passed
@ding113 ding113 mentioned this pull request Nov 22, 2025
5 tasks
@Silentely
Silentely deleted the fix-db-error branch November 26, 2025 12:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size/XS Extra Small PR (< 50 lines)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants