| | | 1 | | namespace ZhEaIsNsAaBn.DbSeeder; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// The input of a SQL script (design tools): the migrations after <c>from</c> up to and including <c>to</c>, each bound |
| | | 5 | | /// exactly as the runner would (<see cref="SeedStepPlanner"/>). The provider turns them into SQL. |
| | | 6 | | /// </summary> |
| | | 7 | | internal static class SeedScriptBuilder |
| | | 8 | | { |
| | | 9 | | /// <returns>The migrations in id order; otherwise <c>TargetNotFound</c> or a binding / planning error.</returns> |
| | | 10 | | public static LayerResponse<IReadOnlyList<SeedScriptMigration>> Build( IModel model, SeedConfiguration configuration |
| | | 11 | | { |
| | 3 | 12 | | if ( configuration.Migrations is not { IsSuccess: true, Result: { } migrations } ) |
| | | 13 | | { |
| | 0 | 14 | | return LayerResponse<IReadOnlyList<SeedScriptMigration>>.Failure( configuration.Migrations ); |
| | | 15 | | } |
| | | 16 | | |
| | 6 | 17 | | foreach ( var id in new[] { fromMigrationId, toMigrationId }.OfType<string>() ) |
| | | 18 | | { |
| | 0 | 19 | | if ( migrations.All( m => m.Id != id ) ) { return SeedErrors.TargetNotFound( id ); } |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | |
| | 3 | 23 | | var writer = new SeedRowWriter( configuration.Options, SeedEngine.Now( configuration ) ); |
| | 3 | 24 | | var result = new List<SeedScriptMigration>(); |
| | | 25 | | |
| | 18 | 26 | | foreach ( var migration in migrations.Where( m => ( fromMigrationId is null || string.CompareOrdinal( m.Id, from |
| | 3 | 27 | | && ( toMigrationId is null || string.CompareOrdinal( m.Id, toMigr |
| | | 28 | | { |
| | 6 | 29 | | var tables = SeedMigrationBinder.Bind( model, configuration, migration ); |
| | | 30 | | |
| | 6 | 31 | | if ( tables.IsFailure ) { return LayerResponse<IReadOnlyList<SeedScriptMigration>>.Failure( tables ); } |
| | | 32 | | |
| | | 33 | | |
| | 6 | 34 | | var steps = SeedStepPlanner.Plan( tables.Result!, writer, migration.Id ); |
| | | 35 | | |
| | 6 | 36 | | if ( steps.IsFailure ) { return LayerResponse<IReadOnlyList<SeedScriptMigration>>.Failure( steps ); } |
| | | 37 | | |
| | 6 | 38 | | result.Add( new SeedScriptMigration |
| | 6 | 39 | | { |
| | 6 | 40 | | Id = migration.Id, |
| | 6 | 41 | | ContextKey = configuration.ContextKey, |
| | 6 | 42 | | Checksum = migration.Checksum, |
| | 6 | 43 | | ProductVersion = DbSeederDiagnostics.Version, |
| | 6 | 44 | | RequiresSchemaMigration = migration.Manifest.RequiresSchemaMigration, |
| | 6 | 45 | | Steps = steps.Result! |
| | 6 | 46 | | } ); |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | |
| | 3 | 50 | | return result; |
| | 0 | 51 | | } |
| | | 52 | | } |