Skip to content

fix: reduce affiliate code collisions#6242

Open
zhibisora wants to merge 1 commit into
QuantumNous:mainfrom
zhibisora:zhibisora/fix-issue-6215
Open

fix: reduce affiliate code collisions#6242
zhibisora wants to merge 1 commit into
QuantumNous:mainfrom
zhibisora:zhibisora/fix-issue-6215

Conversation

@zhibisora

@zhibisora zhibisora commented Jul 16, 2026

Copy link
Copy Markdown

⚠️ 提交说明 / PR Notice

Important

  • 请提供人工撰写的简洁摘要,避免直接粘贴未经整理的 AI 输出。
  • 本 PR 由 AI 辅助实现与整理,提交者已审阅变更逻辑、测试结果及下述设计取舍。

📝 变更描述 / Description

将新用户推广码从 4 位扩展为 8 位,并在写入前有限次数查询候选码是否已被占用;查询包含软删除记录,兼容已有 4 位推广码。普通注册、OAuth 注册、后台创建用户及旧用户补码统一使用模型层分配逻辑,旧用户补码通过条件更新避免同一用户的并发请求互相覆盖。

数据库唯一索引继续作为最终一致性约束。本次有意不处理“候选码查询完成后、写入前被另一请求抢占”的极端竞态:PostgreSQL 需要保存点恢复失败事务,而 MySQL 与 SQLite 的锁及错误语义不同,跨数据库事务内重试会显著扩大改动范围;该竞态发生时仍返回原始数据库错误,但不会写入重复推广码。

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix) - 请关联对应 Issue,避免将设计取舍、理解偏差或预期不一致直接归类为 bug
  • ✨ 新功能 (New feature) - 重大特性建议先通过 Issue 沟通
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 IssuesPRs,确认不是重复提交。
  • Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

  • go test ./model ./controller -count=1
  • go vet ./model ./controller
  • go test ./model -run 'AffCode' -count=10
  • git diff --check

Summary by CodeRabbit

  • Improvements

    • Affiliate codes are now generated more reliably, avoiding conflicts with existing or previously removed codes.
    • Existing accounts without affiliate codes can receive one automatically when requested.
    • Affiliate codes remain consistent after being assigned.
  • Bug Fixes

    • User and OAuth account creation now stops cleanly when an affiliate code cannot be prepared, preventing incomplete account setup.
    • Improved handling of affiliate code assignment during account creation.

@coderabbitai

coderabbitai Bot commented Jul 16, 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: 7827d404-2f93-4248-944b-be87fa368a6b

📥 Commits

Reviewing files that changed from the base of the PR and between a63364d and 0a06089.

📒 Files selected for processing (4)
  • controller/oauth.go
  • controller/user.go
  • model/user.go
  • model/user_aff_code_test.go

Walkthrough

Affiliate-code provisioning is centralized through availability checks, preparation, and conditional backfilling. User creation and OAuth flows prepare codes before persistence, while retrieval ensures missing codes are assigned.

Changes

Affiliate code lifecycle

Layer / File(s) Summary
Centralized affiliate-code allocation
model/user.go, controller/user.go, model/user_aff_code_test.go
Affiliate codes use bounded availability checks, explicit preparation, and conditional backfilling; retrieval now calls EnsureAffCode().
Creation-path preparation
model/user.go, controller/user.go, controller/oauth.go, model/user_aff_code_test.go
Standard and OAuth creation paths prepare affiliate codes before insertion, and transactional insertion requires a populated code. Tests cover retries, generated length, and transactional selection.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant CreateUser
  participant PrepareAffCode
  participant UserInsert
  participant UsersTable
  CreateUser->>PrepareAffCode: prepare affiliate code
  PrepareAffCode->>UsersTable: check generated code availability
  UsersTable-->>PrepareAffCode: available code
  PrepareAffCode-->>CreateUser: continue creation
  CreateUser->>UserInsert: insert prepared user
  UserInsert->>UsersTable: persist user and affiliate code
Loading

Suggested reviewers: seefs001

Poem

A rabbit hops through codes so neat,
Checks each one before the seat.
New users bring a code in tow,
Old users gain one when they show.
Transactions bloom, and tests applaud—
“Hop hop!” says Bun, “the path is shod!”

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 7.69% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: reducing affiliate code collisions.
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.
✨ 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.

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