Repository navigation
fix(billing): make group rate/RPM sync transactional - #8659
Merged
Wei-Shaw merged 1 commit intoNov 16, 2026
Merged
Conversation
SyncGroupRateMultipliers and SyncGroupRPMOverrides ran three destructive statements in autocommit mode: clear rows missing from the payload, delete all-NULL rows, then upsert the payload. If the upsert failed (deleted user FK, duplicate user_id, transient DB error), the clear and delete had already committed, silently wiping the group's overrides. Wrap each sync in one transaction and dedupe entries by user_id before writing so ON CONFLICT cannot hit the same row twice. Any failure now rolls the whole sync back. Add sqlmock tests covering commit, rollback and dedupe.
Contributor
Author
|
I have read the CLA Document and I hereby sign the CLA |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
SyncGroupRateMultipliers/SyncGroupRPMOverrides用三条独立的 autocommit 语句完成“全量同步”:UPDATE ... SET <column> = NULL;DELETE整行全 NULL 的记录;INSERT ... ON CONFLICT DO UPDATE写入本次 entries。没有事务。第 3 步失败时,前两步已经提交且无法回滚,数据库停留在“部分清空”的状态。
触发场景
user_id REFERENCES users(id)失败;user_id→ON CONFLICT DO UPDATE command cannot affect row a second time;结果:接口返回 500,但不在 entries 里的用户配置已经被清空/删除;管理员看不到“其他配置被清空”的提示。
修复
user_id去重(保留最后一次出现的值),避免重复 ID 让ON CONFLICT直接失败;测试
用 sqlmock 验证事务边界:
TestSyncGroupRateMultipliersCommitsInOneTransaction/TestSyncGroupRPMOverridesCommitsInOneTransaction:三步 + Commit;...RollsBackOnInsertFailure:第 3 步失败时执行 Rollback,前两步不会生效;TestDedupeGroupRateMultiplierEntriesKeepsLastValue/TestDedupeGroupRPMOverrideEntriesKeepsLastValue:重复 user_id 保留最后一次。本地验证:
go test -tags=unit ./internal/repository/通过-race通过go build ./...通过Closes #8660