Skip to content

fix: stop repeated allow_balance_pay column migration on restart - #5361

Merged
Calcium-Ion merged 1 commit into
QuantumNous:mainfrom
feitianbubu:pr/3f24a40ac21e91478bbd878f9d5a8b101863cc18
Jun 20, 2026
Merged

fix: stop repeated allow_balance_pay column migration on restart#5361
Calcium-Ion merged 1 commit into
QuantumNous:mainfrom
feitianbubu:pr/3f24a40ac21e91478bbd878f9d5a8b101863cc18

Conversation

@feitianbubu

@feitianbubu feitianbubu commented Jun 7, 2026

Copy link
Copy Markdown
Member

⚠️ 提交说明 / PR Notice

当前 bool类型的列, 如果 default:true
会造成每次启动时, 如果是mysql数据库 migrate会重复报:
model/main.go:304 [60.293ms] [rows:0] ALTER TABLE subscription_plansMODIFY COLUMNallow_balance_pay boolean DEFAULT true
因为mysql的bool默认值是1, true每次会被识别为不匹配

但如果改成 default:1
又会造成如果是postgres数据库 migrate会重复报:
model/main.go:304 [60.293ms] [rows:0] ALTER TABLE subscription_plansMODIFY COLUMNallow_balance_pay boolean DEFAULT 1

目前无论使用哪种默认值, 都无法兼容mysql和pg数据库

🚀 变更类型 / Type of change

最好的方案是去除默认值, 由业务决定默认值,而且这也更符合代码逻辑直觉

✅ 提交前检查项 / Checklist

已检查过,当前AllowBalancePay初始化时, 代码已经默认为true, 因此去除gorm默认值不影响原来的业务逻辑
只会减少每次启动时实际的migrate数据库变动日志噪音

Summary by CodeRabbit

  • Refactor
    • Optimized internal default value handling for subscription plan configuration to enhance application logic efficiency.

@coderabbitai

coderabbitai Bot commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: af0c8eb1-f993-4eca-bdce-0cd5c754b97d

📥 Commits

Reviewing files that changed from the base of the PR and between 4ca47ee and 1800f05.

📒 Files selected for processing (1)
  • model/subscription.go

Walkthrough

This PR updates the SubscriptionPlan struct in model/subscription.go to remove the database-level gorm:"default:true" tag from the AllowBalancePay *bool field, shifting default initialization from the database schema to application logic via the existing NormalizeDefaults() method.

Changes

GORM Default Tag Migration

Layer / File(s) Summary
Remove GORM database default from AllowBalancePay
model/subscription.go
SubscriptionPlan.AllowBalancePay field tag updated to remove gorm:"default:true", delegating the nil-to-true default behavior to the existing NormalizeDefaults() application method.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Possibly related PRs

  • QuantumNous/new-api#1393: Removes gorm:"default:..." tags from multiple boolean fields (IsStream, UnlimitedQuota, ModelLimitsEnabled) to shift default initialization to application logic, following the same schema-default tagging pattern.

Suggested reviewers

  • seefs001

Poem

🐰 A boolean's home, once the database kept,
Now moves to the code where defaults are swept,
NormalizeDefaults walks in with a smile,
No schema default needed this while,
The change is small but the logic runs deep! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and clearly describes the main change: removing a GORM database default that was causing repeated migrations on restart.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Calcium-Ion
Calcium-Ion merged commit fae39cd into QuantumNous:main Jun 20, 2026
2 checks passed
nsuanningmeng added a commit to nsuanningmeng/LemonHub that referenced this pull request Jun 22, 2026
合并上游 3fcd741..0b7ae4e 共 23 个提交(含 tag v1.0.0-rc.13 / rc.14)。

主要上游内容:
- 仪表盘新增流量 Sankey 流向图(model/usedata_flow.go 等新表/统计)、可交互高亮、敏感数据开关
- DataTable 卡片/表格视图切换、渠道卡片重构、移动端卡片布局
- 模型路由可靠性管理(routing-reliability-section)、渠道敏感信息开关
- 迁移修复:allow_wallet_overflow / allow_balance_pay 重复 ALTER 迁移(QuantumNous#5616 QuantumNous#5361)
- 用量日志按登录类型筛选修复、CN 模型图标、计费历史弹窗分页修复
- CLAUDE.md 新增后端规则:避免 GORM 布尔 default 标签导致重启反复 ALTER

冲突处理(兼容本地白标/性能指标功能):
- 上游将 perf_metrics_setting 从「性能」页迁移至「监控与告警(alerts)」页(MonitoringSettingsSection)。
  采纳上游结构,并把本仓库的成功率阈值/错误码白名单/无数据视为满额(success_rate_green/yellow_threshold、
  error_code_whitelist、no_data_as_full)四项特性一并移植进 MonitoringSettingsSection,保持功能不丢失。
- performance-section.tsx 采用上游版本(perf_metrics 已移出)。
- section-registry.tsx 将上述四项接入 alerts 段 defaultValues。

验证:go build ./... 通过;web/default tsc -b 类型检查通过。
nsuanningmeng added a commit to nsuanningmeng/LemonHub that referenced this pull request Jun 22, 2026
合并后 codex + 多代理审查发现的两处低风险项:

- model/main.go: migrateDBFast() 当前为死代码(仅 migrateDB() 在 InitDB 被调用),
  但它缺少 migrateDB() 的失败即停预检(价格精度、model_limits、用户名唯一放松、
  site_id 回填)。一旦将来被接入启动路径,会在 MySQL 非严格模式下静默截断
  price_amount 等数据。为其补齐与 migrateDB() 一致的有序预检 + site_id 回填,
  消除潜在的无损迁移隐患(防御性,非当前可触发缺陷)。
- service/http_client.go: ValidateRelayTargetURL 注释错误地称 SSRF 防护“默认关闭”,
  实际 fetch_setting.go 默认 EnableSSRFProtection=true。更正注释以免误导。

未在本次改动的审查发现(已在汇报中列出,建议作为后续):
- [中] SSRF DNS-rebinding TOCTOU:ValidateURL 解析校验后 http.Transport 连接时二次解析,
  无 IP pinning(共享 HTTP 客户端层面的既有问题,修复涉及面广,单列跟进)。
- [低] SubscriptionPlan.Enabled 仍带 gorm:"default:true",MySQL 每次重启重复 ALTER
  (上游 QuantumNous#5616/QuantumNous#5361 同类修复遗漏项;改 *bool 影响支付校验等 7 处,单列跟进)。
- [信息] 子站 footer/notice 富文本 XSS 为既有特性(HTML/iframe 嵌入),本次新增
  home_badge/title 字段按文本渲染,安全。

验证:go build ./... 通过;model 迁移用例通过。
zhaodechao2008 pushed a commit to zhaodechao2008/new-api that referenced this pull request Jul 27, 2026
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.

2 participants