Skip to content

Commit

Permalink
Merge pull request #104 from lahma/ignore-eol-changes
Browse files Browse the repository at this point in the history
Ignore EOL changes when checking whether script has changed

Looks good. Thanks.
  • Loading branch information
BiggerNoise committed Jan 19, 2013
2 parents 39ce6b4 + b00dcf2 commit 1e9e782
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions product/roundhouse/migrators/DefaultDatabaseMigrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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))
Expand Down

0 comments on commit 1e9e782

Please sign in to comment.