mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-09-24 16:05:44 +08:00
自助注册(含钉钉/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>
17 lines
1.0 KiB
SQL
17 lines
1.0 KiB
SQL
-- 把 grok 平台加入 user_platform_quotas.platform 的 CHECK 约束。
|
|
--
|
|
-- 背景:grok 自 2026-06 起进入默认平台配额(default_platform_quotas /
|
|
-- auth_source_default_*_platform_quotas),但 142 建表时的 CHECK 仅允许
|
|
-- anthropic/openai/gemini/antigravity。自助注册时 snapshotPlatformQuotaDefaults
|
|
-- 会写入 grok 默认配额行 → 违反 CHECK → 整个注册事务被标记 aborted →
|
|
-- OAuth pending 路径 consume 会话时撞 "transaction aborted" → 500 → 清 cookie → 404。
|
|
--
|
|
-- 修复:把约束与代码平台列表(internal/domain/constants.go 的 PlatformGrok)对齐。
|
|
-- DROP ... IF EXISTS 保证可重入;新约束是旧约束的超集,存量行(仅 4 平台)瞬时校验通过。
|
|
ALTER TABLE user_platform_quotas
|
|
DROP CONSTRAINT IF EXISTS user_platform_quotas_platform_check;
|
|
|
|
ALTER TABLE user_platform_quotas
|
|
ADD CONSTRAINT user_platform_quotas_platform_check
|
|
CHECK (platform IN ('anthropic', 'openai', 'gemini', 'antigravity', 'grok'));
|