/// <summary> /// Applies update /// </summary> /// <param name="updateId"></param> public void Apply(Guid updateId) { IUpdate update = GetUpdate(updateId); if (update is null) { throw new Exception($"Update {updateId} is not a valid update"); } if (IsApplied(updateId)) { return; } var missingUpdateIds = GetMissingDependentUpdates(update); if (missingUpdateIds.Count > 0) { throw new Exception($"Update cannot be applied because it depends on update {missingUpdateIds[0]}"); } try { update.Apply(); var updateDetails = new UpdateDetails() { Id = update.Id, Name = update.Name, TimeApplied = DateTime.UtcNow }; _updateDetailsRepository.Add(updateDetails); _updateDetailsRepository.SaveChanges(); } catch (Exception exception) { try { if (update.SupportsRollback) { update.RollBack(); } } catch (Exception rollBackException) { throw new Exception($"Error applying update {update.Name}, roll back failed", rollBackException); } throw new Exception($"Error applying update {update.Name} - {exception.InnerException?.Message}", exception); } }
/// <summary> /// Rolls back the update. /// </summary> /// <param name="updateId"></param> public void RollBack(Guid updateId) { IUpdate update = GetUpdate(updateId); if (update is null) { throw new Exception($"Update {updateId} is not a valid update"); } if (IsApplied(updateId)) { try { update.RollBack(); _updateDetailsRepository.Remove(updateId); _updateDetailsRepository.SaveChanges(); } catch (Exception exception) { throw new Exception($"Error rolling back update {update.Name}", exception); } } }