< Summary

Information
Class: Elsa.Persistence.EFCore.Modules.Management.EFCoreWorkflowDefinitionRegistryGenerationStore
Assembly: Elsa.Persistence.EFCore
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Persistence.EFCore/Modules/Management/WorkflowDefinitionRegistryGenerationStore.cs
Line coverage
96%
Covered lines: 31
Uncovered lines: 1
Coverable lines: 32
Total lines: 65
Line coverage: 96.8%
Branch coverage
66%
Covered branches: 4
Total branches: 6
Branch coverage: 66.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_IsShared()100%11100%
IncrementAsync()75%4495.45%
GetGenerationAsync()100%11100%
NormalizeTenantId(...)50%22100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Persistence.EFCore/Modules/Management/WorkflowDefinitionRegistryGenerationStore.cs

#LineLine coverage
 1using Elsa.Common.Multitenancy;
 2using Elsa.Workflows.Management.Contracts;
 3using Elsa.Workflows.Management.Entities;
 4using Microsoft.EntityFrameworkCore;
 5
 6namespace Elsa.Persistence.EFCore.Modules.Management;
 7
 8/// <summary>
 9/// Stores workflow-definition registry generations in the shared management database.
 10/// </summary>
 40411public class EFCoreWorkflowDefinitionRegistryGenerationStore(ManagementElsaDbContext dbContext) : IWorkflowDefinitionReg
 12{
 13    /// <inheritdoc />
 77214    public bool IsShared => true;
 15
 16    /// <inheritdoc />
 17    public async Task<long> IncrementAsync(string? tenantId, CancellationToken cancellationToken = default)
 18    {
 6519        var normalizedTenantId = NormalizeTenantId(tenantId);
 20
 221        while (true)
 22        {
 6723            var updated = await dbContext.WorkflowDefinitionRegistryGenerations
 6724                .Where(x => x.TenantId == normalizedTenantId)
 13425                .ExecuteUpdateAsync(setters => setters.SetProperty(x => x.Generation, x => x.Generation + 1), cancellati
 26
 6727            if (updated > 0)
 5928                return await GetGenerationAsync(normalizedTenantId, cancellationToken);
 29
 830            var entity = new WorkflowDefinitionRegistryGeneration
 831            {
 832                TenantId = normalizedTenantId,
 833                Generation = 1
 834            };
 835            dbContext.WorkflowDefinitionRegistryGenerations.Add(entity);
 36
 37            try
 38            {
 839                await dbContext.SaveChangesAsync(cancellationToken);
 640                return 1;
 41            }
 242            catch (DbUpdateException exception) when (DbExceptionClassifier.IsDuplicateKey(exception))
 43            {
 44                // Another node may have inserted the first generation concurrently.
 245                dbContext.Entry(entity).State = EntityState.Detached;
 246                var insertedByAnotherNode = await dbContext.WorkflowDefinitionRegistryGenerations
 247                    .AnyAsync(x => x.TenantId == normalizedTenantId, cancellationToken);
 248                if (!insertedByAnotherNode)
 049                    throw;
 50            }
 251        }
 6552    }
 53
 54    /// <inheritdoc />
 55    public async Task<long> GetGenerationAsync(string? tenantId, CancellationToken cancellationToken = default)
 56    {
 161357        var normalizedTenantId = NormalizeTenantId(tenantId);
 161358        return await dbContext.WorkflowDefinitionRegistryGenerations
 161359            .Where(x => x.TenantId == normalizedTenantId)
 161360            .Select(x => x.Generation)
 161361            .SingleOrDefaultAsync(cancellationToken);
 161362    }
 63
 167864    private static string NormalizeTenantId(string? tenantId) => tenantId ?? Tenant.DefaultTenantId;
 65}