Skip to content

Comments

fix: 修复数据库迁移枚举类型重复创建错误#181

Merged
ding113 merged 1 commit intomainfrom
dev
Nov 22, 2025
Merged

fix: 修复数据库迁移枚举类型重复创建错误#181
ding113 merged 1 commit intomainfrom
dev

Conversation

@ding113
Copy link
Owner

@ding113 ding113 commented Nov 22, 2025

Summary

修复数据库迁移过程中 daily_reset_mode 枚举类型重复创建导致的错误。

Changes

  • drizzle/0022_simple_stardust.sql 中添加异常处理逻辑
  • 使用 DO $$ BEGIN ... EXCEPTION 块安全创建枚举类型
  • 确保迁移脚本的幂等性,避免 "type already exists" 错误

Background

项目中存在两个迁移文件尝试创建相同的枚举类型:

  • 0021_daily_cost_limits.sql - 已有异常处理 ✅
  • 0022_simple_stardust.sql - 缺少异常处理 ❌(本次修复)

当数据库中已存在该枚举类型时,直接使用 CREATE TYPE 会导致迁移失败。

Solution

将原来的:

CREATE TYPE "public"."daily_reset_mode" AS ENUM('fixed', 'rolling');

修改为:

DO $$ BEGIN
    CREATE TYPE "public"."daily_reset_mode" AS ENUM('fixed', 'rolling');
EXCEPTION
    WHEN duplicate_object THEN null;
END $$;

Test Plan

  • 本地开发环境测试
  • Docker 环境测试
  • 验证迁移脚本幂等性

Impact

  • ✅ 修复 Docker 部署时的迁移失败问题
  • ✅ 确保 AUTO_MIGRATE=true 环境下的稳定性
  • ✅ 兼容已存在和不存在枚举类型的两种场景

🤖 Generated with Claude Code

- 在 0022_simple_stardust.sql 中添加异常处理逻辑
- 使用 DO $$ BEGIN ... EXCEPTION 块安全创建 daily_reset_mode 枚举
- 解决 "type already exists" 错误,确保迁移幂等性

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

Co-Authored-By: Claude <noreply@anthropic.com>
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @ding113, 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!

此拉取请求旨在通过在数据库迁移脚本中引入异常处理机制,解决 daily_reset_mode 枚举类型重复创建导致的问题。这一改进确保了迁移过程的健壮性和幂等性,尤其是在 Docker 部署和 AUTO_MIGRATE=true 环境下,显著提升了系统的稳定性,并兼容了枚举类型已存在或不存在的两种场景。

Highlights

  • 修复数据库迁移错误: 解决了在数据库迁移过程中,daily_reset_mode 枚举类型因重复创建而导致的错误。
  • 引入异常处理: 在 drizzle/0022_simple_stardust.sql 文件中,为枚举类型的创建添加了 DO $$ BEGIN ... EXCEPTION WHEN duplicate_object THEN null; END $$; 异常处理逻辑。
  • 确保迁移脚本幂等性: 通过添加异常处理,确保了迁移脚本的幂等性,即使枚举类型已存在,脚本也能安全执行,避免了 'type already 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 linked an issue Nov 22, 2025 that may be closed by this pull request
@ding113 ding113 added the size/XS Extra Small PR (< 50 lines) label Nov 22, 2025
Copy link
Contributor

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

Choose a reason for hiding this comment

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

Code Review

本次 PR 旨在修复数据库迁移中因重复创建枚举类型 daily_reset_mode 而导致的错误。通过在 drizzle/0022_simple_stardust.sql 文件中使用 DO ... EXCEPTION 块,确保了迁移脚本的幂等性,这是一个正确且健壮的解决方案。代码变更有效解决了问题。我提供了一个关于在异常处理中增加日志输出的建议,以提高迁移过程的可观察性。

DO $$ BEGIN
CREATE TYPE "public"."daily_reset_mode" AS ENUM('fixed', 'rolling');
EXCEPTION
WHEN duplicate_object THEN null;
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

在异常处理块中,使用 null; 会静默地忽略错误,这虽然能保证幂等性,但在调试迁移问题时可能会造成困惑,因为没有任何信息表明 CREATE TYPE 被跳过了。建议使用 RAISE NOTICE 来打印一条消息,这样在日志中可以清晰地看到该类型已存在并被跳过,有助于提高迁移脚本的可观察性和可维护性。

    WHEN duplicate_object THEN RAISE NOTICE 'type "public"."daily_reset_mode" already exists, skipping';

@ding113 ding113 added the bug Something isn't working label Nov 22, 2025
@ding113 ding113 merged commit 848defe into main Nov 22, 2025
8 of 9 checks passed
ding113 pushed a commit that referenced this pull request Nov 22, 2025
This was referenced Nov 25, 2025
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.

一键部署失败

1 participant