1
0
mirror of https://github.com/bitwarden/server synced 2026-02-28 10:23:24 +00:00

Adding .AsNoTracking() to Project and SecretRepository to improve performance (#7096)

This commit is contained in:
cd-bitwarden
2026-02-27 11:02:48 -05:00
committed by GitHub
parent 1e7f7acb97
commit 5da65d517f
2 changed files with 20 additions and 12 deletions

View File

@@ -22,6 +22,7 @@ public class ProjectRepository : Repository<Core.SecretsManager.Entities.Project
{
var dbContext = GetDatabaseContext(scope);
var project = await dbContext.Project
.AsNoTracking()
.Where(c => c.Id == id && c.DeletedDate == null)
.FirstOrDefaultAsync();
return Mapper.Map<Core.SecretsManager.Entities.Project>(project);
@@ -36,7 +37,7 @@ public class ProjectRepository : Repository<Core.SecretsManager.Entities.Project
using var scope = ServiceScopeFactory.CreateScope();
var dbContext = GetDatabaseContext(scope);
var query = dbContext.Project.Where(p => p.OrganizationId == organizationId && p.DeletedDate == null).OrderBy(p => p.RevisionDate);
var query = dbContext.Project.AsNoTracking().Where(p => p.OrganizationId == organizationId && p.DeletedDate == null).OrderBy(p => p.RevisionDate);
var projects = ProjectToPermissionDetails(query, userId, accessType);
@@ -58,7 +59,7 @@ public class ProjectRepository : Repository<Core.SecretsManager.Entities.Project
{
using var scope = ServiceScopeFactory.CreateScope();
var dbContext = GetDatabaseContext(scope);
var query = dbContext.Project.Where(p => p.OrganizationId == organizationId && p.DeletedDate == null);
var query = dbContext.Project.AsNoTracking().Where(p => p.OrganizationId == organizationId && p.DeletedDate == null);
query = accessType switch
{
@@ -118,6 +119,7 @@ public class ProjectRepository : Repository<Core.SecretsManager.Entities.Project
{
var dbContext = GetDatabaseContext(scope);
var projects = await dbContext.Project
.AsNoTracking()
.Include(p => p.Secrets)
.Where(c => ids.Contains(c.Id) && c.DeletedDate == null)
.ToListAsync();
@@ -141,6 +143,7 @@ public class ProjectRepository : Repository<Core.SecretsManager.Entities.Project
var dbContext = GetDatabaseContext(scope);
var projectQuery = dbContext.Project
.AsNoTracking()
.Where(s => s.Id == id);
var accessQuery = BuildProjectAccessQuery(projectQuery, userId, accessType);
@@ -153,7 +156,7 @@ public class ProjectRepository : Repository<Core.SecretsManager.Entities.Project
{
using var scope = ServiceScopeFactory.CreateScope();
var dbContext = GetDatabaseContext(scope);
var results = await dbContext.Project.Where(p => p.OrganizationId == organizationId && projectIds.Contains(p.Id)).ToListAsync();
var results = await dbContext.Project.AsNoTracking().Where(p => p.OrganizationId == organizationId && projectIds.Contains(p.Id)).ToListAsync();
return projectIds.Count == results.Count;
}
@@ -166,7 +169,7 @@ public class ProjectRepository : Repository<Core.SecretsManager.Entities.Project
await using var scope = ServiceScopeFactory.CreateAsyncScope();
var dbContext = GetDatabaseContext(scope);
var projectsQuery = dbContext.Project.Where(p => projectIds.Contains(p.Id));
var projectsQuery = dbContext.Project.AsNoTracking().Where(p => projectIds.Contains(p.Id));
var accessQuery = BuildProjectAccessQuery(projectsQuery, userId, accessType);
return await accessQuery.ToDictionaryAsync(pa => pa.Id, pa => (pa.Read, pa.Write));
@@ -177,7 +180,7 @@ public class ProjectRepository : Repository<Core.SecretsManager.Entities.Project
{
await using var scope = ServiceScopeFactory.CreateAsyncScope();
var dbContext = GetDatabaseContext(scope);
var query = dbContext.Project.Where(p => p.OrganizationId == organizationId && p.DeletedDate == null);
var query = dbContext.Project.AsNoTracking().Where(p => p.OrganizationId == organizationId && p.DeletedDate == null);
query = accessType switch
{
@@ -193,7 +196,7 @@ public class ProjectRepository : Repository<Core.SecretsManager.Entities.Project
{
await using var scope = ServiceScopeFactory.CreateAsyncScope();
var dbContext = GetDatabaseContext(scope);
var query = dbContext.Project.Where(p => p.Id == projectId && p.DeletedDate == null);
var query = dbContext.Project.AsNoTracking().Where(p => p.Id == projectId && p.DeletedDate == null);
var queryReadAccess = accessType switch
{

View File

@@ -25,6 +25,7 @@ public class SecretRepository : Repository<Core.SecretsManager.Entities.Secret,
{
var dbContext = GetDatabaseContext(scope);
var secret = await dbContext.Secret
.AsNoTracking()
.Include("Projects")
.Where(c => c.Id == id && c.DeletedDate == null)
.FirstOrDefaultAsync();
@@ -38,6 +39,7 @@ public class SecretRepository : Repository<Core.SecretsManager.Entities.Secret,
{
var dbContext = GetDatabaseContext(scope);
var secrets = await dbContext.Secret
.AsNoTracking()
.Where(c => ids.Contains(c.Id) && c.DeletedDate == null)
.Include(c => c.Projects)
.ToListAsync();
@@ -51,6 +53,7 @@ public class SecretRepository : Repository<Core.SecretsManager.Entities.Secret,
{
var dbContext = GetDatabaseContext(scope);
var secrets = await dbContext.Secret
.AsNoTracking()
.Where(c => ids.Contains(c.Id) && c.DeletedDate != null)
.Include(c => c.Projects)
.ToListAsync();
@@ -64,6 +67,7 @@ public class SecretRepository : Repository<Core.SecretsManager.Entities.Secret,
await using var scope = ServiceScopeFactory.CreateAsyncScope();
var dbContext = GetDatabaseContext(scope);
var query = dbContext.Secret
.AsNoTracking()
.Include(c => c.Projects)
.Where(c => c.OrganizationId == organizationId && c.DeletedDate == null);
@@ -88,12 +92,12 @@ public class SecretRepository : Repository<Core.SecretsManager.Entities.Secret,
var dbContext = GetDatabaseContext(scope);
var query = dbContext.Secret
.AsNoTracking()
.Include(c => c.Projects)
.Where(c => c.OrganizationId == organizationId && c.DeletedDate == null)
.OrderBy(s => s.RevisionDate);
var secrets = SecretToPermissionDetails(query, userId, accessType);
return await secrets.ToListAsync();
}
@@ -113,11 +117,11 @@ public class SecretRepository : Repository<Core.SecretsManager.Entities.Secret,
{
var dbContext = GetDatabaseContext(scope);
var secrets = await dbContext.Secret
.AsNoTracking()
.Where(s => ids.Contains(s.Id) && s.OrganizationId == organizationId && s.DeletedDate != null)
.Include("Projects")
.OrderBy(c => c.RevisionDate)
.ToListAsync();
return Mapper.Map<List<Core.SecretsManager.Entities.Secret>>(secrets);
}
}
@@ -128,6 +132,7 @@ public class SecretRepository : Repository<Core.SecretsManager.Entities.Secret,
{
var dbContext = GetDatabaseContext(scope);
var secrets = await dbContext.Secret
.AsNoTracking()
.Where(c => c.OrganizationId == organizationId && c.DeletedDate != null)
.Include("Projects")
.OrderBy(c => c.RevisionDate)
@@ -147,11 +152,10 @@ public class SecretRepository : Repository<Core.SecretsManager.Entities.Secret,
{
using var scope = ServiceScopeFactory.CreateScope();
var dbContext = GetDatabaseContext(scope);
var query = dbContext.Secret.Include(s => s.Projects)
var query = dbContext.Secret.AsNoTracking().Include(s => s.Projects)
.Where(s => s.Projects.Any(p => p.Id == projectId) && s.DeletedDate == null);
var secrets = SecretToPermissionDetails(query, userId, accessType);
return await secrets.ToListAsync();
}
@@ -307,12 +311,12 @@ public class SecretRepository : Repository<Core.SecretsManager.Entities.Secret,
var dbContext = GetDatabaseContext(scope);
var secret = dbContext.Secret
.AsNoTracking()
.Where(s => s.Id == id);
var query = BuildSecretAccessQuery(secret, userId, accessType);
var policy = await query.FirstOrDefaultAsync();
return policy == null ? (false, false) : (policy.Read, policy.Write);
}
@@ -325,6 +329,7 @@ public class SecretRepository : Repository<Core.SecretsManager.Entities.Secret,
var dbContext = GetDatabaseContext(scope);
var secrets = dbContext.Secret
.AsNoTracking()
.Where(s => ids.Contains(s.Id));
var accessQuery = BuildSecretAccessQuery(secrets, userId, accessType);
@@ -347,7 +352,7 @@ public class SecretRepository : Repository<Core.SecretsManager.Entities.Secret,
{
await using var scope = ServiceScopeFactory.CreateAsyncScope();
var dbContext = GetDatabaseContext(scope);
var query = dbContext.Secret.Where(s => s.OrganizationId == organizationId && s.DeletedDate == null);
var query = dbContext.Secret.AsNoTracking().Where(s => s.OrganizationId == organizationId && s.DeletedDate == null);
query = accessType switch
{