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(); } } }
static void Main(string[] args) { Display.InitializeDisplay(); if (LoadConfigurations() == -1) { return; } bool isDefaultDatabaseSpecified = ConfigReader.Config.DatabaseGroups.Count(x => x.Name == "DEFAULT") == 1; if (isDefaultDatabaseSpecified && ChangeReader.AllReleaseChanges.Count > 0) { Display.DisplayMessage(DisplayType.General, "Press ENTER to execute latest changes (up to version \"{0}\") in the latest release script (\"{1}\") to \"DEFAULT\" database group.\nElse enter specific command. To exit type \"Q\". Type \"-help\" for help.", ChangeReader.AllReleaseChanges.FirstOrDefault(x => x.IsLatestRelease).LastChangeVersion, ChangeReader.AllReleaseChanges.FirstOrDefault(x => x.IsLatestRelease).Name); } else { Display.DisplayMessage(DisplayType.General, "Enter command. To exit type \"Q\". Type \"-help\" for help."); } CommandManager commandManager = new CommandManager(); string command = commandManager.GetCommand(); while (command != "Q") { try { if (string.IsNullOrEmpty(command)) { if (isDefaultDatabaseSpecified && ChangeReader.AllReleaseChanges.Count > 0) { ChangeExecutor.ExecuteChanges(ConfigReader.Config.DatabaseGroups.FirstOrDefault(x => x.Name == "DEFAULT")); } else { Display.DisplayMessage(DisplayType.Warning, "Incorrect Command."); } } else if (command.StartsWith("-init")) { string[] options = command.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); if (options.Length != 2) { Display.DisplayMessage(DisplayType.Warning, "Incorrect command. Correct format is -init<space>{database_group}"); } else if (ConfigReader.Config.DatabaseGroups.Count(x => x.Name == options[1]) == 0) { Display.DisplayMessage(DisplayType.Warning, "Specified database group does not exist in the config file."); } else { Initializer.Initialize(ConfigReader.Config.DatabaseGroups.Where(x => x.Name == options[1]).FirstOrDefault()); } } else if (command.StartsWith("-generatescript")) { string[] options = command.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); if (options.Length != 2) { Display.DisplayMessage(DisplayType.Warning, "Incorrect command. Correct format is -generatescript<space>{database_group}"); } else if (ConfigReader.Config.DatabaseGroups.Count(x => x.Name == options[1]) == 0) { Display.DisplayMessage(DisplayType.Warning, "Specified database group does not exist in the config file."); } else { DatabaseScriptGenerator.ScriptGenerator generator = new DatabaseScriptGenerator.ScriptGenerator(ConfigReader.Config.DatabaseGroups.FirstOrDefault(x => x.Name == options[1]).Databases[0].ConnectionString, Constants.CHANGE_SCRIPT_DIRECTORY); generator.GenerateScriptAndWriteXML(); } } else if (command.StartsWith("-status")) { string[] options = command.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); if (options.Length != 2) { Display.DisplayMessage(DisplayType.Warning, "Incorrect command. Correct format is -status<space>{database_group}"); } else if (ConfigReader.Config.DatabaseGroups.Count(x => x.Name == options[1]) == 0) { Display.DisplayMessage(DisplayType.Warning, "Specified database group does not exist in the config file."); } else { ChangeExecutor.ShowLastExecutedChanges(ConfigReader.Config.DatabaseGroups.Where(x => x.Name == options[1]).FirstOrDefault()); } } else if (command.StartsWith("-log")) { string[] options = command.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); int changeVersion = -1; if (options.Length != 4 || !Int32.TryParse(options[3], out changeVersion)) { Display.DisplayMessage(DisplayType.Warning, "Incorrect command. Correct format is -log<space>{database_group}<space>{release_version}<space>{change_version}"); } else if (ConfigReader.Config.DatabaseGroups.Count(x => x.Name == options[1]) == 0) { Display.DisplayMessage(DisplayType.Warning, "Specified database group does not exist in the config file."); } else { Initializer.LogChanges(ConfigReader.Config.DatabaseGroups.Where(x => x.Name == options[1]).FirstOrDefault(), options[2], changeVersion); } } else if (command.StartsWith("-clear")) { Console.Clear(); } else if (command.StartsWith("-reload")) { if (LoadConfigurations() == -1) { return; } isDefaultDatabaseSpecified = ConfigReader.Config.DatabaseGroups.Count(x => x.Name == "DEFAULT") == 1; commandManager = new CommandManager(); Display.DisplayMessage(DisplayType.Info, "All Configurations have been reloaded."); } else if (command.StartsWith("-help")) { Display.DisplayMessage(DisplayType.Info, @" >> {database_group}<space>{release_version}<space>{change_version} Executes the changes up to {change_version} in {release_version} in the databases in {database_group}. >> {database_group}<space>{release_version} Executes all the latest changes in {release_version} in the databases in {database_group}. >> {database_group} Executes all the latest changes in the latest release version in the databases in {database_group}. >> ENTER (Key press) Executes all the latest changes in the latest release version in the databases in ""DEFAULT"" database_group. >> {database_group}<space>{release_version}<space>{change_version}<space>-force Forcibly executes (even if version chain is not maintained) the changes in {change_version} in the {release_version} in the databases in {database_group} without inserting the log. >> -status<space>{database_group} Shows the current status of the databases in the specified {database_group}. >> -init<space>{database_group} Creates the log table in the databases in {database_group}. >> -generatescript<space>{database_group} Creates the exisitng table/SP/function scripts from the first database in the specified {database_group}. Should be used if the tool is intended to use from an existing database and to automatically generate the scripts for the first time. >> -log<space>{database_group}<space>{release_version}<space>{change_version} Inserts all the logs up to given {change_version} and {release_version} in the databases in {database_group}. It's helpful to insert logs in the existing databases for the first time without actually executing the changes. >> -reload Reloads all the configurations (Config XML and Change XMLs). >> -clear Clears the screen "); } else { string[] parameters = command.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); DatabaseGroup databaseGroup = ConfigReader.Config.DatabaseGroups.FirstOrDefault(x => x.Name == parameters[0]); int changeVersion = -1; if (databaseGroup == null) { Display.DisplayMessage(DisplayType.Warning, "Database Group - {0} does not exist.", parameters[0]); } else if (parameters.Length == 1) { ChangeExecutor.ExecuteChanges(databaseGroup); } else if (parameters.Length == 2) { ChangeExecutor.ExecuteChanges(databaseGroup, parameters[1]); } else if ((parameters.Length == 3 || (parameters.Length == 4 && parameters[3] == "-force")) && Int32.TryParse(parameters[2], out changeVersion)) { ChangeExecutor.ExecuteChanges(databaseGroup, parameters[1], changeVersion, (parameters.Length == 4 && parameters[3] == "-force")); } else { Display.DisplayMessage(DisplayType.Warning, "Incorrect Command."); } } } catch (Exception ex) { if (ex is VersioningException) { Display.DisplayMessage(DisplayType.Warning, ex.Message); } else { Display.DisplayMessage(DisplayType.Error, "The command was aborted due to exception - {0}", ex); } } Display.DisplayMessage(DisplayType.General, "\n\nEnter command. To exit type \"Q\". Type \"-help\" for help."); command = commandManager.GetCommand(); } }