| | | 1 | | using Elsa.Common.Multitenancy; |
| | | 2 | | using Elsa.Workflows.Management.Contracts; |
| | | 3 | | using Elsa.Workflows.Management.Entities; |
| | | 4 | | using Microsoft.EntityFrameworkCore; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Persistence.EFCore.Modules.Management; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Stores workflow-definition registry generations in the shared management database. |
| | | 10 | | /// </summary> |
| | 404 | 11 | | public class EFCoreWorkflowDefinitionRegistryGenerationStore(ManagementElsaDbContext dbContext) : IWorkflowDefinitionReg |
| | | 12 | | { |
| | | 13 | | /// <inheritdoc /> |
| | 772 | 14 | | public bool IsShared => true; |
| | | 15 | | |
| | | 16 | | /// <inheritdoc /> |
| | | 17 | | public async Task<long> IncrementAsync(string? tenantId, CancellationToken cancellationToken = default) |
| | | 18 | | { |
| | 65 | 19 | | var normalizedTenantId = NormalizeTenantId(tenantId); |
| | | 20 | | |
| | 2 | 21 | | while (true) |
| | | 22 | | { |
| | 67 | 23 | | var updated = await dbContext.WorkflowDefinitionRegistryGenerations |
| | 67 | 24 | | .Where(x => x.TenantId == normalizedTenantId) |
| | 134 | 25 | | .ExecuteUpdateAsync(setters => setters.SetProperty(x => x.Generation, x => x.Generation + 1), cancellati |
| | | 26 | | |
| | 67 | 27 | | if (updated > 0) |
| | 59 | 28 | | return await GetGenerationAsync(normalizedTenantId, cancellationToken); |
| | | 29 | | |
| | 8 | 30 | | var entity = new WorkflowDefinitionRegistryGeneration |
| | 8 | 31 | | { |
| | 8 | 32 | | TenantId = normalizedTenantId, |
| | 8 | 33 | | Generation = 1 |
| | 8 | 34 | | }; |
| | 8 | 35 | | dbContext.WorkflowDefinitionRegistryGenerations.Add(entity); |
| | | 36 | | |
| | | 37 | | try |
| | | 38 | | { |
| | 8 | 39 | | await dbContext.SaveChangesAsync(cancellationToken); |
| | 6 | 40 | | return 1; |
| | | 41 | | } |
| | 2 | 42 | | catch (DbUpdateException exception) when (DbExceptionClassifier.IsDuplicateKey(exception)) |
| | | 43 | | { |
| | | 44 | | // Another node may have inserted the first generation concurrently. |
| | 2 | 45 | | dbContext.Entry(entity).State = EntityState.Detached; |
| | 2 | 46 | | var insertedByAnotherNode = await dbContext.WorkflowDefinitionRegistryGenerations |
| | 2 | 47 | | .AnyAsync(x => x.TenantId == normalizedTenantId, cancellationToken); |
| | 2 | 48 | | if (!insertedByAnotherNode) |
| | 0 | 49 | | throw; |
| | | 50 | | } |
| | 2 | 51 | | } |
| | 65 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <inheritdoc /> |
| | | 55 | | public async Task<long> GetGenerationAsync(string? tenantId, CancellationToken cancellationToken = default) |
| | | 56 | | { |
| | 1613 | 57 | | var normalizedTenantId = NormalizeTenantId(tenantId); |
| | 1613 | 58 | | return await dbContext.WorkflowDefinitionRegistryGenerations |
| | 1613 | 59 | | .Where(x => x.TenantId == normalizedTenantId) |
| | 1613 | 60 | | .Select(x => x.Generation) |
| | 1613 | 61 | | .SingleOrDefaultAsync(cancellationToken); |
| | 1613 | 62 | | } |
| | | 63 | | |
| | 1678 | 64 | | private static string NormalizeTenantId(string? tenantId) => tenantId ?? Tenant.DefaultTenantId; |
| | | 65 | | } |