private static void ApplyMigration( IRepositoryToMigrate repositoryToMigrate, IRepository repository, RepositoryMigration startedMigration) { try { MigrationHelpers.StartMigration( repository.AddMigration, startedMigration); startedMigration.Migration.Update(repository, repositoryToMigrate); MigrationHelpers.CompleteMigration( repository.UpsertMigration, startedMigration); } catch (Exception exception) { OnUpgradeException( repositoryToMigrate, repository, startedMigration, exception); } }
/// <summary> /// Use the migration locator to derive what migrations should be executed. /// Compare the migrations to be executed against the migrations applied to the repository. /// Synchronously apply the migrations that were not applied to the repository to migrate in ascending order. /// </summary> /// <param name="migrationLocator">The migration locator</param> /// <param name="repository">The migration repository</param> /// <param name="repositoryToMigrate">The repository to apply the migrations to.</param> public static void UpdateToLatest( IMigrationLocator migrationLocator, IRepository repository, IRepositoryToMigrate repositoryToMigrate) { MigrationSession migrationSession; if (MigrationSessionHelpers.TryStartMigrationSession( repository.AddMigrationSession, MigrationSessionHelpers.MigrationSessionIsAvailableToExecute(repository.GetMigrationSessions()), MigrationHelpers.GetMigrationsToBeAppliedAscending( MigrationHelpers.GetMaxVersionOrDefault(repository.GetMigrations()), migrationLocator.GetAllMigrations()), out migrationSession)) { try { UpdateTo( repositoryToMigrate, repository, migrationSession); MigrationSessionHelpers.CompleteMigrationSession( repository.UpsertMigrationSession, migrationSession); } catch (MigrationException migrationException) { MigrationSessionHelpers.FailMigrationSession( repository.UpsertMigrationSession, migrationSession, migrationException); throw migrationException; } } }
private static async Task ApplyMigrationAsync( IRepositoryToMigrate repositoryToMigrate, IRepositoryAsync repository, RepositoryMigrationAsync startedMigration) { try { await MigrationHelpersAsync.StartMigrationAsync( repository.AddMigrationAsync, startedMigration).ConfigureAwait(false); await startedMigration.Migration.UpdateAsync( repository, repositoryToMigrate).ConfigureAwait(false); await MigrationHelpersAsync.CompleteMigrationAsync( repository.UpsertMigrationAsync, startedMigration).ConfigureAwait(false); } catch (Exception exception) { await OnUpgradeExceptionAsync( repositoryToMigrate, repository, startedMigration, exception).ConfigureAwait(false); } }
private static async Task OnUpgradeExceptionAsync( IRepositoryToMigrate repositoryToMigrate, IRepositoryAsync repository, RepositoryMigrationAsync repositoryMigration, Exception exception) { try { await repositoryMigration.Migration.RollbackAsync( repository, repositoryToMigrate).ConfigureAwait(false); await MigrationHelpersAsync.FailMigrationAsync( repository.DeleteMigrationAsync, repositoryMigration).ConfigureAwait(false); throw new MigrationException( exception, repositoryMigration.Version); } catch (Exception rollbackEx) { await OnRollbackExceptionAsync( repositoryMigration.Migration, rollbackEx).ConfigureAwait(false); } }
private static void UpdateTo( IRepositoryToMigrate repositoryToMigrate, IRepository repository, MigrationSession migrationSession) { ApplyMigrations( repositoryToMigrate, repository, migrationSession); }
private static Task UpdateToAsync( IRepositoryToMigrate repositoryToMigrate, IRepositoryAsync repository, MigrationSessionAsync migrationSession) { return(ApplyMigrationsAsync( repositoryToMigrate, repository, migrationSession)); }
private static void ApplyMigrations( IRepositoryToMigrate repositoryToMigrate, IRepository repository, MigrationSession migrationSession) { foreach (Migration migration in migrationSession.MigrationsToBeApplied) { ApplyMigration( repositoryToMigrate, repository, new RepositoryMigration(migration)); } }
private static async Task ApplyMigrationsAsync( IRepositoryToMigrate repositoryToMigrate, IRepositoryAsync repository, MigrationSessionAsync migrationSession) { foreach (MigrationAsync migration in migrationSession.MigrationsToBeApplied) { await ApplyMigrationAsync( repositoryToMigrate, repository, new RepositoryMigrationAsync(migration)).ConfigureAwait(false); } }
private static void OnUpgradeException( IRepositoryToMigrate repositoryToMigrate, IRepository repository, RepositoryMigration repositoryMigration, Exception exception) { try { repositoryMigration.Migration.Rollback(repository, repositoryToMigrate); MigrationHelpers.FailMigration( repository.DeleteMigration, repositoryMigration); throw new MigrationException( exception, repositoryMigration.Version); } catch (Exception rollbackEx) { OnRollbackException( repositoryMigration.Migration, rollbackEx); } }
/// <summary> /// Use the migration locator to derive what migrations should be executed. /// Compare the migrations to be executed against the migrations applied to the repository. /// Synchronously apply the migrations that were not applied to the repository to migrate in ascending order. /// </summary> /// <param name="migrationLocator">The migration locator</param> /// <param name="repository">The migration repository</param> /// <param name="repositoryToMigrate">The repository to apply the migrations to.</param> public static async Task UpdateToLatestAsync( IMigrationLocatorAsync migrationLocator, IRepositoryAsync repository, IRepositoryToMigrate repositoryToMigrate) { MigrationSessionAsync migrationSession = await MigrationSessionHelpersAsync.TryStartMigrationSession( repository.AddMigrationSessionAsync, MigrationSessionHelpersAsync.MigrationSessionIsAvailableToExecuteAsync(await repository.GetMigrationSessionsAsync().ConfigureAwait(false)), MigrationHelpersAsync.GetMigrationsToBeAppliedAscending( MigrationHelpersAsync.GetMaxVersionOrDefault(await repository.GetMigrationsAsync().ConfigureAwait(false)), await migrationLocator.GetAllMigrationsAsync().ConfigureAwait(false))).ConfigureAwait(false); if (migrationSession != null) { try { await UpdateToAsync( repositoryToMigrate, repository, migrationSession).ConfigureAwait(false); await MigrationSessionHelpersAsync.CompleteMigrationSessionAsync( repository.UpsertMigrationSessionAsync, migrationSession).ConfigureAwait(false); } catch (MigrationException migrationException) { await MigrationSessionHelpersAsync.FailMigrationSessionAsync( repository.UpsertMigrationSessionAsync, migrationSession, migrationException).ConfigureAwait(false); throw migrationException; } } }
public abstract void Rollback(IRepository repository, IRepositoryToMigrate repositoryToMigrate);
public abstract void Update(IRepository repository, IRepositoryToMigrate repositoryToMigrate);
public abstract Task RollbackAsync(IRepositoryAsync repository, IRepositoryToMigrate repositoryToMigrate);
public abstract Task UpdateAsync(IRepositoryAsync repository, IRepositoryToMigrate repositoryToMigrate);