Commit 5fef4d01 authored by Robert Speicher's avatar Robert Speicher

Merge branch 'project-statistics-upload' into 'master'

Fix project statistics Uploads

See merge request gitlab-org/gitlab!71370
parents 228e7ddd 7ebd4cb2
......@@ -18,6 +18,8 @@ class Upload < ApplicationRecord
before_save :calculate_checksum!, if: :foreground_checksummable?
after_commit :schedule_checksum, if: :needs_checksum?
after_commit :update_project_statistics, on: [:create, :destroy], if: :project?
# as the FileUploader is not mounted, the default CarrierWave ActiveRecord
# hooks are not executed and the file will not be deleted
after_destroy :delete_file!, if: -> { uploader_class <= FileUploader }
......@@ -161,6 +163,14 @@ class Upload < ApplicationRecord
def mount_point
super&.to_sym
end
def project?
model_type == "Project"
end
def update_project_statistics
ProjectCacheWorker.perform_async(model_id, [], [:uploads_size])
end
end
Upload.prepend_mod_with('Upload')
......@@ -242,4 +242,28 @@ RSpec.describe Upload do
it { expect(subject.uploader_context).to match(a_hash_including(secret: 'secret', identifier: 'file.txt')) }
end
describe '#update_project_statistics' do
let_it_be(:project) { create(:project) }
subject do
create(:upload, model: project)
end
it 'updates project statistics when upload is added' do
expect(ProjectCacheWorker).to receive(:perform_async)
.with(project.id, [], [:uploads_size])
subject.save!
end
it 'updates project statistics when upload is removed' do
subject.save!
expect(ProjectCacheWorker).to receive(:perform_async)
.with(project.id, [], [:uploads_size])
subject.destroy!
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment