Skip to content

Commit

Permalink
Add task to delete bad SubscriberLists
Browse files Browse the repository at this point in the history
These lists were created by accident in April 2023, the active
subscribers were migrated away from the lists by this PR:
#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.
  • Loading branch information
KludgeKML committed Sep 14, 2023
1 parent 2f77cef commit 6c94659
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
56 changes: 56 additions & 0 deletions lib/bad_list_deleter.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions lib/tasks/data_cleanup.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
desc "Update one of the tags in a subscriber list"
task :delete_bad_lists => :environment do |_t, _args|

Check failure on line 2 in lib/tasks/data_cleanup.rake

View workflow job for this annotation

GitHub Actions / Lint Ruby / Run RuboCop

Style/HashSyntax: Use the new Ruby 1.9 hash syntax. (https://rubystyle.guide#hash-literals)
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

Check failure on line 12 in lib/tasks/data_cleanup.rake

View workflow job for this annotation

GitHub Actions / Lint Ruby / Run RuboCop

Layout/TrailingEmptyLines: Final newline missing. (https://rubystyle.guide#newline-eof)

0 comments on commit 6c94659

Please sign in to comment.