From a49740f399002dc85d8110c57c0ed1b8906786e2 Mon Sep 17 00:00:00 2001 From: Manny Dinssa <44172848+Dinssa@users.noreply.github.com> Date: Wed, 9 Oct 2024 12:22:35 +0100 Subject: [PATCH] Rake task to check and remove duplicate rent periods --- lib/tasks/duplicate_rent_periods.rake | 53 +++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 lib/tasks/duplicate_rent_periods.rake diff --git a/lib/tasks/duplicate_rent_periods.rake b/lib/tasks/duplicate_rent_periods.rake new file mode 100644 index 000000000..9883dae66 --- /dev/null +++ b/lib/tasks/duplicate_rent_periods.rake @@ -0,0 +1,53 @@ +desc "Find and output each group of duplicate rent periods with a total count" +task find_redundant_rent_periods: :environment do + duplicate_groups = OrganisationRentPeriod + .select('organisation_id, rent_period') + .group('organisation_id, rent_period') + .having('COUNT(*) > 1') + + duplicate_records = OrganisationRentPeriod + .where(organisation_id: duplicate_groups.map(&:organisation_id), rent_period: duplicate_groups.map(&:rent_period)) + + duplicate_groups.each do |group| + group_records = duplicate_records.where(organisation_id: group.organisation_id, rent_period: group.rent_period) + group_records.each do |record| + puts "ID: #{record.id}, Organisation ID: #{record.organisation_id}, Rent Period: #{record.rent_period}" + end + puts "----------------------" + end + + ids_to_keep = OrganisationRentPeriod + .select('MIN(id) as id') + .group('organisation_id, rent_period') + .having('COUNT(*) > 1') + .map(&:id) + + redundant_ids = duplicate_records.pluck(:id) - ids_to_keep + + puts "Number of duplicate records: #{redundant_ids.size}" + puts "Number of records to keep: #{ids_to_keep.size}" +end + +desc "Delete redundant rent periods" +task delete_duplicate_rent_periods: :environment do + duplicate_groups = OrganisationRentPeriod + .select('organisation_id, rent_period') + .group('organisation_id, rent_period') + .having('COUNT(*) > 1') + + duplicate_ids = OrganisationRentPeriod + .where(organisation_id: duplicate_groups.map(&:organisation_id), rent_period: duplicate_groups.map(&:rent_period)) + .pluck(:id) + + ids_to_keep = OrganisationRentPeriod + .select('MIN(id) as id') + .group('organisation_id, rent_period') + .having('COUNT(*) > 1') + .map(&:id) + + redundant_ids = duplicate_ids - ids_to_keep + + OrganisationRentPeriod.where(id: redundant_ids).delete_all + + puts "Number of deleted duplicate records: #{redundant_ids.size}" +end