diff options
author | James Lopez <james@jameslopez.es> | 2018-06-20 15:45:15 +0200 |
---|---|---|
committer | James Lopez <james@jameslopez.es> | 2018-06-20 15:45:15 +0200 |
commit | 8522173e0ad912c41aaa68f4e01f03ac73cc4514 (patch) | |
tree | 52f90785e2c12eca84cfcd19e7c2e4705cdd224f | |
parent | cc061cd1c664f2003d4c7abb3930834704d2eb9e (diff) | |
download | gitlab-ce-8522173e0ad912c41aaa68f4e01f03ac73cc4514.tar.gz |
refactor code once again to fix IID issues
-rw-r--r-- | lib/gitlab/import_export/group_project_finder.rb | 29 | ||||
-rw-r--r-- | lib/gitlab/import_export/relation_factory.rb | 32 | ||||
-rw-r--r-- | spec/lib/gitlab/import_export/group_project_finder_spec.rb | 28 |
3 files changed, 47 insertions, 42 deletions
diff --git a/lib/gitlab/import_export/group_project_finder.rb b/lib/gitlab/import_export/group_project_finder.rb index cd239466a5c..37923edb9db 100644 --- a/lib/gitlab/import_export/group_project_finder.rb +++ b/lib/gitlab/import_export/group_project_finder.rb @@ -31,7 +31,9 @@ module Gitlab private def where_clause - @attributes.except(:group, :project).map do |key, value| + return { project_id: @project.id } unless milestone? || label? + + @attributes.slice(:title).map do |key, value| project_clause = table[key].eq(value).and(table[:project_id].eq(@project.id)) if @group @@ -48,13 +50,36 @@ module Gitlab def project_attributes @attributes.except(:group).tap do |atts| - atts['type'] = 'ProjectLabel' if label? + if label? + atts['type'] = 'ProjectLabel' + elsif milestone? + if atts['group_id'] + atts['iid'] = nil + atts.delete('group_id') + else + claim_iid + end + end end end def label? @klass == Label || @klass < Label end + + def milestone? + @klass == Milestone + end + + def claim_iid + group_milestone = @project.milestones.find_by(iid: @attributes['iid']) + + group_milestone.update!(iid: max_milestone_iid + 1) if group_milestone + end + + def max_milestone_iid + [@attributes['iid'], @project.milestones.maximum(:iid)].compact.max + end end end end diff --git a/lib/gitlab/import_export/relation_factory.rb b/lib/gitlab/import_export/relation_factory.rb index ff3873bc8ed..992e3e708d5 100644 --- a/lib/gitlab/import_export/relation_factory.rb +++ b/lib/gitlab/import_export/relation_factory.rb @@ -257,15 +257,7 @@ module Gitlab end def existing_object - @existing_object ||= - begin - existing_object = find_or_create_object! - - # Done in two steps, as MySQL behaves differently than PostgreSQL using - # the +find_or_create_by+ method and does not return the ID the second time. - existing_object.update!(parsed_relation_hash) - existing_object - end + @existing_object ||= find_or_create_object! end def unknown_service? @@ -275,29 +267,17 @@ module Gitlab def find_or_create_object! # Can't use IDs as validation exists calilng `.group` or `.project` - finder_hash = { project: @project }.tap do |hash| + finder_hash = parsed_relation_hash.tap do |hash| hash[:group] = @project.group if relation_class.attribute_method?('group_id') + hash[:project] = @project hash[:title] = parsed_relation_hash['title'] if parsed_relation_hash['title'] + hash.delete('project_id') end if label? - label = GroupProjectFinder.find_or_new(Label, finder_hash) - parsed_relation_hash.delete('priorities') if label.persisted? - parsed_relation_hash.delete('type') - - label.save! - label + GroupProjectFinder.find_or_new(Label, finder_hash) else - object = GroupProjectFinder.find_or_create(relation_class, finder_hash) - - if milestone? - parsed_relation_hash.delete('group_id') if object.project - parsed_relation_hash.delete('project_id') if object.group - parsed_relation_hash.delete('iid') - parsed_relation_hash.delete('id') - end - - object + GroupProjectFinder.find_or_create(relation_class, finder_hash) end end diff --git a/spec/lib/gitlab/import_export/group_project_finder_spec.rb b/spec/lib/gitlab/import_export/group_project_finder_spec.rb index a86f0c2dd44..c36d80e51d4 100644 --- a/spec/lib/gitlab/import_export/group_project_finder_spec.rb +++ b/spec/lib/gitlab/import_export/group_project_finder_spec.rb @@ -16,15 +16,15 @@ describe Gitlab::ImportExport::GroupProjectFinder do expect(described_class.find_or_new(Label, title: 'group label', - 'project_id' => project.id, - 'group_id' => project.group.id)).to eq(group_label) + project: project, + group: project.group)).to eq(group_label) end it 'initializes a new label' do label = described_class.find_or_new(Label, title: 'group label', - 'project_id' => project.id, - 'group_id' => project.group.id) + project: project, + group: project.group) expect(label.persisted?).to be false end @@ -32,8 +32,8 @@ describe Gitlab::ImportExport::GroupProjectFinder do it 'creates a new label' do label = described_class.find_or_create(Label, title: 'group label', - 'project_id' => project.id, - 'group_id' => project.group.id) + project: project, + group: project.group) expect(label.persisted?).to be true end @@ -45,24 +45,24 @@ describe Gitlab::ImportExport::GroupProjectFinder do expect(described_class.find_or_new(Milestone, title: 'group milestone', - 'project_id' => project.id, - 'group_id' => project.group.id)).to eq(milestone) + project: project, + group: project.group)).to eq(milestone) end it 'initializes a new milestone' do milestone = described_class.find_or_new(Milestone, - title: 'group milestone', - 'project_id' => project.id, - 'group_id' => project.group.id) + title: 'group milestone', + project: project, + group: project.group) expect(milestone.persisted?).to be false end it 'creates a new milestone' do milestone = described_class.find_or_create(Milestone, - title: 'group milestone', - 'project_id' => project.id, - 'group_id' => project.group.id) + title: 'group milestone', + project: project, + group: project.group) expect(milestone.persisted?).to be true end |