Commit 707a06e0 authored by Josianne Hyson's avatar Josianne Hyson

Ignore predefined relations on Import

Currently our Milestone model has some predefined instances that are not
actually persisted to the database (only their ID is persisted on the
related model).

We don't want to import these as actual relations in the database as
their predefined IDs imply some additional functionality in the system -
which is lost if they are imported as a new record.

For now, ignore these records on import as we do not have a clear way to
handle them - but we still need to import the user generated milestones.
parent b06b4736
...@@ -134,11 +134,12 @@ class Milestone < ApplicationRecord ...@@ -134,11 +134,12 @@ class Milestone < ApplicationRecord
reorder(nil).group(:state).count reorder(nil).group(:state).count
end end
def predefined_id?(id)
[Any.id, None.id, Upcoming.id, Started.id].include?(id)
end
def predefined?(milestone) def predefined?(milestone)
milestone == Any || predefined_id?(milestone&.id)
milestone == None ||
milestone == Upcoming ||
milestone == Started
end end
end end
......
...@@ -87,7 +87,8 @@ ...@@ -87,7 +87,8 @@
"created_at": "2019-11-20T17:27:41.118Z", "created_at": "2019-11-20T17:27:41.118Z",
"updated_at": "2019-11-20T17:27:41.118Z", "updated_at": "2019-11-20T17:27:41.118Z",
"name": "first board", "name": "first board",
"milestone_id": 7638, "milestone_id": -2,
"milestone": {"id": -2, "name": "#upcoming", "title": "Upcoming"},
"group_id": 4351, "group_id": 4351,
"weight": null, "weight": null,
"labels": [], "labels": [],
...@@ -153,6 +154,31 @@ ...@@ -153,6 +154,31 @@
} }
} }
] ]
},
{
"id": 57,
"project_id": null,
"created_at": "2019-11-20T17:27:41.118Z",
"updated_at": "2019-11-20T17:27:41.118Z",
"name": "second board",
"milestone_id": 7642,
"milestone": {
"id": 7642,
"title": "v4.0",
"project_id": null,
"description": "Et laudantium enim omnis ea reprehenderit iure.",
"due_date": null,
"created_at": "2019-11-20T17:02:14.336Z",
"updated_at": "2019-11-20T17:02:14.336Z",
"state": "closed",
"iid": 5,
"start_date": null,
"group_id": 4351
},
"group_id": 4351,
"weight": null,
"labels": [],
"lists": []
} }
], ],
"members": [ "members": [
......
...@@ -46,5 +46,17 @@ describe Gitlab::ImportExport::Group::TreeRestorer do ...@@ -46,5 +46,17 @@ describe Gitlab::ImportExport::Group::TreeRestorer do
expect(lists.map(&:list_type)).to contain_exactly('assignee', 'milestone') expect(lists.map(&:list_type)).to contain_exactly('assignee', 'milestone')
end end
end end
context 'boards' do
it 'has user generated milestones' do
board = group.boards.find_by(name: 'second board')
expect(board.milestone.title).to eq 'v4.0'
end
it 'does not have predefined milestones' do
board = group.boards.find_by(name: 'first board')
expect(board.milestone).to be_nil
end
end
end end
end end
...@@ -67,7 +67,7 @@ module Gitlab ...@@ -67,7 +67,7 @@ module Gitlab
# the relation_hash, updating references with new object IDs, mapping users using # the relation_hash, updating references with new object IDs, mapping users using
# the "members_mapper" object, also updating notes if required. # the "members_mapper" object, also updating notes if required.
def create def create
return if invalid_relation? return if invalid_relation? || predefined_relation?
setup_base_models setup_base_models
setup_models setup_models
...@@ -89,6 +89,10 @@ module Gitlab ...@@ -89,6 +89,10 @@ module Gitlab
false false
end end
def predefined_relation?
relation_class.try(:predefined_id?, @relation_hash['id'])
end
def setup_models def setup_models
raise NotImplementedError raise NotImplementedError
end end
......
...@@ -33,6 +33,15 @@ describe Gitlab::ImportExport::Base::RelationFactory do ...@@ -33,6 +33,15 @@ describe Gitlab::ImportExport::Base::RelationFactory do
end end
end end
context 'when the relation is predefined' do
let(:relation_sym) { :milestone }
let(:relation_hash) { { 'name' => '#upcoming', 'title' => 'Upcoming', 'id' => -2 } }
it 'returns without creating a new relation' do
expect(subject).to be_nil
end
end
context 'when #setup_models is not implemented' do context 'when #setup_models is not implemented' do
it 'raises NotImplementedError' do it 'raises NotImplementedError' do
expect { subject }.to raise_error(NotImplementedError) expect { subject }.to raise_error(NotImplementedError)
......
...@@ -191,6 +191,16 @@ describe Milestone do ...@@ -191,6 +191,16 @@ describe Milestone do
end end
end end
describe '.predefined_id?' do
it 'returns true for a predefined Milestone ID' do
expect(Milestone.predefined_id?(described_class::Upcoming.id)).to be true
end
it 'returns false for a Milestone ID that is not predefined' do
expect(Milestone.predefined_id?(milestone.id)).to be false
end
end
describe '.order_by_name_asc' do describe '.order_by_name_asc' do
it 'sorts by name ascending' do it 'sorts by name ascending' do
milestone1 = create(:milestone, title: 'Foo') milestone1 = create(:milestone, title: 'Foo')
......
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