| | | 1 | | namespace ZhEaIsNsAaBn.DbSeeder; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Turns a bound migration into its ordered steps (§6.1 – §6.2): upserts principals first (self-referencing rows by lev |
| | | 5 | | /// restores, then per table dependents first the <c>Delete</c> rows (entity delete mode: soft when <c>Auto</c> and soft |
| | | 6 | | /// <c>SoftDelete</c> rows. The runner executes these steps; the SQL script is generated from the same ones. |
| | | 7 | | /// </summary> |
| | | 8 | | internal static class SeedStepPlanner |
| | | 9 | | { |
| | | 10 | | /// <returns>The steps; otherwise <c>SystemUserRequired</c>, <c>CycleDetected</c> or <c>SoftDeleteNotSupported</c>.< |
| | | 11 | | public static LayerResponse<IReadOnlyList<SeedStep>> Plan( IReadOnlyList<SeedBoundTable> tables, SeedRowWriter write |
| | | 12 | | { |
| | 343 | 13 | | var steps = new List<SeedStep>(); |
| | | 14 | | |
| | | 15 | | |
| | 2540 | 16 | | foreach ( var table in tables.Where( t => t.UpsertRows.Count > 0 ) ) |
| | | 17 | | { |
| | 927 | 18 | | var writes = writer.Upserts( table, migrationId ); |
| | | 19 | | |
| | 927 | 20 | | if ( writes.IsFailure ) { return LayerResponse<IReadOnlyList<SeedStep>>.Failure( writes ); } |
| | | 21 | | |
| | 927 | 22 | | steps.AddRange( writes.Result!.Select( w => new SeedStep { Kind = SeedStepKind.Upsert, Table = table.Table, |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | |
| | 756 | 26 | | foreach ( var table in tables.Where( t => t.RestoreKeys.Count > 0 ) ) |
| | | 27 | | { |
| | 35 | 28 | | if ( table.Table.SoftDeleteFlag is null ) { return SeedErrors.SoftDeleteNotSupported( table.EntityType.Name |
| | | 29 | | |
| | | 30 | | |
| | 35 | 31 | | var values = writer.RestoreValues( table ); |
| | | 32 | | |
| | 35 | 33 | | if ( values.IsFailure ) { return LayerResponse<IReadOnlyList<SeedStep>>.Failure( values ); } |
| | | 34 | | |
| | 35 | 35 | | steps.Add( new SeedStep { Kind = SeedStepKind.Restore, Table = table.Table, Keys = Keys( table, table.Restor |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | |
| | 2736 | 39 | | foreach ( var table in tables.Reverse() ) |
| | | 40 | | { |
| | | 41 | | // Delete follows the entity's delete mode; SoftDelete is always soft. |
| | 6150 | 42 | | foreach ( var (keys, mode) in new[] { ( table.DeleteKeys, table.Roles.DeleteMode ), ( table.SoftDeleteKeys, |
| | | 43 | | { |
| | 2050 | 44 | | if ( keys.Count == 0 ) { continue; } |
| | | 45 | | |
| | | 46 | | |
| | 207 | 47 | | var soft = mode == SeedDeleteMode.Soft || ( mode == SeedDeleteMode.Auto && table.Table.SoftDeleteFlag is |
| | | 48 | | |
| | 207 | 49 | | if ( !soft ) |
| | | 50 | | { |
| | 70 | 51 | | steps.Add( new SeedStep { Kind = SeedStepKind.Delete, Table = table.Table, Keys = Keys( table, keys |
| | | 52 | | |
| | 70 | 53 | | continue; |
| | | 54 | | } |
| | | 55 | | |
| | 137 | 56 | | if ( table.Table.SoftDeleteFlag is null ) { return SeedErrors.SoftDeleteNotSupported( table.EntityType.N |
| | | 57 | | |
| | | 58 | | |
| | 137 | 59 | | var values = writer.SoftDeleteValues( table ); |
| | | 60 | | |
| | 137 | 61 | | if ( values.IsFailure ) { return LayerResponse<IReadOnlyList<SeedStep>>.Failure( values ); } |
| | | 62 | | |
| | 137 | 63 | | steps.Add( new SeedStep { Kind = SeedStepKind.SoftDelete, Table = table.Table, Keys = Keys( table, keys |
| | | 64 | | } |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | |
| | 343 | 68 | | return steps; |
| | 0 | 69 | | } |
| | | 70 | | |
| | | 71 | | |
| | 242 | 72 | | private static SeedTableKeys Keys( SeedBoundTable table, IReadOnlyList<IReadOnlyList<object?>> keys ) => new() { Tab |
| | | 73 | | } |