From c00e21adb9bfe715cdbbc11a1df597cc7f705b11 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Tue, 5 Apr 2022 11:51:50 +0200 Subject: [PATCH] after_party task which removes duplicate attestation --- ...5092317_remove_duplicate_attestations.rake | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 lib/tasks/deployment/20220405092317_remove_duplicate_attestations.rake diff --git a/lib/tasks/deployment/20220405092317_remove_duplicate_attestations.rake b/lib/tasks/deployment/20220405092317_remove_duplicate_attestations.rake new file mode 100644 index 000000000..c0dfbc54a --- /dev/null +++ b/lib/tasks/deployment/20220405092317_remove_duplicate_attestations.rake @@ -0,0 +1,25 @@ +namespace :after_party do + desc 'Deployment task: remove_duplicate_attestations' + task remove_duplicate_attestations: :environment do + dossier_ids = Attestation + .group(:dossier_id) + .count + .filter { |_, attestation_count| attestation_count > 1 } + .map(&:first) + + dossier_ids.each do |dossier_id| + current_attestation, *old_attestations = Attestation.where(dossier_id: dossier_id).order(created_at: :desc) + + old_attestations.each do |old| + if current_attestation.created_at < old.created_at + raise "something odd, old attestation #{old.id} is newer than the current attestation #{current_attestation.id}" + end + + old.destroy + end + end + + AfterParty::TaskRecord + .create version: AfterParty::TaskRecorder.new(__FILE__).timestamp + end +end