From 6c94659b0b05294f3e89053054dec0d80e01c748 Mon Sep 17 00:00:00 2001 From: Keith Lawrence Date: Wed, 13 Sep 2023 13:56:37 +0100 Subject: [PATCH] Add task to delete bad SubscriberLists These lists were created by accident in April 2023, the active subscribers were migrated away from the lists by this PR: https://github.com/alphagov/email-alert-api/pull/1948 The lists will delete automatically, but not for a year because of the one-year retention period for ended subscriptions. In this case, the data is confusing so worthwhile clearing up earlier. We won't touch the Subscribers, they'll be retained for a year if they have no active subscriptions, then cleared up automatically. --- lib/bad_list_deleter.rb | 56 +++++++++++++++++++++++++++++++++++++ lib/tasks/data_cleanup.rake | 12 ++++++++ 2 files changed, 68 insertions(+) create mode 100644 lib/bad_list_deleter.rb create mode 100644 lib/tasks/data_cleanup.rake diff --git a/lib/bad_list_deleter.rb b/lib/bad_list_deleter.rb new file mode 100644 index 000000000..8af299017 --- /dev/null +++ b/lib/bad_list_deleter.rb @@ -0,0 +1,56 @@ +class BadListDeleter + VALID_PREFIXES = %w[ + topic + organisations + government/people + government/ministers + government/topical-events + service-manual + service-manual/service-standard + ].freeze + + attr_reader :prefix + + def initialize(prefix) + @prefix = prefix + end + + def process_all_lists + message = "Bad list deletion not possible for the provided prefix" + raise message unless valid_prefix? + + bad_lists.each do |bad_list| + if bad_list.subscriptions.active.any? + next + end + + bad_list.destroy! + end + end + + def bad_lists + subscriber_lists - valid_links_based_lists?(subscriber_lists) + end + +private + + def valid_prefix? + VALID_PREFIXES.include?(prefix) + end + + def subscriber_lists + @subscriber_lists ||= SubscriberList.where("url LIKE ?", "%/#{prefix}/%") + end + + def valid_links_based_lists?(lists) + lists.select { |list| valid_links_based_list?(list) } + end + + def valid_links_based_list?(list) + list.content_id.nil? && has_links_or_tags(list) + end + + def has_links_or_tags(list) + list.links.present? || list.tags.present? + end +end diff --git a/lib/tasks/data_cleanup.rake b/lib/tasks/data_cleanup.rake new file mode 100644 index 000000000..baa3a147b --- /dev/null +++ b/lib/tasks/data_cleanup.rake @@ -0,0 +1,12 @@ +desc "Update one of the tags in a subscriber list" +task :delete_bad_lists => :environment do |_t, _args| + BadListDeleter::VALID_PREFIXES.each do |prefix| + deleter = BadListDeleter.new(prefix) + + bad_lists = deleter.bad_lists + subscriptions_sum = bad_lists.sum { |l| l.subscriptions.ended.count } + puts "Prefix: #{prefix} attempting to delete #{bad_lists.count} lists and #{subscriptions_sum} subscriptions" + deleter.process_all_lists + puts " - #{deleter.bad_lists.count} could not be deleted" if deleter.bad_lists.any? + end +end \ No newline at end of file