diff options
author | Stan Hu <stanhu@gmail.com> | 2019-07-24 18:01:44 +0000 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2019-07-24 18:01:44 +0000 |
commit | 0d538e44aff066372ecd9d10ac6786681bc347c9 (patch) | |
tree | 5a0fb1f83246d27a63f0d201c3fd11963f5b25de /app/models/project.rb | |
parent | f73d65197bfa3f608350f6f96e1445a7060d90e5 (diff) | |
parent | 8d1e97fc3b9af28d2a34d2b16239e52d3b5d0303 (diff) | |
download | gitlab-ce-0d538e44aff066372ecd9d10ac6786681bc347c9.tar.gz |
Merge branch 'optimise-import-performance' into 'master'
Optimise import performance
Closes #64924
See merge request gitlab-org/gitlab-ce!31045
Diffstat (limited to 'app/models/project.rb')
-rw-r--r-- | app/models/project.rb | 32 |
1 files changed, 14 insertions, 18 deletions
diff --git a/app/models/project.rb b/app/models/project.rb index 8030c645e2e..0020e423628 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1862,16 +1862,24 @@ class Project < ApplicationRecord end def append_or_update_attribute(name, value) - old_values = public_send(name.to_s) # rubocop:disable GitlabSecurity/PublicSend + if Project.reflect_on_association(name).try(:macro) == :has_many + # if this is 1-to-N relation, update the parent object + value.each do |item| + item.update!( + Project.reflect_on_association(name).foreign_key => id) + end + + # force to drop relation cache + public_send(name).reset # rubocop:disable GitlabSecurity/PublicSend - if Project.reflect_on_association(name).try(:macro) == :has_many && old_values.any? - update_attribute(name, old_values + value) + # succeeded + true else + # if this is another relation or attribute, update just object update_attribute(name, value) end - - rescue ActiveRecord::RecordNotSaved => e - handle_update_attribute_error(e, value) + rescue ActiveRecord::RecordInvalid => e + raise e, "Failed to set #{name}: #{e.message}" end # Tries to set repository as read_only, checking for existing Git transfers in progress beforehand @@ -2260,18 +2268,6 @@ class Project < ApplicationRecord ContainerRepository.build_root_repository(self).has_tags? end - def handle_update_attribute_error(ex, value) - if ex.message.start_with?('Failed to replace') - if value.respond_to?(:each) - invalid = value.detect(&:invalid?) - - raise ex, ([ex.message] + invalid.errors.full_messages).join(' ') if invalid - end - end - - raise ex - end - def fetch_branch_allows_collaboration(user, branch_name = nil) return false unless user |