2018-05-07 16:47:29 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2016-04-22 18:18:46 +03:00
|
|
|
namespace :events_registrations do
|
2020-10-30 18:06:43 +09:00
|
|
|
desc "Deletes duplicate entries"
|
2016-04-22 18:18:46 +03:00
|
|
|
task deduplicate: :environment do
|
|
|
|
|
if ActiveRecord::Migrator.get_all_versions.include? 20160403214841
|
|
|
|
|
duplicates = EventsRegistration.all.map { |er| er.id if er.valid? == false}.compact
|
|
|
|
|
|
|
|
|
|
puts "Duplicates found: #{duplicates.count}"
|
2025-08-12 16:44:20 +02:00
|
|
|
if duplicates.any?
|
2016-04-22 18:18:46 +03:00
|
|
|
puts "With IDs: #{duplicates}"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
EventsRegistration.all.each do |er|
|
|
|
|
|
records = EventsRegistration.where(registration_id: er.registration_id, event_id: er.event_id)
|
2025-08-12 16:44:20 +02:00
|
|
|
if records.many?
|
2016-04-22 18:18:46 +03:00
|
|
|
# Iterate through duplicates (excluding 1st record)
|
|
|
|
|
(1..(records.count - 1)).each do |i|
|
|
|
|
|
puts "Deleting EventsRegistration record with ID #{records[i].id} ..."
|
|
|
|
|
if records[i].destroy
|
|
|
|
|
puts 'Succeeded!'
|
|
|
|
|
else
|
|
|
|
|
puts 'Faild!'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
puts 'Please migrate to run this task. Make sure your migration include 20160403214841'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|