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

This commit is contained in:
Arpit Jalan 2019-04-03 13:31:19 +05:30 committed by GitHub
parent d1fa2b71cf
commit 7b194743d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 4 deletions

View File

@ -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

View File

@ -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