1
0
mirror of https://github.com/bitwarden/server synced 2026-01-02 00:23:40 +00:00

[PM-25138] Reduce db locking when creating default collections (#6308)

* Use single method for default collection creation

* Use GenerateComb to create sequential guids

* Pre-sort data for SqlBulkCopy

* Add SqlBulkCopy options per dbops recommendations
This commit is contained in:
Thomas Rittson
2025-09-18 14:50:36 +10:00
committed by GitHub
parent e46365ac20
commit 780400fcf9
9 changed files with 172 additions and 33 deletions

View File

@@ -0,0 +1,26 @@
using System.Data.SqlTypes;
namespace Bit.Infrastructure.Dapper.Utilities;
public static class SqlGuidHelpers
{
/// <summary>
/// Sorts the source IEnumerable by the specified Guid property using the <see cref="SqlGuid"/> comparison logic.
/// This is required because MSSQL server compares (and therefore sorts) Guids differently to C#.
/// Ref: https://learn.microsoft.com/en-us/sql/connect/ado-net/sql/compare-guid-uniqueidentifier-values
/// </summary>
public static IOrderedEnumerable<T> OrderBySqlGuid<T>(
this IEnumerable<T> source,
Func<T, Guid> keySelector)
{
return source.OrderBy(x => new SqlGuid(keySelector(x)));
}
/// <inheritdoc cref="OrderBySqlGuid"/>
public static IOrderedEnumerable<T> ThenBySqlGuid<T>(
this IOrderedEnumerable<T> source,
Func<T, Guid> keySelector)
{
return source.ThenBy(x => new SqlGuid(keySelector(x)));
}
}