diff options
author | Adam Niedzielski <adamsunday@gmail.com> | 2017-06-26 17:36:09 +0200 |
---|---|---|
committer | Adam Niedzielski <adamsunday@gmail.com> | 2017-06-26 17:36:09 +0200 |
commit | 0eb3d18c569cbae0f4b58284857c7a21972c4153 (patch) | |
tree | 50d70b3ce1fb007457cebff4dfcad44fafdbb7f5 | |
parent | d4384dc154cb1e44c7b0a5cd802d48629f585fdb (diff) | |
download | gitlab-ce-0eb3d18c569cbae0f4b58284857c7a21972c4153.tar.gz |
Store merge request ref_fetched status in the database34052-store-mr-ref-fetched-in-database
Closes #34052
-rw-r--r-- | app/models/merge_request.rb | 9 | ||||
-rw-r--r-- | changelogs/unreleased/34052-store-mr-ref-fetched-in-database.yml | 4 | ||||
-rw-r--r-- | db/migrate/20170622162730_add_ref_fetched_to_merge_request.rb | 9 | ||||
-rw-r--r-- | db/schema.rb | 3 | ||||
-rw-r--r-- | lib/gitlab/import_export/import_export.yml | 1 | ||||
-rw-r--r-- | spec/lib/gitlab/import_export/fork_spec.rb | 4 | ||||
-rw-r--r-- | spec/models/merge_request_spec.rb | 36 |
7 files changed, 63 insertions, 3 deletions
diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb index f581a25f093..c099d731082 100644 --- a/app/models/merge_request.rb +++ b/app/models/merge_request.rb @@ -771,6 +771,7 @@ class MergeRequest < ActiveRecord::Base "refs/heads/#{source_branch}", ref_path ) + update_column(:ref_fetched, true) end def ref_path @@ -778,7 +779,13 @@ class MergeRequest < ActiveRecord::Base end def ref_fetched? - project.repository.ref_exists?(ref_path) + super || + begin + computed_value = project.repository.ref_exists?(ref_path) + update_column(:ref_fetched, true) if computed_value + + computed_value + end end def ensure_ref_fetched diff --git a/changelogs/unreleased/34052-store-mr-ref-fetched-in-database.yml b/changelogs/unreleased/34052-store-mr-ref-fetched-in-database.yml new file mode 100644 index 00000000000..4bacfca7551 --- /dev/null +++ b/changelogs/unreleased/34052-store-mr-ref-fetched-in-database.yml @@ -0,0 +1,4 @@ +--- +title: Store merge request ref_fetched status in the database +merge_request: 12424 +author: diff --git a/db/migrate/20170622162730_add_ref_fetched_to_merge_request.rb b/db/migrate/20170622162730_add_ref_fetched_to_merge_request.rb new file mode 100644 index 00000000000..62aa1a4b4f0 --- /dev/null +++ b/db/migrate/20170622162730_add_ref_fetched_to_merge_request.rb @@ -0,0 +1,9 @@ +class AddRefFetchedToMergeRequest < ActiveRecord::Migration + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + def change + add_column :merge_requests, :ref_fetched, :boolean + end +end diff --git a/db/schema.rb b/db/schema.rb index 028556bdccf..8c7440ee610 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170621102400) do +ActiveRecord::Schema.define(version: 20170622162730) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -770,6 +770,7 @@ ActiveRecord::Schema.define(version: 20170621102400) do t.datetime "last_edited_at" t.integer "last_edited_by_id" t.integer "head_pipeline_id" + t.boolean "ref_fetched" end add_index "merge_requests", ["assignee_id"], name: "index_merge_requests_on_assignee_id", using: :btree diff --git a/lib/gitlab/import_export/import_export.yml b/lib/gitlab/import_export/import_export.yml index 72183e8aad4..1860352c96d 100644 --- a/lib/gitlab/import_export/import_export.yml +++ b/lib/gitlab/import_export/import_export.yml @@ -99,6 +99,7 @@ excluded_attributes: - :milestone_id merge_requests: - :milestone_id + - :ref_fetched award_emoji: - :awardable_id statuses: diff --git a/spec/lib/gitlab/import_export/fork_spec.rb b/spec/lib/gitlab/import_export/fork_spec.rb index 42f3fc59f04..70796781532 100644 --- a/spec/lib/gitlab/import_export/fork_spec.rb +++ b/spec/lib/gitlab/import_export/fork_spec.rb @@ -44,6 +44,8 @@ describe 'forked project import', services: true do end it 'can access the MR' do - expect(project.merge_requests.first.ensure_ref_fetched.first).to include('refs/merge-requests/1/head') + project.merge_requests.first.ensure_ref_fetched + + expect(project.repository.ref_exists?('refs/merge-requests/1/head')).to be_truthy end end diff --git a/spec/models/merge_request_spec.rb b/spec/models/merge_request_spec.rb index 1240c9745e2..bb5273074a2 100644 --- a/spec/models/merge_request_spec.rb +++ b/spec/models/merge_request_spec.rb @@ -1574,4 +1574,40 @@ describe MergeRequest, models: true do end end end + + describe '#fetch_ref' do + it 'sets "ref_fetched" flag to true' do + subject.update!(ref_fetched: nil) + + subject.fetch_ref + + expect(subject.reload.ref_fetched).to be_truthy + end + end + + describe '#ref_fetched?' do + it 'does not perform git operation when value is cached' do + subject.ref_fetched = true + + expect_any_instance_of(Repository).not_to receive(:ref_exists?) + expect(subject.ref_fetched?).to be_truthy + end + + it 'caches the value when ref exists but value is not cached' do + subject.update!(ref_fetched: nil) + allow_any_instance_of(Repository).to receive(:ref_exists?) + .and_return(true) + + expect(subject.ref_fetched?).to be_truthy + expect(subject.reload.ref_fetched).to be_truthy + end + + it 'returns false when ref does not exist' do + subject.update!(ref_fetched: nil) + allow_any_instance_of(Repository).to receive(:ref_exists?) + .and_return(false) + + expect(subject.ref_fetched?).to be_falsey + end + end end |