Skip to content

Commit e9ae3cf

Browse files
Repositories: Retrieve users for groups in batches to fix UserService.GetAllInGroup failing on too many users in a group (#20298)
* fix: Identified everywhere the bugs happen and implemented the InGroupOf() extension to successfully batch the SQL queries * Added helper function in case this batching functionality is in future scopes * Accidently deleted a groupIds.Any() check while adding BatchFetch helper function * Removed helper function and instead utilising built in FetchByGroups extension method
1 parent f5da0db commit e9ae3cf

File tree

1 file changed

+22
-25
lines changed

1 file changed

+22
-25
lines changed

src/Umbraco.Infrastructure/Persistence/Repositories/Implement/UserRepository.cs

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -409,17 +409,14 @@ private void PerformGetReferencedDtos(List<UserDto> dtos)
409409
Sql<ISqlContext> sql;
410410
try
411411
{
412-
sql = SqlContext.Sql()
413-
.Select<UserGroupDto>(x => x.Id, x => x.Key)
414-
.From<UserGroupDto>()
415-
.InnerJoin<User2UserGroupDto>().On<UserGroupDto, User2UserGroupDto>((left, right) => left.Id == right.UserGroupId)
416-
.WhereIn<User2UserGroupDto>(x => x.UserId, userIds);
417-
418-
List<UserGroupDto>? userGroups = Database.Fetch<UserGroupDto>(sql);
419-
420-
421-
groupKeys = userGroups.Select(x => x.Key).ToList();
422-
412+
groupKeys = Database.FetchByGroups<UserGroupDto, int>(userIds, Constants.Sql.MaxParameterCount, ints =>
413+
{
414+
return SqlContext.Sql()
415+
.Select<UserGroupDto>(x => x.Id, x => x.Key)
416+
.From<UserGroupDto>()
417+
.InnerJoin<User2UserGroupDto>().On<UserGroupDto, User2UserGroupDto>((left, right) => left.Id == right.UserGroupId)
418+
.WhereIn<User2UserGroupDto>(x => x.UserId, ints);
419+
}).Select(x => x.Key).ToList();
423420
}
424421
catch (DbException)
425422
{
@@ -433,20 +430,20 @@ private void PerformGetReferencedDtos(List<UserDto> dtos)
433430

434431

435432
// get users2groups
436-
sql = SqlContext.Sql()
437-
.Select<User2UserGroupDto>()
438-
.From<User2UserGroupDto>()
439-
.WhereIn<User2UserGroupDto>(x => x.UserId, userIds);
440-
441-
List<User2UserGroupDto>? user2Groups = Database.Fetch<User2UserGroupDto>(sql);
433+
List<User2UserGroupDto>? user2Groups = Database.FetchByGroups<User2UserGroupDto, int>(userIds, Constants.Sql.MaxParameterCount, ints =>
434+
{
435+
return SqlContext.Sql()
436+
.Select<User2UserGroupDto>()
437+
.From<User2UserGroupDto>()
438+
.WhereIn<User2UserGroupDto>(x => x.UserId, ints);
439+
}).ToList();
442440

443441
if (groupIds.Any() is false)
444442
{
445443
//this can happen if we are upgrading, so we try do read from this table, as we counn't because of the key earlier
446444
groupIds = user2Groups.Select(x => x.UserGroupId).Distinct().ToList();
447445
}
448446

449-
450447
// get groups
451448
// We wrap this in a try-catch, as this might throw errors when you try to login before having migrated your database
452449
Dictionary<int, UserGroupDto> groups;
@@ -485,13 +482,13 @@ private void PerformGetReferencedDtos(List<UserDto> dtos)
485482
.ToDictionary(x => x.Key, x => x);
486483

487484
// get start nodes
488-
489-
sql = SqlContext.Sql()
490-
.Select<UserStartNodeDto>()
491-
.From<UserStartNodeDto>()
492-
.WhereIn<UserStartNodeDto>(x => x.UserId, userIds);
493-
494-
List<UserStartNodeDto>? startNodes = Database.Fetch<UserStartNodeDto>(sql);
485+
List<UserStartNodeDto>? startNodes = Database.FetchByGroups<UserStartNodeDto, int>(userIds, Constants.Sql.MaxParameterCount, ints =>
486+
{
487+
return SqlContext.Sql()
488+
.Select<UserStartNodeDto>()
489+
.From<UserStartNodeDto>()
490+
.WhereIn<UserStartNodeDto>(x => x.UserId, ints);
491+
}).ToList();
495492

496493
// get groups2languages
497494

0 commit comments

Comments
 (0)