From b00dcf213548942e1056ddb192f9b5b7c02b1408 Mon Sep 17 00:00:00 2001 From: Marko Lahma Date: Tue, 8 Jan 2013 09:55:10 +0200 Subject: [PATCH] Ignore EOL changes between Windows and Unix formats when checking whether script hashes match --- .../migrators/DefaultDatabaseMigrator.cs | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/product/roundhouse/migrators/DefaultDatabaseMigrator.cs b/product/roundhouse/migrators/DefaultDatabaseMigrator.cs index d2bc067f..5ce06e53 100644 --- a/product/roundhouse/migrators/DefaultDatabaseMigrator.cs +++ b/product/roundhouse/migrators/DefaultDatabaseMigrator.cs @@ -278,8 +278,6 @@ private bool this_is_a_one_time_script_that_has_changes_but_has_already_been_run private bool this_script_has_changed_since_last_run(string script_name, string sql_to_run) { - bool hash_is_same = false; - string old_text_hash = string.Empty; try { @@ -294,14 +292,44 @@ private bool this_script_has_changed_since_last_run(string script_name, string s string new_text_hash = create_hash(sql_to_run); - if (string.Compare(old_text_hash, new_text_hash, true) == 0) + bool hash_is_same = hashes_are_equal(new_text_hash, old_text_hash); + + if (!hash_is_same) { - hash_is_same = true; + // extra checks if only line endings have changed + hash_is_same = have_same_hash_ignoring_platform(sql_to_run, old_text_hash); + if (hash_is_same) + { + Log.bound_to(this).log_a_warning_event_containing("Script {0} had different line endings than before but equal content", script_name); + } } return !hash_is_same; } + private bool hashes_are_equal(string new_text_hash, string old_text_hash) + { + return string.Compare(old_text_hash, new_text_hash, true) == 0; + } + + private bool have_same_hash_ignoring_platform(string sql_to_run, string old_text_hash) + { + // check with unix and windows line endings + const string line_ending_windows = "\r\n"; + const string line_ending_unix = "\n"; + string new_text_hash = create_hash(sql_to_run.Replace(line_ending_windows, line_ending_unix)); + bool hash_is_same = hashes_are_equal(new_text_hash, old_text_hash); + + if (!hash_is_same) + { + // try other way around + new_text_hash = create_hash(sql_to_run.Replace(line_ending_unix, line_ending_windows)); + hash_is_same = hashes_are_equal(new_text_hash, old_text_hash); + } + + return hash_is_same; + } + private bool this_script_should_run(string script_name, string sql_to_run, bool run_this_script_once, bool run_this_script_every_time) { if (this_is_an_every_time_script(script_name, run_this_script_every_time))