diff --git a/app/tasks/maintenance/remove_non_unique_champs_task.rb b/app/tasks/maintenance/remove_non_unique_champs_task.rb new file mode 100644 index 000000000..a49862471 --- /dev/null +++ b/app/tasks/maintenance/remove_non_unique_champs_task.rb @@ -0,0 +1,21 @@ +module Maintenance + class RemoveNonUniqueChampsTask < MaintenanceTasks::Task + attribute :stable_ids, :string + validates :stable_ids, presence: true + + def collection + champs = Champ.where(stable_id: stable_ids.split(',').map(&:strip).map(&:to_i)) + champs + .group_by { [_1.dossier_id, _1.stream, _1.stable_id, _1.row_id] } + .values + .filter { _1.size > 1 } + end + + def process(champs) + champs_to_remove = champs.sort_by(&:updated_at)[0...-1] + champs_to_remove.each do |champ| + champ.update_column(:stream, 'bad_data') + end + end + end +end