| | | 1 | | using ArturRios.Data.Relational.Core.Entities; |
| | | 2 | | using ArturRios.Heimdall.Data.Configuration; |
| | | 3 | | using ArturRios.Heimdall.Domain.Enums; |
| | | 4 | | using ArturRios.Heimdall.Domain.Persistence; |
| | | 5 | | using Microsoft.EntityFrameworkCore; |
| | | 6 | | |
| | | 7 | | namespace ArturRios.Heimdall.Data.Persistence; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// <see cref="IScopeCascades" /> on EF Core's <c>ExecuteUpdate</c> and <c>ExecuteDelete</c>: one |
| | | 11 | | /// <c>UPDATE … WHERE NOT is_deleted</c> per kind of member for a logical delete, and |
| | | 12 | | /// <c>DELETE … WHERE id = ANY(…)</c> per batch of ids for a hard one. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <remarks> |
| | | 15 | | /// <para> |
| | | 16 | | /// Nothing in the context intercepts a save to stamp these rows: <c>UpdatedAt</c> and the |
| | | 17 | | /// deletion columns were always set by the handler before a tracked save, and the one hook the |
| | | 18 | | /// base context has — the concurrency stamp of a versioned entity — applies to none of these |
| | | 19 | | /// types. So the statements set exactly what the tracked saves did. The database's own |
| | | 20 | | /// triggers on these tables (the public-id guards) fire for set-based statements as for any |
| | | 21 | | /// other. |
| | | 22 | | /// </para> |
| | | 23 | | /// <para> |
| | | 24 | | /// A hard delete goes by the ids its caller tombstoned, not by the scope, so exactly the rows |
| | | 25 | | /// with a tombstone are removed. Each statement carries at most <see cref="BatchSize" /> of |
| | | 26 | | /// them, so neither a parameter nor a statement grows with the tenant; the rows referencing them |
| | | 27 | | /// (<c>SCOPE_USER</c>, tokens, two-factor rows, credentials) go by their foreign keys' |
| | | 28 | | /// <c>ON DELETE CASCADE</c>. |
| | | 29 | | /// </para> |
| | | 30 | | /// </remarks> |
| | 14 | 31 | | public sealed class ScopeCascades(AppDbContext context) : IScopeCascades |
| | | 32 | | { |
| | | 33 | | /// <summary>How many ids one hard-delete statement carries.</summary> |
| | | 34 | | public const int BatchSize = 2000; |
| | | 35 | | |
| | | 36 | | public async Task MarkMembersDeletedAsync(long scopeId, DateTime at) |
| | | 37 | | { |
| | | 38 | | const int administrative = (int)DeletionKinds.Administrative; |
| | | 39 | | |
| | 8 | 40 | | await context.Persons |
| | 8 | 41 | | .Where(x => !x.IsDeleted && context.ScopeUsers.Any(member => member.PersonId == x.Id && member.ScopeId == sc |
| | 16 | 42 | | .ExecuteUpdateAsync(set => set |
| | 16 | 43 | | .SetProperty(x => x.IsDeleted, true) |
| | 16 | 44 | | .SetProperty(x => x.DeletedAt, at) |
| | 16 | 45 | | .SetProperty(x => x.DeletionKind, administrative) |
| | 16 | 46 | | .SetProperty(x => x.UpdatedAt, at)); |
| | | 47 | | |
| | 8 | 48 | | await context.GoogleUsers |
| | 8 | 49 | | .Where(x => x.ScopeId == scopeId && !x.IsDeleted) |
| | 16 | 50 | | .ExecuteUpdateAsync(set => set |
| | 16 | 51 | | .SetProperty(x => x.IsDeleted, true) |
| | 16 | 52 | | .SetProperty(x => x.DeletedAt, at) |
| | 16 | 53 | | .SetProperty(x => x.DeletionKind, administrative) |
| | 16 | 54 | | .SetProperty(x => x.UpdatedAt, at)); |
| | | 55 | | |
| | 7 | 56 | | await context.Applications |
| | 7 | 57 | | .Where(x => x.ScopeId == scopeId && !x.IsDeleted) |
| | 14 | 58 | | .ExecuteUpdateAsync(set => set |
| | 14 | 59 | | .SetProperty(x => x.IsDeleted, true) |
| | 14 | 60 | | .SetProperty(x => x.UpdatedAt, at)); |
| | 7 | 61 | | } |
| | | 62 | | |
| | | 63 | | public async Task DeleteMembersAsync(ScopeMemberIds members) |
| | | 64 | | { |
| | 4 | 65 | | await DeleteInBatchesAsync(context.Applications, members.ApplicationIds); |
| | 4 | 66 | | await DeleteInBatchesAsync(context.GoogleUsers, members.GoogleUserIds); |
| | 4 | 67 | | await DeleteInBatchesAsync(context.Persons, members.PersonIds); |
| | 4 | 68 | | } |
| | | 69 | | |
| | | 70 | | private static async Task DeleteInBatchesAsync<T>(DbSet<T> set, IReadOnlyCollection<long> ids) |
| | | 71 | | where T : Entity<long> |
| | | 72 | | { |
| | 44 | 73 | | foreach (var batch in ids.Chunk(BatchSize)) |
| | | 74 | | { |
| | 10 | 75 | | await set.Where(x => batch.Contains(x.Id)).ExecuteDeleteAsync(); |
| | | 76 | | } |
| | 12 | 77 | | } |
| | | 78 | | } |