mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-08 08:55:22 +00:00
fix(auth-signup): 平台配额快照脱离注册事务 + grok 补入 CHECK 约束
自助注册(含钉钉/OAuth)报 500→404 的根因:grok 自 2026-06 进入默认平台配额 (default_platform_quotas / auth_source_*),但 user_platform_quotas 的 CHECK 约束(迁移 142)仅允许 anthropic/openai/gemini/antigravity。注册时 snapshotPlatformQuotaDefaults 写 grok 行违反约束 → 整个注册事务被 Postgres 标记 aborted → consumePendingOAuthBrowserSessionTx 撞 "transaction aborted" → 500 → clearCookies → 用户重试拿到 404(PENDING_AUTH_SESSION_NOT_FOUND)。 影响面:所有新自助注册(不限钉钉)。 修复(两层): - 事务隔离(fix①):snapshotPlatformQuotaDefaults 用 ent.WithoutTx 剥离调用方事务, 在基础连接 autocommit 执行。best-effort 快照失败永不毒化注册主事务,从根上消除 "事务内 fail-open 形同虚设"陷阱——今后任何平台/约束漂移都不会再连累注册。 - 迁移 157:把 grok 加入 user_platform_quotas.platform 的 CHECK 约束,与代码平台 列表(domain/constants.go PlatformGrok)对齐(DROP IF EXISTS + ADD,可重入)。 新增 ent.WithoutTx(ctx) helper(手写文件,不动生成代码)。 测试: - 单测 TestSnapshotPlatformQuotaDefaults_DetachesCallerTransaction(RED→GREEN): 快照即便在事务 ctx 中也必须用脱离事务的 ctx 调 repo。 - 集成测试 TestUserPlatformQuotaRepository_BulkInsertInitial_GrokAllowed: 迁移 157 后 grok 可写入(真实 postgres 容器验证)。 验证:go build ./... / go vet -tags unit ./... / 全量单测(-tags unit,45 包) / 平台配额+迁移集成测试 全绿。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
dcd8689d78
commit
185f9c9920
5 files changed
+100
-1
No files matched your search
@@ -72,6 +72,32 @@ func TestUserPlatformQuotaRepository_BulkInsertInitial_Empty(t *testing.T) {
|
||||
require.NoError(t, repo.BulkInsertInitial(txCtx, []UserPlatformQuotaRecord{}))
|
||||
}
|
||||
|
||||
// TestUserPlatformQuotaRepository_BulkInsertInitial_GrokAllowed 回归迁移 157:
|
||||
// grok 平台必须能写入 user_platform_quotas(CHECK 约束已含 grok)。
|
||||
// 历史 bug:grok 不在约束内 → 注册写默认配额违约 → 注册事务 aborted → 自助注册 500/404。
|
||||
func TestUserPlatformQuotaRepository_BulkInsertInitial_GrokAllowed(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
tx := testEntTx(t)
|
||||
txCtx := dbent.NewTxContext(ctx, tx)
|
||||
client := tx.Client()
|
||||
|
||||
userID := mustCreateUserForQuota(t, client)
|
||||
repo := NewUserPlatformQuotaRepository(client)
|
||||
|
||||
daily := 9.0
|
||||
records := []UserPlatformQuotaRecord{
|
||||
{UserID: userID, Platform: "grok", DailyLimitUSD: &daily},
|
||||
}
|
||||
require.NoError(t, repo.BulkInsertInitial(txCtx, records),
|
||||
"grok 平台应可写入(迁移 157 后 CHECK 约束已含 grok)")
|
||||
|
||||
rec, err := repo.GetByUserPlatform(txCtx, userID, "grok")
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, rec, "grok 配额行应已写入")
|
||||
require.NotNil(t, rec.DailyLimitUSD)
|
||||
require.InDelta(t, 9.0, *rec.DailyLimitUSD, 1e-9)
|
||||
}
|
||||
|
||||
func TestUserPlatformQuotaRepository_GetByUserPlatform(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
tx := testEntTx(t)
|
||||
|
||||
Reference in new issue
Block a user