fix: 修复角色分配权限并发竞态导致唯一约束冲突报错
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m4s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m4s
- Store 层 Create 方法对唯一约束冲突幂等处理,消除并发写入报错 - Service 层预批量加载角色已有权限集合(1次查询),替换逐条 Exists 查询(N次查询),同时降低竞态窗口 💘 Generated with Crush Assisted-by: Claude Sonnet 4.6 via Crush <crush@charm.land>
This commit is contained in:
@@ -223,10 +223,19 @@ func (s *Service) AssignPermissions(ctx context.Context, roleID uint, permIDs []
|
||||
return nil, errors.New(errors.CodeInvalidParam, fmt.Sprintf("权限 %v 不适用于此角色类型", invalidPermIDs))
|
||||
}
|
||||
|
||||
// 批量获取已有权限集合,避免逐条 Exists 查询
|
||||
existingPermIDs, err := s.rolePermissionStore.GetPermIDsByRoleID(ctx, roleID)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(errors.CodeInternalError, err, "获取已有权限失败")
|
||||
}
|
||||
existingSet := make(map[uint]bool, len(existingPermIDs))
|
||||
for _, id := range existingPermIDs {
|
||||
existingSet[id] = true
|
||||
}
|
||||
|
||||
var rps []*model.RolePermission
|
||||
for _, permID := range permIDs {
|
||||
exists, _ := s.rolePermissionStore.Exists(ctx, roleID, permID)
|
||||
if exists {
|
||||
if existingSet[permID] {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user