From 3ed34c78c128f3cdd3440afa35182b7b35b06891 Mon Sep 17 00:00:00 2001 From: Luke McGregor Date: Wed, 28 Oct 2015 10:28:57 +1300 Subject: [PATCH] added switch warnandignoreononetimescriptchanges fixes #226 skips over one and updates baseline for onetime scripts which have changed. --- product/roundhouse.console/Program.cs | 6 ++- product/roundhouse.tasks/Roundhouse.cs | 7 ++- .../consoles/DefaultConfiguration.cs | 5 +- .../ConfigurationPropertyHolder.cs | 3 +- .../migrators/DefaultDatabaseMigrator.cs | 51 +++++++++++-------- 5 files changed, 42 insertions(+), 30 deletions(-) diff --git a/product/roundhouse.console/Program.cs b/product/roundhouse.console/Program.cs index 96a5da02..15a7a4c3 100644 --- a/product/roundhouse.console/Program.cs +++ b/product/roundhouse.console/Program.cs @@ -283,6 +283,10 @@ private static void parse_arguments_and_set_up_configuration(ConfigurationProper .Add("w|warnononetimescriptchanges", "WarnOnOneTimeScriptChanges - Instructs RH to execute changed one time scripts (DDL/DML in Up folder) that have previously been run against the database instead of failing. A warning is logged for each one time scripts that is rerun. Defaults to false.", option => configuration.WarnOnOneTimeScriptChanges = option != null) + //warn on changes + .Add("warnandignoreononetimescriptchanges", + "WarnAndIgnoreOnOneTimeScriptChanges - Instructs RH to ignore and update the hash of changed one time scripts (DDL/DML in Up folder) that have previously been run against the database instead of failing. A warning is logged for each one time scripts that is rerun. Defaults to false.", + option => configuration.WarnAndIgnoreOnOneTimeScriptChanges = option != null) //silent? .Add("silent|ni|noninteractive", "Silent - tells RH not to ask for any input when it runs. Defaults to false.", @@ -395,7 +399,7 @@ public static void show_help(string message, OptionSet option_set) public static void init_folder(ConfigurationPropertyHolder configuration) { the_logger.Info("Initializing folder for roundhouse"); - Container.get_an_instance_of().Initialize(configuration,"."); + Container.get_an_instance_of().Initialize(configuration, "."); Environment.Exit(0); } diff --git a/product/roundhouse.tasks/Roundhouse.cs b/product/roundhouse.tasks/Roundhouse.cs index c7ca90f0..117c944f 100644 --- a/product/roundhouse.tasks/Roundhouse.cs +++ b/product/roundhouse.tasks/Roundhouse.cs @@ -7,12 +7,9 @@ using infrastructure.app.logging; using infrastructure.containers; using infrastructure.filesystem; - using Microsoft.Build.Framework; - using migrators; - using Microsoft.Build.Utilities; - + using migrators; using resolvers; using runners; using Environment = environments.Environment; @@ -128,6 +125,8 @@ bool ITask.Execute() public bool WarnOnOneTimeScriptChanges { get; set; } + public bool WarnAndIgnoreOnOneTimeScriptChanges { get; set; } + public bool Silent { get; set; } public string DatabaseType { get; set; } diff --git a/product/roundhouse/consoles/DefaultConfiguration.cs b/product/roundhouse/consoles/DefaultConfiguration.cs index c14b2783..23387d9b 100644 --- a/product/roundhouse/consoles/DefaultConfiguration.cs +++ b/product/roundhouse/consoles/DefaultConfiguration.cs @@ -16,12 +16,12 @@ public sealed class DefaultConfiguration : ConfigurationPropertyHolder public int CommandTimeoutAdmin { get; set; } public string SqlFilesDirectory { get; set; } public string RepositoryPath { get; set; } - public string Version { get; set; } + public string Version { get; set; } public string VersionFile { get; set; } public string VersionXPath { get; set; } public string AlterDatabaseFolderName { get; set; } public string RunAfterCreateDatabaseFolderName { get; set; } - public string RunBeforeUpFolderName { get; set; } + public string RunBeforeUpFolderName { get; set; } public string UpFolderName { get; set; } public string DownFolderName { get; set; } public string RunFirstAfterUpFolderName { get; set; } @@ -45,6 +45,7 @@ public sealed class DefaultConfiguration : ConfigurationPropertyHolder public string CreateDatabaseCustomScript { get; set; } public string OutputPath { get; set; } public bool WarnOnOneTimeScriptChanges { get; set; } + public bool WarnAndIgnoreOnOneTimeScriptChanges { get; set; } public bool Silent { get; set; } public string DatabaseType { get; set; } public bool Drop { get; set; } diff --git a/product/roundhouse/infrastructure.app/ConfigurationPropertyHolder.cs b/product/roundhouse/infrastructure.app/ConfigurationPropertyHolder.cs index 76f2ac99..da8001c7 100644 --- a/product/roundhouse/infrastructure.app/ConfigurationPropertyHolder.cs +++ b/product/roundhouse/infrastructure.app/ConfigurationPropertyHolder.cs @@ -21,7 +21,7 @@ public interface ConfigurationPropertyHolder string VersionXPath { get; set; } string AlterDatabaseFolderName { get; set; } string RunAfterCreateDatabaseFolderName { get; set; } - string RunBeforeUpFolderName { get; set; } + string RunBeforeUpFolderName { get; set; } string UpFolderName { get; set; } string DownFolderName { get; set; } string RunFirstAfterUpFolderName { get; set; } @@ -45,6 +45,7 @@ public interface ConfigurationPropertyHolder string CreateDatabaseCustomScript { get; set; } string OutputPath { get; set; } bool WarnOnOneTimeScriptChanges { get; set; } + bool WarnAndIgnoreOnOneTimeScriptChanges { get; set; } bool Silent { get; set; } string DatabaseType { get; set; } bool Drop { get; set; } diff --git a/product/roundhouse/migrators/DefaultDatabaseMigrator.cs b/product/roundhouse/migrators/DefaultDatabaseMigrator.cs index 4a292cdc..e6d12297 100644 --- a/product/roundhouse/migrators/DefaultDatabaseMigrator.cs +++ b/product/roundhouse/migrators/DefaultDatabaseMigrator.cs @@ -22,6 +22,7 @@ public sealed class DefaultDatabaseMigrator : DatabaseMigrator private readonly string custom_restore_options; private readonly string output_path; private readonly bool error_on_one_time_script_changes; + private readonly bool ignore_one_time_script_changes; private bool running_in_a_transaction; private readonly bool is_running_all_any_time_scripts; private readonly bool is_baseline; @@ -36,7 +37,8 @@ public DefaultDatabaseMigrator(Database database, CryptographicService crypto_pr restore_path = configuration.RestoreFromPath; custom_restore_options = configuration.RestoreCustomOptions; output_path = configuration.OutputPath; - error_on_one_time_script_changes = !configuration.WarnOnOneTimeScriptChanges; + error_on_one_time_script_changes = !configuration.WarnOnOneTimeScriptChanges && !configuration.WarnAndIgnoreOnOneTimeScriptChanges; + ignore_one_time_script_changes = configuration.WarnAndIgnoreOnOneTimeScriptChanges; is_running_all_any_time_scripts = configuration.RunAllAnyTimeScripts; is_baseline = configuration.Baseline; is_dryrun = configuration.DryRun; @@ -111,7 +113,7 @@ public void restore_database(string restore_from_path, string restore_options) public void set_recovery_mode(bool simple) { //database.open_connection(false); - Log.bound_to(this).log_an_info_event_containing("Setting recovery mode to '{0}' for database {1}.", simple ? "Simple":"Full", database.database_name ); + Log.bound_to(this).log_an_info_event_containing("Setting recovery mode to '{0}' for database {1}.", simple ? "Simple" : "Full", database.database_name); database.set_recovery_mode(simple); //database.close_connection(); } @@ -166,6 +168,7 @@ public long version_the_database(string repository_path, string repository_versi public bool run_sql(string sql_to_run, string script_name, bool run_this_script_once, bool run_this_script_every_time, long version_id, Environment environment, string repository_version, string repository_path, ConnectionType connection_type) { bool this_sql_ran = false; + bool skip_run = is_baseline; if (this_is_a_one_time_script_that_has_changes_but_has_already_been_run(script_name, sql_to_run, run_this_script_once)) { @@ -177,6 +180,10 @@ public bool run_sql(string sql_to_run, string script_name, bool run_this_script_ database.close_connection(); throw new Exception(error_message); } + if (ignore_one_time_script_changes) + { + skip_run = true; + } Log.bound_to(this).log_a_warning_event_containing("{0} is a one time script that has changed since it was run.", script_name); } @@ -186,25 +193,25 @@ public bool run_sql(string sql_to_run, string script_name, bool run_this_script_ if (!is_dryrun) { Log.bound_to(this).log_an_info_event_containing(" {3} {0} on {1} - {2}.", script_name, database.server_name, database.database_name, - is_baseline ? "BASELINING: Recording" : "Running"); + skip_run ? "BASELINING: Recording" : "Running"); } - if (!is_baseline) + if (!skip_run) { if (!is_dryrun) { - foreach (var sql_statement in get_statements_to_run(sql_to_run)) - { - try - { - database.run_sql(sql_statement, connection_type); - } - catch (Exception ex) - { - database.rollback(); - - record_script_in_scripts_run_errors_table(script_name, sql_to_run, sql_statement, ex.Message, repository_version, repository_path); - database.close_connection(); - throw; + foreach (var sql_statement in get_statements_to_run(sql_to_run)) + { + try + { + database.run_sql(sql_statement, connection_type); + } + catch (Exception ex) + { + database.rollback(); + + record_script_in_scripts_run_errors_table(script_name, sql_to_run, sql_statement, ex.Message, repository_version, repository_path); + database.close_connection(); + throw; } } } @@ -213,11 +220,11 @@ public bool run_sql(string sql_to_run, string script_name, bool run_this_script_ Log.bound_to(this).log_a_warning_event_containing(" DryRun: {0} on {1} - {2}.", script_name, database.server_name, database.database_name); } } - if (!is_dryrun) + if (!is_dryrun) { - record_script_in_scripts_run_table(script_name, sql_to_run, run_this_script_once, version_id); - this_sql_ran = true; - } + record_script_in_scripts_run_table(script_name, sql_to_run, run_this_script_once, version_id); + this_sql_ran = true; + } } else { @@ -366,7 +373,7 @@ private bool this_script_should_run(string script_name, string sql_to_run, bool { return false; } - + return true; }