-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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: #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
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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| | ||
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 | ||