FEATURE: rake posts:destroy_old_user_data_exports

FEATURE: rake posts:destroy_old_user_data_exports

Historically we would keep the user data export posts around but delete the uploads.

This leaves a lot of broken uploads in the system.

This rake task allows us to clean up old mess.

diff --git a/app/jobs/regular/export_csv_file.rb b/app/jobs/regular/export_csv_file.rb
index 6474618..58f1f20 100644
--- a/app/jobs/regular/export_csv_file.rb
+++ b/app/jobs/regular/export_csv_file.rb
@@ -106,7 +106,6 @@ module Jobs
     def user_list_export
       return enum_for(:user_list_export) unless block_given?
 
-      user_array = []
       user_field_ids = UserField.pluck(:id)
 
       condition = {}
diff --git a/lib/tasks/posts.rake b/lib/tasks/posts.rake
index 776aa5f..186e656 100644
--- a/lib/tasks/posts.rake
+++ b/lib/tasks/posts.rake
@@ -493,3 +493,35 @@ task 'posts:missing_uploads', [:single_site] => :environment do |_, args|
     end
   end
 end
+
+def destroy_old_user_data_exports
+  topics = Topic.with_deleted.where(<<~SQL, 2.days.ago)
+    slug = 'user-archive-data-export-complete' AND
+    archetype = 'private_message' AND
+    posts_count = 1 AND
+    created_at < ? AND
+    user_id = -1
+  SQL
+
+  puts "Found #{topics.count} old user data exports on #{RailsMultisite::ConnectionManagement.current_db}, destroying"
+  puts
+  topics.each do |t|
+    Topic.transaction do
+      t.posts.first.destroy!
+      t.destroy!
+      print "."
+    end
+  end
+  puts "done"
+end
+
+desc 'destroys all user archive PMs (they may contain broken images)'
+task 'posts:destroy_old_user_data_exports' => :environment do
+  if RailsMultisite::ConnectionManagement.current_db != "default"
+    destroy_old_user_data_exports
+  else
+    RailsMultisite::ConnectionManagement.each_connection do
+      destroy_old_user_data_exports
+    end
+  end
+end

GitHub sha: 95429365

2 Likes