| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using Elsa.Alterations.Core.Contracts; |
| | | 4 | | using Elsa.Alterations.Core.Entities; |
| | | 5 | | using Elsa.Alterations.Core.Filters; |
| | | 6 | | using Elsa.Alterations.Core.Models; |
| | | 7 | | using Elsa.Alterations.Core.Stores; |
| | | 8 | | using Elsa.Tenants.Options; |
| | | 9 | | using Microsoft.EntityFrameworkCore; |
| | | 10 | | using Microsoft.Extensions.DependencyInjection; |
| | | 11 | | using Microsoft.Extensions.Options; |
| | | 12 | | |
| | | 13 | | namespace Elsa.Persistence.EFCore.Modules.Alterations; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// An EF Core implementation of <see cref="IAlterationPlanStore"/>. |
| | | 17 | | /// </summary> |
| | | 18 | | public class EFCoreAlterationPlanStore : IAlterationPlanStore |
| | | 19 | | { |
| | | 20 | | private readonly EntityStore<AlterationsElsaDbContext, AlterationPlan> _store; |
| | | 21 | | private readonly IAlterationSerializer _alterationSerializer; |
| | | 22 | | private readonly bool _tenantEnabled; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Constructor. |
| | | 26 | | /// </summary> |
| | | 27 | | public EFCoreAlterationPlanStore( |
| | | 28 | | EntityStore<AlterationsElsaDbContext, AlterationPlan> store, |
| | | 29 | | IAlterationSerializer alterationSerializer) |
| | 0 | 30 | | : this(store, alterationSerializer, Options.Create(new TenantsOptions())) |
| | | 31 | | { |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Constructor used by dependency injection. Direct construction through the legacy |
| | | 36 | | /// overload keeps tenancy-aware upsert disabled for compatibility. |
| | | 37 | | /// </summary> |
| | | 38 | | [ActivatorUtilitiesConstructor] |
| | 445 | 39 | | public EFCoreAlterationPlanStore( |
| | 445 | 40 | | EntityStore<AlterationsElsaDbContext, AlterationPlan> store, |
| | 445 | 41 | | IAlterationSerializer alterationSerializer, |
| | 445 | 42 | | IOptions<TenantsOptions> tenantsOptions) |
| | | 43 | | { |
| | 445 | 44 | | _store = store; |
| | 445 | 45 | | _alterationSerializer = alterationSerializer; |
| | 445 | 46 | | _tenantEnabled = tenantsOptions.Value.IsEnabled; |
| | 445 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | | 50 | | public async Task SaveAsync(AlterationPlan record, CancellationToken cancellationToken = default) |
| | | 51 | | { |
| | 42 | 52 | | if (!_tenantEnabled) |
| | | 53 | | { |
| | 4 | 54 | | await _store.SaveAsync(record, OnSaveAsync, cancellationToken); |
| | 4 | 55 | | return; |
| | | 56 | | } |
| | | 57 | | |
| | 38 | 58 | | await using var dbContext = await _store.CreateDbContextAsync(cancellationToken); |
| | 38 | 59 | | var ambientTenantId = AlterationTenantOwnedUpsert.AmbientTenantId(dbContext); |
| | 38 | 60 | | AlterationTenantOwnedUpsert.StampTenantId(record, ambientTenantId); |
| | 38 | 61 | | OnSave(dbContext, record); |
| | | 62 | | |
| | 38 | 63 | | var status = record.Status; |
| | 38 | 64 | | var createdAt = record.CreatedAt; |
| | 38 | 65 | | var startedAt = record.StartedAt; |
| | 38 | 66 | | var completedAt = record.CompletedAt; |
| | 38 | 67 | | var serializedAlterations = dbContext.Entry(record).Property<string>("SerializedAlterations").CurrentValue; |
| | 38 | 68 | | var serializedFilter = dbContext.Entry(record).Property<string>("SerializedWorkflowInstanceFilter").CurrentValue |
| | | 69 | | |
| | 38 | 70 | | var query = dbContext.Set<AlterationPlan>() |
| | 38 | 71 | | .IgnoreQueryFilters() |
| | 38 | 72 | | .Where(AlterationTenantOwnedUpsert.OwnedId<AlterationPlan>(record.Id, record.TenantId, ambientTenantId)); |
| | | 73 | | |
| | | 74 | | // Inline lambda so net8/net9 bind SetPropertyCalls and net10 binds UpdateSettersBuilder. |
| | 47 | 75 | | Task<int> UpdateOwnedAsync() => query.ExecuteUpdateAsync( |
| | 47 | 76 | | setters => setters |
| | 47 | 77 | | .SetProperty(plan => plan.Status, status) |
| | 47 | 78 | | .SetProperty(plan => plan.CreatedAt, createdAt) |
| | 47 | 79 | | .SetProperty(plan => plan.StartedAt, startedAt) |
| | 47 | 80 | | .SetProperty(plan => plan.CompletedAt, completedAt) |
| | 47 | 81 | | .SetProperty(plan => EF.Property<string>(plan, "SerializedAlterations"), serializedAlterations) |
| | 47 | 82 | | .SetProperty(plan => EF.Property<string>(plan, "SerializedWorkflowInstanceFilter"), serializedFilter), |
| | 47 | 83 | | cancellationToken); |
| | | 84 | | |
| | 38 | 85 | | var updated = await _store.ExecuteWithDbExceptionHandlingAsync(UpdateOwnedAsync, cancellationToken); |
| | | 86 | | |
| | 37 | 87 | | if (updated == 0) |
| | | 88 | | { |
| | 33 | 89 | | var inserted = await _store.ExecuteWithDbExceptionHandlingAsync( |
| | 33 | 90 | | () => AlterationTenantOwnedUpsert.InsertIfAbsentAsync(dbContext, record, cancellationToken), |
| | 33 | 91 | | cancellationToken); |
| | 33 | 92 | | if (!inserted) |
| | | 93 | | { |
| | 9 | 94 | | var retried = await _store.ExecuteWithDbExceptionHandlingAsync(UpdateOwnedAsync, cancellationToken); |
| | | 95 | | |
| | 9 | 96 | | if (retried == 0) |
| | 8 | 97 | | throw AlterationStoreConflict.HiddenPlanId(record.Id); |
| | | 98 | | } |
| | | 99 | | } |
| | 33 | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <inheritdoc /> |
| | | 103 | | [RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Deserialize<TValue>(String, JsonSerializerOptions)" |
| | | 104 | | public async Task<AlterationPlan?> FindAsync(AlterationPlanFilter filter, CancellationToken cancellationToken = defa |
| | | 105 | | { |
| | 32 | 106 | | return await _store.FindAsync(filter.Apply, OnLoadAsync, cancellationToken); |
| | 32 | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <inheritdoc /> |
| | | 110 | | public async Task<long> CountAsync(AlterationPlanFilter filter, CancellationToken cancellationToken = default) |
| | | 111 | | { |
| | 7 | 112 | | return await _store.CountAsync(filter.Apply, cancellationToken); |
| | 7 | 113 | | } |
| | | 114 | | |
| | | 115 | | private void OnSave(AlterationsElsaDbContext elsaDbContext, AlterationPlan entity) |
| | | 116 | | { |
| | 42 | 117 | | elsaDbContext.Entry(entity).Property("SerializedAlterations").CurrentValue = _alterationSerializer.SerializeMany |
| | 42 | 118 | | elsaDbContext.Entry(entity).Property("SerializedWorkflowInstanceFilter").CurrentValue = JsonSerializer.Serialize |
| | 42 | 119 | | } |
| | | 120 | | |
| | | 121 | | private ValueTask OnSaveAsync(AlterationsElsaDbContext dbContext, AlterationPlan entity, CancellationToken cancellat |
| | | 122 | | { |
| | 4 | 123 | | OnSave(dbContext, entity); |
| | 4 | 124 | | return default; |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | [RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Deserialize<TValue>(String, JsonSerializerOptions)" |
| | | 128 | | private ValueTask OnLoadAsync(AlterationsElsaDbContext elsaDbContext, AlterationPlan? entity, CancellationToken canc |
| | | 129 | | { |
| | 23 | 130 | | if (entity is null) |
| | 0 | 131 | | return default; |
| | | 132 | | |
| | 23 | 133 | | var alterationsJson = elsaDbContext.Entry(entity).Property<string>("SerializedAlterations").CurrentValue; |
| | 23 | 134 | | var workflowInstanceFilterJson = elsaDbContext.Entry(entity).Property<string>("SerializedWorkflowInstanceFilter" |
| | 23 | 135 | | entity.Alterations = _alterationSerializer.DeserializeMany(alterationsJson).ToList(); |
| | 23 | 136 | | entity.WorkflowInstanceFilter = JsonSerializer.Deserialize<AlterationWorkflowInstanceFilter>(workflowInstanceFil |
| | | 137 | | |
| | 23 | 138 | | return default; |
| | | 139 | | } |
| | | 140 | | } |