diff options
author | Stan Hu <stanhu@gmail.com> | 2018-09-04 11:57:13 -0700 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2018-09-04 12:20:58 -0700 |
commit | d4bdcfbf19b594646d597bae5eb6d0c0f7354362 (patch) | |
tree | f2e29f5c4b60128ffacd8103ef2867eeefc9b544 | |
parent | 8ff02cf7c43b7f7a2f5ca46aa678cfce67eab39b (diff) | |
download | gitlab-ce-d4bdcfbf19b594646d597bae5eb6d0c0f7354362.tar.gz |
Disable project avatar validation if avatar has not changed
Every time a column in the projects table is changed, the Avatarable concern
would validate that the avatar file size was under 200K. This not only delays
the database changes, but it also can lead to unrelated failures if the HTTP
request fails for some reason.
Closes #51053
-rw-r--r-- | app/models/concerns/avatarable.rb | 2 | ||||
-rw-r--r-- | changelogs/unreleased/sh-disable-unnecessary-avatar-revalidation.yml | 5 | ||||
-rw-r--r-- | spec/models/concerns/avatarable_spec.rb | 20 |
3 files changed, 26 insertions, 1 deletions
diff --git a/app/models/concerns/avatarable.rb b/app/models/concerns/avatarable.rb index c0233661a9b..0d5311a9985 100644 --- a/app/models/concerns/avatarable.rb +++ b/app/models/concerns/avatarable.rb @@ -9,7 +9,7 @@ module Avatarable include Gitlab::Utils::StrongMemoize validate :avatar_type, if: ->(user) { user.avatar.present? && user.avatar_changed? } - validates :avatar, file_size: { maximum: 200.kilobytes.to_i } + validates :avatar, file_size: { maximum: 200.kilobytes.to_i }, if: :avatar_changed? mount_uploader :avatar, AvatarUploader diff --git a/changelogs/unreleased/sh-disable-unnecessary-avatar-revalidation.yml b/changelogs/unreleased/sh-disable-unnecessary-avatar-revalidation.yml new file mode 100644 index 00000000000..386410484fe --- /dev/null +++ b/changelogs/unreleased/sh-disable-unnecessary-avatar-revalidation.yml @@ -0,0 +1,5 @@ +--- +title: Disable project avatar validation if avatar has not changed +merge_request: +author: +type: performance diff --git a/spec/models/concerns/avatarable_spec.rb b/spec/models/concerns/avatarable_spec.rb index 76f734079b7..7d617cb7b19 100644 --- a/spec/models/concerns/avatarable_spec.rb +++ b/spec/models/concerns/avatarable_spec.rb @@ -12,6 +12,26 @@ describe Avatarable do stub_config_setting(relative_url_root: relative_url_root) end + describe '#update' do + let(:validator) { project._validators[:avatar].detect { |v| v.is_a?(FileSizeValidator) } } + + context 'when avatar changed' do + it 'validates the file size' do + expect(validator).to receive(:validate_each).and_call_original + + project.update(avatar: 'uploads/avatar.png') + end + end + + context 'when avatar was not changed' do + it 'skips validation of file size' do + expect(validator).not_to receive(:validate_each) + + project.update(name: 'Hello world') + end + end + end + describe '#avatar_path' do using RSpec::Parameterized::TableSyntax |