diff --git a/db/migrate/20171101102630_clean_up_duplicates_in_containers_tables.rb b/db/migrate/20171101102630_clean_up_duplicates_in_containers_tables.rb index 7bc6254cc..762db62f3 100644 --- a/db/migrate/20171101102630_clean_up_duplicates_in_containers_tables.rb +++ b/db/migrate/20171101102630_clean_up_duplicates_in_containers_tables.rb @@ -39,7 +39,7 @@ class Container < ActiveRecord::Base end def duplicate_data_query_returning_min_id(model, unique_index_columns) - model.group(unique_index_columns).select("min(id)") + model.group(unique_index_columns).select("max(id)") end def cleanup_duplicate_data_delete_all(model, unique_index_columns) diff --git a/spec/migrations/20171101102630_clean_up_duplicates_in_containers_tables_spec.rb b/spec/migrations/20171101102630_clean_up_duplicates_in_containers_tables_spec.rb index fd7b98e99..04a981910 100644 --- a/spec/migrations/20171101102630_clean_up_duplicates_in_containers_tables_spec.rb +++ b/spec/migrations/20171101102630_clean_up_duplicates_in_containers_tables_spec.rb @@ -44,7 +44,7 @@ def build_value(key, foreign_key_value, string_value) def analyze(model) original_values = {} duplicate_values = {} - model.all.each do |record| + model.order("id DESC").all.each do |record| index = record.attributes.symbolize_keys.slice(*model_unique_keys(model)) if original_values[index] duplicate_values[index] << record.id @@ -64,9 +64,9 @@ def assert_before_migration_test_data(model, original_values, duplicate_values) expect(original_values.count).to eq(original_values_count(model)) expect(duplicate_values.values.flatten.count).to eq(duplicate_values_count(model)) - # Check that original values ids are the min or all duplicated ids + # Check that original values ids are the max or all duplicated ids original_values.each do |key, value| - expect((duplicate_values[key] << value).min).to eq value + expect((duplicate_values[key] << value).max).to eq value end end