Exemple #1
0
        /// <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);
            }
        }