| | | 1 | | namespace ZhEaIsNsAaBn.DbSeeder; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// The standalone runner: takes the database seed lock, then applies. Works on a context from <see cref="IDbContextFact |
| | | 5 | | /// (owned and disposed here) when one is registered, otherwise on the scoped context. |
| | | 6 | | /// </summary> |
| | | 7 | | internal sealed class SeedMigrator< TDbContext > : ISeedMigrator<TDbContext>, IDisposable, IAsyncDisposable |
| | | 8 | | where TDbContext : DbContext |
| | | 9 | | { |
| | | 10 | | private readonly SeedConfiguration _configuration; |
| | | 11 | | private readonly ISeedDatabaseProvider _provider; |
| | | 12 | | private readonly Lazy<(TDbContext Context, bool Owned)> _context; |
| | | 13 | | |
| | | 14 | | |
| | 174 | 15 | | public SeedMigrator( SeedConfiguration configuration, ISeedDatabaseProvider provider, Func<(TDbContext Context, bool |
| | | 16 | | { |
| | 174 | 17 | | _configuration = configuration; |
| | 174 | 18 | | _provider = provider; |
| | 174 | 19 | | _context = new Lazy<(TDbContext, bool)>( contextFactory ); |
| | 174 | 20 | | } |
| | | 21 | | |
| | | 22 | | |
| | | 23 | | public Task<LayerResponse<SeedRunResult>> MigrateAsync( CancellationToken cancellationToken = default ) |
| | 68 | 24 | | => SeedEngine.UpAsync( _context.Value.Context, _configuration, _provider, null, takeLock: true, async: true, can |
| | | 25 | | |
| | | 26 | | public LayerResponse<SeedRunResult> Migrate() |
| | 13 | 27 | | => SyncPath.Result( SeedEngine.UpAsync( _context.Value.Context, _configuration, _provider, null, takeLock: true, |
| | | 28 | | |
| | | 29 | | public Task<LayerResponse<SeedRunResult>> MigrateToAsync( string targetMigrationId, CancellationToken cancellationTo |
| | | 30 | | { |
| | 60 | 31 | | ArgumentException.ThrowIfNullOrWhiteSpace( targetMigrationId ); |
| | | 32 | | |
| | 60 | 33 | | return SeedEngine.UpAsync( _context.Value.Context, _configuration, _provider, targetMigrationId, takeLock: true, |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | public LayerResponse<SeedRunResult> MigrateTo( string targetMigrationId ) |
| | | 37 | | { |
| | 3 | 38 | | ArgumentException.ThrowIfNullOrWhiteSpace( targetMigrationId ); |
| | | 39 | | |
| | 0 | 40 | | return SyncPath.Result( SeedEngine.UpAsync( _context.Value.Context, _configuration, _provider, targetMigrationId |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | public Task<LayerResponse<SeedRunResult>> MigrateDownAsync( int steps, SeedDeleteMode? deleteMode = null, Cancellati |
| | | 44 | | { |
| | 25 | 45 | | ArgumentOutOfRangeException.ThrowIfLessThan( steps, 1 ); |
| | | 46 | | |
| | 22 | 47 | | return SeedRollback.DownAsync( _context.Value.Context, _configuration, _provider, steps, null, deleteMode, async |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | public LayerResponse<SeedRunResult> MigrateDown( int steps, SeedDeleteMode? deleteMode = null ) |
| | | 51 | | { |
| | 4 | 52 | | ArgumentOutOfRangeException.ThrowIfLessThan( steps, 1 ); |
| | | 53 | | |
| | 4 | 54 | | return SyncPath.Result( SeedRollback.DownAsync( _context.Value.Context, _configuration, _provider, steps, null, |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | public Task<LayerResponse<SeedRunResult>> MigrateDownToAsync( string targetMigrationId, SeedDeleteMode? deleteMode = |
| | | 58 | | { |
| | 8 | 59 | | ArgumentException.ThrowIfNullOrWhiteSpace( targetMigrationId ); |
| | | 60 | | |
| | 8 | 61 | | return SeedRollback.DownAsync( _context.Value.Context, _configuration, _provider, null, targetMigrationId, delet |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | public LayerResponse<SeedRunResult> MigrateDownTo( string targetMigrationId, SeedDeleteMode? deleteMode = null ) |
| | | 65 | | { |
| | 3 | 66 | | ArgumentException.ThrowIfNullOrWhiteSpace( targetMigrationId ); |
| | | 67 | | |
| | 0 | 68 | | return SyncPath.Result( SeedRollback.DownAsync( _context.Value.Context, _configuration, _provider, null, targetM |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | public Task<LayerResponse<SeedRollbackPlan>> PlanRollbackAsync( int steps, SeedDeleteMode? deleteMode = null, Cancel |
| | | 72 | | { |
| | 4 | 73 | | ArgumentOutOfRangeException.ThrowIfLessThan( steps, 1 ); |
| | | 74 | | |
| | 4 | 75 | | return SeedRollback.PlanAsync( _context.Value.Context, _configuration, _provider, steps, null, deleteMode, async |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | public LayerResponse<SeedRollbackPlan> PlanRollback( int steps, SeedDeleteMode? deleteMode = null ) |
| | | 79 | | { |
| | 7 | 80 | | ArgumentOutOfRangeException.ThrowIfLessThan( steps, 1 ); |
| | | 81 | | |
| | 4 | 82 | | return SyncPath.Result( SeedRollback.PlanAsync( _context.Value.Context, _configuration, _provider, steps, null, |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | public Task<LayerResponse<IReadOnlyList<SeedMigrationInfo>>> GetMigrationsAsync( CancellationToken cancellationToken |
| | 3 | 86 | | => SeedEngine.ListAsync( _context.Value.Context, _configuration, _provider, async: true, cancellationToken ); |
| | | 87 | | |
| | | 88 | | public LayerResponse<IReadOnlyList<SeedMigrationInfo>> GetMigrations() |
| | 3 | 89 | | => SyncPath.Result( SeedEngine.ListAsync( _context.Value.Context, _configuration, _provider, async: false, Cance |
| | | 90 | | |
| | | 91 | | public void Dispose() |
| | | 92 | | { |
| | 6 | 93 | | if ( _context is { IsValueCreated: true, Value.Owned: true } ) { _context.Value.Context.Dispose(); } |
| | 6 | 94 | | } |
| | | 95 | | |
| | | 96 | | public async ValueTask DisposeAsync() |
| | | 97 | | { |
| | 36 | 98 | | if ( _context is { IsValueCreated: true, Value.Owned: true } ) { await _context.Value.Context.DisposeAsync().Con |
| | 36 | 99 | | } |
| | | 100 | | } |