Browse Source

CLDC-4406: Add a script to delete logs in batches (#3306)

pull/3309/head v0.6.8
Samuel Young 4 days ago committed by GitHub
parent
commit
31e707495a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 34
      lib/tasks/delete_logs_before_year.rake

34
lib/tasks/delete_logs_before_year.rake

@ -0,0 +1,34 @@
desc "Deletes all logs in a given collection year and earlier. Note that this operation is PERMANENT and this will bypass callbacks/paper trail. Use only as instructed in a yearly cleanup task."
task :delete_logs_before_year, %i[year] => :environment do |_task, args|
year = args[:year].to_i
if year < 2020
raise ArgumentError, "Year must be above 2020. Make sure you've written out the entire year"
end
if year > Time.zone.now.year - 3
raise ArgumentError, "Year cannot be the last 3 years, as these may contain visible logs"
end
puts "Deleting Logs before #{year}"
puts "Deleting Sales Logs in batches of 10000"
logs = SalesLog.filter_by_year_or_earlier(year)
logs.in_batches(of: 10_000).each_with_index do |logs, i|
puts "Deleting batch #{i + 1}"
logs.delete_all
end
puts "Done deleting Sales Logs"
puts "Deleting Lettings Logs in batches of 10000"
logs = LettingsLog.filter_by_year_or_earlier(year)
logs.in_batches(of: 10_000).each_with_index do |logs, i|
puts "Deleting batch #{i + 1}"
logs.delete_all
end
puts "Done deleting Lettings Logs"
puts "Done deleting Logs before #{year}"
end
Loading…
Cancel
Save