Esempio n. 1
0
        /// <summary>
        /// Runs the script against the database
        /// </summary>
        /// <param name="databaseAdapter">The database adapter.</param>
        /// <returns></returns>
        private ErrorCode RunImplementation(Strategies.DatabaseAdapter databaseAdapter)
        {
            var errorCode = ErrorCode.Ok;

            if (WrapInTransaction)
            {
                if (!databaseAdapter.BeginTransaction())
                {
                    log.Error("Failed to begin the transaction on the database.");
                    return ErrorCode.DatabaseAdapterFailureAtBeginTransaction;
                }
            }

            if (null != CurrentVersion)
            {
                bool versionConfirmed;
                if (!databaseAdapter.ConfirmVersion(DatabaseName, CurrentVersion, out versionConfirmed))
                {
                    log.Error("Failed to check the current version of the database.");
                    return ErrorCode.DatabaseAdapterFailureAtConfirmVersion;
                }

                if (versionConfirmed)
                {
                    log.Info("The current version of the database is compatible with the script");
                }
                else
                {
                    log.Error("The current version of the database is not compatible with the script.");
                    return ErrorCode.IncorrectCurrentVersion;
                }
            };

            if (!databaseAdapter.RunCommand(DatabaseName, Command))
            {
                log.Error("Failed to execute the command on the database.");
                errorCode = ErrorCode.DatabaseAdapterFailureAtRunCommand;
            }

            if ((errorCode == ErrorCode.Ok) && (NewVersion != null))
            {
                if (!databaseAdapter.SetVersion(DatabaseName, NewVersion))
                {
                    log.Error("Failed to set the new version on the database.");
                    errorCode = ErrorCode.DatabaseAdapterFailureAtSetVersion;
                }
            }

            if ((errorCode == ErrorCode.Ok) && WrapInTransaction)
            {
                if (!databaseAdapter.CommitTransaction())
                {
                    log.Error("Failed to commit the transaction on the database.");
                    errorCode = ErrorCode.DatabaseAdapterFailureAtCommitTransaction;
                }
            }

            if (errorCode != ErrorCode.Ok)
            {
                if (WrapInTransaction)
                {
                    log.Warn("Rolling back the script transaction.");

                    if (!databaseAdapter.RollBackTransaction())
                    {
                        log.Error("An error occurred when rolling back the script transaction. You must check the database is in the correct state.");
                        errorCode = ErrorCode.DatabaseAdapterFailureAtRollbackTransaction;
                    }

                    log.Warn("The script transaction rolled back successfully");
                }
                else
                {
                    log.Warn("The script will not be rolled back. You must check the database is in the correct state.");
                }
            }

            return errorCode;
        }