Пример #1
0
        public static void LogChanges(DatabaseGroup databaseGroup, string toReleaseVersion, int toChangeVersion)
        {
            ReleaseChanges toReleaseChanges = ChangeExecutor.AsserValidReleaseVersion(toReleaseVersion);

            if (!toReleaseChanges.Changes.Select(x => x.Version).Contains(toChangeVersion))
            {
                throw new VersioningException("Change Version - " + toChangeVersion + " does not exist in Release version - " + toReleaseVersion + " in the change xml files.");
            }

            foreach (Database database in databaseGroup.Databases)
            {
                DatabaseVersion lastExecutedVersion = ChangeExecutor.GetLastExecutedVersion(database);
                if (lastExecutedVersion.ReleaseVersion != DatabaseVersion.NO_EXECUTED_RELEASE_VERSION)
                {
                    Display.DisplayMessage(DisplayType.Error, "Can not insert logs in database - {0} since some logs were already inserted in this database by the tool.", database.Name);
                    continue;
                }

                SqlDatabaseManager databaseManager = new SqlDatabaseManager(database, true);
                SqlTransaction     tx = databaseManager.Connection.BeginTransaction();

                try
                {
                    for (int sequence = 1; sequence <= toReleaseChanges.Sequence; sequence++)
                    {
                        ReleaseChanges releaseChanges = ChangeReader.AllReleaseChanges.FirstOrDefault(x => x.Sequence == sequence);

                        int toChangeVersionToExecute = releaseChanges.LastChangeVersion;

                        if (releaseChanges.Sequence == toReleaseChanges.Sequence)
                        {
                            toChangeVersionToExecute = toChangeVersion;
                        }

                        for (int version = 1; version <= toChangeVersionToExecute; version++)
                        {
                            databaseManager.ExecuteNonQuery(@"
			                INSERT INTO "             + Constants.CHANGE_LOG_TABLE + @" (RELEASE_VERSION, CHANGE_VERSION, EXECUTION_TIME, EXECUTOR_NAME, EXECUTOR_IP, DESCRIPTION)
			                VALUES ('"             + releaseChanges.Name + "'," + version + " ,CURRENT_TIMESTAMP, SUSER_NAME(), CAST(CONNECTIONPROPERTY('client_net_address') AS VARCHAR(255)), '" + (releaseChanges.Changes.FirstOrDefault(x => x.Version == version).Description ?? "") + "');", tx);
                        }
                    }

                    tx.Commit();

                    Display.DisplayMessage(DisplayType.Success, "Successfully inserted logs in database - {0}.", database.Name);
                }
                catch (Exception ex)
                {
                    Display.DisplayMessage(DisplayType.Error, "Exception occured while inserting logs in database - {0}. Exception - {1}", database.Name, ex);
                    tx.Rollback();
                }
                finally
                {
                    databaseManager.CloseConnection();
                }
            }
        }
Пример #2
0
        public static void Initialize(DatabaseGroup databaseGroup)
        {
            foreach (Database database in databaseGroup.Databases)
            {
                try
                {
                    SqlDatabaseManager databaseManager = new SqlDatabaseManager(database, true);
                    databaseManager.ExecuteNonQuery(GetVersioningTableScript(), transaction: null);
                    databaseManager.CloseConnection();

                    Display.DisplayMessage(DisplayType.Success, "Log table created in database - {0}.", database.Name);
                }
                catch (Exception ex)
                {
                    Display.DisplayMessage(DisplayType.Error, "Could not create change log table in database - {0}. See logs for details.", database.Name);
                    Logger.Log(ex);
                }
            }
        }
Пример #3
0
        public static void ExecuteChanges(DatabaseGroup databaseGroup, string releaseVersion, int changeVersion, bool force)
        {
            ReleaseChanges releaseChanges = AsserValidReleaseVersion(releaseVersion);

            if (ChangeReader.AllReleaseChanges.First(x => x.Name == releaseVersion).Changes.Count(y => y.Version == changeVersion) == 0)
            {
                throw new VersioningException("Change version - \"" + changeVersion + "\" does not exist in the change xml file for Release version - \"" + releaseVersion + "\" .");
            }

            foreach (Database database in databaseGroup.Databases)
            {
                int lastExecutedCurrentChangeVersion = GetLastExecutedChangeVersion(database, releaseVersion);

                if (force)
                {
                    lastExecutedCurrentChangeVersion = changeVersion - 1;
                }
                else if (!IsVersionChainMaintained(database, releaseChanges, changeVersion, lastExecutedCurrentChangeVersion))
                {
                    continue;
                }

                SqlDatabaseManager databaseManager = new SqlDatabaseManager(database, true);
                SqlTransaction     tx = databaseManager.Connection.BeginTransaction();

                Change executingChange = null;
                string executingSql    = null;

                try
                {
                    List <int> executedVersions = new List <int>();

                    for (int version = lastExecutedCurrentChangeVersion + 1; version <= changeVersion; version++)
                    {
                        executingChange = releaseChanges.Changes.FirstOrDefault(x => x.Version == version);

                        foreach (var sql in executingChange.ChangeSqls)
                        {
                            executingSql = string.Empty;

                            if (!string.IsNullOrEmpty(sql.Path))
                            {
                                executingSql = System.IO.File.ReadAllText(System.IO.Path.Combine(Constants.CHANGE_SCRIPT_DIRECTORY, sql.Path));
                            }
                            else
                            {
                                executingSql = sql.Sql;
                            }

                            if (database.Replacements != null)
                            {
                                foreach (var replacement in database.Replacements)
                                {
                                    executingSql = executingSql.Replace(replacement.Text, replacement.ReplacementText);
                                }
                            }

                            databaseManager.ExecuteNonQuery(executingSql, tx);
                        }

                        if (!force)
                        {
                            databaseManager.ExecuteNonQuery(@"
			                INSERT INTO "             + Constants.CHANGE_LOG_TABLE + @" (RELEASE_VERSION, CHANGE_VERSION, EXECUTION_TIME, EXECUTOR_NAME, EXECUTOR_IP, DESCRIPTION)
			                VALUES ('"             + releaseVersion + "'," + version + " ,CURRENT_TIMESTAMP, SUSER_NAME(), CAST(CONNECTIONPROPERTY('client_net_address') AS VARCHAR(255)), '" + (releaseChanges.Changes.FirstOrDefault(x => x.Version == version).Description ?? "") + "');", tx);
                        }

                        executedVersions.Add(version);
                    }

                    tx.Commit();

                    Display.DisplayMessage(DisplayType.Success, "Successfulle executed change versions {0} in release {1} in database {2}", string.Join(", ", executedVersions), releaseVersion, database.Name);
                }
                catch (Exception ex)
                {
                    string additionaMessage = string.Empty;
                    string executingSqlText = string.Empty;

                    if (executingChange != null)
                    {
                        additionaMessage += "\nException in Change \"" + executingChange.Version + "\" (" + (executingChange.Description ?? "") + ").\n";
                    }

                    if (!string.IsNullOrEmpty(executingSql))
                    {
                        executingSqlText = "\nException while executing SQL:\n" + executingSql;
                    }

                    Display.DisplayMessage(DisplayType.Error, "Exception occured while executing changes in release \"{0}\" in database \"{1}\".{2}\nException {3}{4}", releaseVersion, database.Name, additionaMessage, ex, executingSqlText);
                    tx.Rollback();
                }
                finally
                {
                    databaseManager.CloseConnection();
                }
            }
        }