From a34df536f13e36b890e939045197cacbda46aa26 Mon Sep 17 00:00:00 2001 From: Mohamed El-Sawy Date: Tue, 5 Mar 2024 18:37:56 +0200 Subject: [PATCH] Delete old tipline request and fix sentry issue (#1817) * CV2-4351: fix sentry issue * CV2-4352: delete old tipline requests(annotation of type smooch) * CV2-4352: add logs to rake task --- app/models/concerns/team_rules.rb | 2 +- ...20231122054128_migrate_tipline_requests.rake | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/models/concerns/team_rules.rb b/app/models/concerns/team_rules.rb index cfe7fac3e2..ec20105ae5 100644 --- a/app/models/concerns/team_rules.rb +++ b/app/models/concerns/team_rules.rb @@ -50,7 +50,7 @@ def text_contains_keyword(text, value) def get_smooch_message(pm) smooch_message = pm.smooch_message - smooch_message.nil? ? pm.tipline_requests.last.smooch_data : smooch_message + smooch_message.nil? ? pm.tipline_requests.last&.smooch_data : smooch_message end def title_matches_regexp(pm, value, _rule_id) diff --git a/lib/tasks/migrate/20231122054128_migrate_tipline_requests.rake b/lib/tasks/migrate/20231122054128_migrate_tipline_requests.rake index 5d3981e98c..d055867f17 100644 --- a/lib/tasks/migrate/20231122054128_migrate_tipline_requests.rake +++ b/lib/tasks/migrate/20231122054128_migrate_tipline_requests.rake @@ -448,5 +448,22 @@ namespace :check do minutes = ((Time.now.to_i - started) / 60).to_i puts "[#{Time.now}] Done in #{minutes} minutes." end + + # Remove old tipline requests (annotations of type smooch) + # bundle exec rails check:migrate:remove_old_tipline_requests + task remove_old_tipline_requests: :environment do + started = Time.now.to_i + total = Annotation.where(annotation_type: 'smooch').count + deleted = 0 + Annotation.where(annotation_type: 'smooch').find_in_batches(:batch_size => 2000) do |annotations| + ids = annotations.map(&:id) + deleted += ids.count + DynamicAnnotation::Field.where(annotation_type: 'smooch', annotation_id: ids).delete_all + Annotation.where(id: ids).delete_all + puts "\nDeleted #{deleted} / #{total}\n" + end + minutes = ((Time.now.to_i - started) / 60).to_i + puts "[#{Time.now}] Done in #{minutes} minutes." + end end end