| | | 1 | | using System.Linq.Expressions; |
| | | 2 | | using Elsa.Common.Entities; |
| | | 3 | | using Elsa.Common.Multitenancy; |
| | | 4 | | using Microsoft.EntityFrameworkCore; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Persistence.EFCore.Modules.Alterations; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Compare-and-swap upsert for Alterations EF writes. Ownership is decided in the UPDATE |
| | | 10 | | /// predicate itself (no pre-read guard). A 0-row update falls through to INSERT; a PK |
| | | 11 | | /// collision is the unowned-row refuse path. |
| | | 12 | | /// </summary> |
| | | 13 | | internal static class AlterationTenantOwnedUpsert |
| | | 14 | | { |
| | | 15 | | public static void StampTenantId(Entity entity, string? ambientTenantId) |
| | | 16 | | { |
| | 82 | 17 | | if (entity.TenantId == Tenant.AgnosticTenantId) |
| | 14 | 18 | | return; |
| | | 19 | | |
| | 68 | 20 | | if (entity.TenantId == null && ambientTenantId != null) |
| | 9 | 21 | | entity.TenantId = ambientTenantId; |
| | 68 | 22 | | } |
| | | 23 | | |
| | | 24 | | public static string AmbientTenantId(AlterationsElsaDbContext dbContext) => |
| | 82 | 25 | | dbContext.TenantId ?? Tenant.DefaultTenantId; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// EF translation of <see cref="TenantVisibility.CanReplaceOwnedRow"/>. Named UPDATE |
| | | 29 | | /// matches <c>Target.TenantId</c> to the ambient writer, not a forged source |
| | | 30 | | /// <c>TenantId</c>. <c>*</c> updates still require ambient and source <c>*</c>. |
| | | 31 | | /// </summary> |
| | | 32 | | public static Expression<Func<TEntity, bool>> OwnedId<TEntity>(string id, string? sourceTenantId, string ambientTena |
| | | 33 | | where TEntity : Entity |
| | | 34 | | { |
| | 82 | 35 | | return entity => entity.Id == id && ( |
| | 82 | 36 | | (entity.TenantId == Tenant.AgnosticTenantId && sourceTenantId == Tenant.AgnosticTenantId && ambientTenantId |
| | 82 | 37 | | || (entity.TenantId != Tenant.AgnosticTenantId && ( |
| | 82 | 38 | | entity.TenantId == ambientTenantId |
| | 82 | 39 | | || (entity.TenantId == null && ambientTenantId == Tenant.DefaultTenantId)))); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | public static async Task<bool> InsertIfAbsentAsync<TEntity>( |
| | | 43 | | AlterationsElsaDbContext dbContext, |
| | | 44 | | TEntity entity, |
| | | 45 | | CancellationToken cancellationToken) |
| | | 46 | | where TEntity : Entity |
| | | 47 | | { |
| | 73 | 48 | | dbContext.Entry(entity).State = EntityState.Added; |
| | | 49 | | |
| | | 50 | | try |
| | | 51 | | { |
| | 73 | 52 | | await dbContext.SaveChangesAsync(cancellationToken); |
| | 54 | 53 | | } |
| | 19 | 54 | | catch (DbUpdateException exception) when (DbExceptionClassifier.IsDuplicateKey(exception)) |
| | | 55 | | { |
| | 18 | 56 | | dbContext.Entry(entity).State = EntityState.Detached; |
| | 18 | 57 | | return false; |
| | | 58 | | } |
| | | 59 | | |
| | 54 | 60 | | return true; |
| | 72 | 61 | | } |
| | | 62 | | } |