| | | 1 | | namespace ZhEaIsNsAaBn.DbSeeder; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// The write rules of §6.2, provider-neutral. Insert: created date / by / user (date <b>always</b> from the clock, also |
| | | 5 | | /// has a database default) and the soft-delete flag <see langword="false"/>. Update (only when a data column changed): |
| | | 6 | | /// date / by / user. The user is <c>SystemUser( id, name )</c>; the time is the configured clock's local time, one valu |
| | | 7 | | /// </summary> |
| | | 8 | | internal sealed class SeedRowWriter |
| | | 9 | | { |
| | | 10 | | private readonly SeedOptions _options; |
| | | 11 | | private readonly DateTime _now; |
| | | 12 | | |
| | | 13 | | |
| | 464 | 14 | | public SeedRowWriter( SeedOptions options, DateTime now ) |
| | | 15 | | { |
| | 464 | 16 | | _options = options; |
| | 464 | 17 | | _now = now; |
| | 464 | 18 | | } |
| | | 19 | | |
| | | 20 | | |
| | | 21 | | /// The upserts of one table: one write, or one per dependency level for a self-referencing table. |
| | | 22 | | /// <returns>The writes; otherwise <c>SystemUserRequired</c> or <c>CycleDetected</c>.</returns> |
| | 945 | 23 | | public LayerResponse<IReadOnlyList<SeedTableWrite>> Upserts( SeedBoundTable table, string migrationId ) => Upserts( |
| | | 24 | | |
| | | 25 | | /// The upserts of arbitrary rows of the table (e.g. the <c>Before</c> rows of a rollback), same rules. |
| | | 26 | | public LayerResponse<IReadOnlyList<SeedTableWrite>> Upserts( SeedBoundTable table, IReadOnlyList<SeedColumn> columns |
| | | 27 | | { |
| | | 28 | | // A new row starts not deleted (the flag is managed, never in data, and often NOT NULL without a database defau |
| | 1023 | 29 | | var insert = Values( table, (SeedRole.CreatedDate, _ => _now), (SeedRole.CreatedById, Id), (SeedRole.CreatedByUs |
| | 1023 | 30 | | (SeedRole.SoftDeleteFlag, _ => false) ); |
| | | 31 | | |
| | 1023 | 32 | | if ( insert.IsFailure ) { return LayerResponse<IReadOnlyList<SeedTableWrite>>.Failure( insert ); } |
| | | 33 | | |
| | | 34 | | |
| | 1017 | 35 | | var update = Values( table, (SeedRole.UpdatedDate, _ => _now), (SeedRole.UpdatedById, Id), (SeedRole.UpdatedByUs |
| | | 36 | | |
| | 1017 | 37 | | if ( update.IsFailure ) { return LayerResponse<IReadOnlyList<SeedTableWrite>>.Failure( update ); } |
| | | 38 | | |
| | | 39 | | |
| | 1017 | 40 | | var written = columns.Select( c => c.PropertyName ).ToHashSet( StringComparer.Ordinal ); |
| | 1017 | 41 | | var selfReferences = table.SelfReferences.Where( k => k.Properties.Concat( k.PrincipalKey.Properties ).All( p = |
| | 1017 | 42 | | var (levels, cycle) = SeedRowLevels.Split( columns, rows, selfReferences ); |
| | | 43 | | |
| | 1017 | 44 | | if ( levels is null ) |
| | | 45 | | { |
| | 3 | 46 | | return SeedErrors.CycleDetected( migrationId, cycle.Select( r => $"{table.EntityType.Name}[{r}]" ).ToList() |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | |
| | 1014 | 50 | | return levels.Select( rows => new SeedTableWrite |
| | 1014 | 51 | | { |
| | 1014 | 52 | | Table = table.Table, |
| | 1014 | 53 | | Columns = columns, |
| | 1014 | 54 | | Rows = rows, |
| | 1014 | 55 | | InsertValues = insert.Result!, |
| | 1014 | 56 | | UpdateValues = update.Result! |
| | 1014 | 57 | | } ) |
| | 1014 | 58 | | .ToList(); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | /// The flag and the deleted fields of a soft delete. |
| | | 62 | | /// <returns>The values; otherwise <c>SystemUserRequired</c>.</returns> |
| | | 63 | | public LayerResponse<IReadOnlyList<SeedColumnValue>> SoftDeleteValues( SeedBoundTable table ) |
| | 189 | 64 | | => Values( table, (SeedRole.SoftDeleteFlag, _ => true), (SeedRole.DeletedDate, _ => _now), (SeedRole.DeletedById |
| | | 65 | | |
| | | 66 | | |
| | | 67 | | /// Bringing a soft-deleted row back: the flag <see langword="false"/>, the deleted fields cleared (where nullable), |
| | | 68 | | /// <returns>The values; otherwise <c>SystemUserRequired</c>.</returns> |
| | | 69 | | public LayerResponse<IReadOnlyList<SeedColumnValue>> RestoreValues( SeedBoundTable table ) |
| | | 70 | | { |
| | 62 | 71 | | var cleared = table.Managed |
| | 62 | 72 | | .Where( m => m.Key is SeedRole.DeletedDate or SeedRole.DeletedById or SeedRole.DeletedByUser |
| | 62 | 73 | | .Select( m => new SeedColumnValue { Column = m.Value, Value = null } ); |
| | | 74 | | |
| | 62 | 75 | | var stamped = Values( table, (SeedRole.SoftDeleteFlag, _ => false), (SeedRole.UpdatedDate, _ => _now), (SeedRole |
| | 62 | 76 | | (SeedRole.UpdatedByUser, _ => _options.SystemUserName) ); |
| | | 77 | | |
| | 62 | 78 | | return stamped.IsFailure ? stamped : stamped.Result!.Concat( cleared ).ToList(); |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | private object? Id( SeedColumn column ) |
| | | 82 | | { |
| | 847 | 83 | | var expected = Nullable.GetUnderlyingType( column.Property.ClrType ) ?? column.Property.ClrType; |
| | | 84 | | |
| | 847 | 85 | | return _options.SystemUserId is null || _options.SystemUserId.GetType() == expected |
| | 847 | 86 | | ? _options.SystemUserId |
| | 847 | 87 | | : throw new InvalidOperationException( $"SystemUser's key is {_options.SystemUserId.GetType().Name} b |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | private LayerResponse<IReadOnlyList<SeedColumnValue>> Values( SeedBoundTable table, params (SeedRole Role, Func<Seed |
| | | 91 | | { |
| | 2291 | 92 | | var values = new List<SeedColumnValue>(); |
| | | 93 | | |
| | | 94 | | |
| | 20837 | 95 | | foreach ( var (role, value) in roles ) |
| | | 96 | | { |
| | 8132 | 97 | | if ( table[ role ] is not { } column ) { continue; } |
| | | 98 | | |
| | 3629 | 99 | | if ( role is SeedRole.CreatedById or SeedRole.CreatedByUser or SeedRole.UpdatedById or SeedRole.UpdatedByUse |
| | 3629 | 100 | | && _options.SystemUserId is null ) |
| | | 101 | | { |
| | 6 | 102 | | return SeedErrors.SystemUserRequired( table.EntityType.Name ); |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | |
| | 3623 | 106 | | var raw = value( column ); |
| | | 107 | | |
| | 3620 | 108 | | values.Add( new SeedColumnValue |
| | 3620 | 109 | | { |
| | 3620 | 110 | | Column = column, |
| | 3620 | 111 | | Value = raw is null ? null : column.Property.GetTypeMapping().Converter?.ConvertToProv |
| | 3620 | 112 | | IsClock = role is SeedRole.CreatedDate or SeedRole.UpdatedDate or SeedRole.DeletedDate |
| | 3620 | 113 | | } ); |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | |
| | 2282 | 117 | | return values; |
| | | 118 | | } |
| | | 119 | | } |