FIX: clean up user export csv upload records in scheduled job (#7309)

FIX: clean up user export csv upload records in scheduled job (#7309)

diff --git a/app/models/user_export.rb b/app/models/user_export.rb
index 6db61c3..1438386 100644
--- a/app/models/user_export.rb
+++ b/app/models/user_export.rb
@@ -1,12 +1,9 @@
 class UserExport < ActiveRecord::Base
   belongs_to :user
+  belongs_to :upload, dependent: :destroy
 
   def self.remove_old_exports
     UserExport.where('created_at < ?', 2.days.ago).find_each do |user_export|
-      file_name = "#{user_export.file_name}-#{user_export.id}.csv.gz"
-      file_path = "#{UserExport.base_directory}/#{file_name}"
-
-      File.delete(file_path) if File.exist?(file_path)
       user_export.destroy!
     end
   end
diff --git a/spec/models/user_export_spec.rb b/spec/models/user_export_spec.rb
index f1d5432..0431d01 100644
--- a/spec/models/user_export_spec.rb
+++ b/spec/models/user_export_spec.rb
@@ -5,15 +5,19 @@ RSpec.describe UserExport do
 
   describe '.remove_old_exports' do
     it 'should remove the right records' do
+      csv_file_1 = Fabricate(:upload, created_at: 3.days.ago)
       export = UserExport.create!(
         file_name: "test",
         user: user,
+        upload_id: csv_file_1.id,
         created_at: 3.days.ago
       )
 
+      csv_file_2 = Fabricate(:upload, created_at: 1.day.ago)
       export2 = UserExport.create!(
         file_name: "test2",
         user: user,
+        upload_id: csv_file_2.id,
         created_at: 1.day.ago
       )
 
@@ -22,7 +26,9 @@ RSpec.describe UserExport do
       end.to change { UserExport.count }.by(-1)
 
       expect(UserExport.exists?(id: export.id)).to eq(false)
+      expect(Upload.exists?(id: csv_file_1.id)).to eq(false)
       expect(UserExport.exists?(id: export2.id)).to eq(true)
+      expect(Upload.exists?(id: csv_file_2.id)).to eq(true)
     end
   end
 end

GitHub sha: 7b194743

This commit has been mentioned on Discourse Meta. There might be relevant details there: