Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Léo-Paul Géneau
gitlab-ce
Commits
49dc8215
Commit
49dc8215
authored
Aug 06, 2018
by
Oswaldo Ferreira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Avoid N+1 on MRs page when metrics merging date cannot be found
parent
415b2f94
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
89 additions
and
11 deletions
+89
-11
app/models/merge_request.rb
app/models/merge_request.rb
+17
-11
changelogs/unreleased/osw-fix-n-plus-1-for-mrs-without-merge-info.yml
...nreleased/osw-fix-n-plus-1-for-mrs-without-merge-info.yml
+5
-0
spec/models/merge_request_spec.rb
spec/models/merge_request_spec.rb
+67
-0
No files found.
app/models/merge_request.rb
View file @
49dc8215
...
...
@@ -1074,23 +1074,29 @@ class MergeRequest < ActiveRecord::Base
def
can_be_reverted?
(
current_user
)
return
false
unless
merge_commit
return
false
unless
merged_at
merged_at
=
metrics
&
.
merged_at
notes_association
=
notes_with_associations
# It is not guaranteed that Note#created_at will be strictly later than
# MergeRequestMetric#merged_at. Nanoseconds on MySQL may break this
# comparison, as will a HA environment if clocks are not *precisely*
# synchronized. Add a minute's leeway to compensate for both possibilities
cutoff
=
merged_at
-
1
.
minute
if
merged_at
# It is not guaranteed that Note#created_at will be strictly later than
# MergeRequestMetric#merged_at. Nanoseconds on MySQL may break this
# comparison, as will a HA environment if clocks are not *precisely*
# synchronized. Add a minute's leeway to compensate for both possibilities
cutoff
=
merged_at
-
1
.
minute
notes_association
=
notes_association
.
where
(
'created_at >= ?'
,
cutoff
)
end
notes_association
=
notes_with_associations
.
where
(
'created_at >= ?'
,
cutoff
)
!
merge_commit
.
has_been_reverted?
(
current_user
,
notes_association
)
end
def
merged_at
strong_memoize
(
:merged_at
)
do
next
unless
merged?
metrics
&
.
merged_at
||
merge_event
&
.
created_at
||
notes
.
system
.
reorder
(
nil
).
find_by
(
note:
'merged'
)
&
.
created_at
end
end
def
can_be_cherry_picked?
merge_commit
.
present?
end
...
...
changelogs/unreleased/osw-fix-n-plus-1-for-mrs-without-merge-info.yml
0 → 100644
View file @
49dc8215
---
title
:
Avoid N+1 on MRs page when metrics merging date cannot be found
merge_request
:
21053
author
:
type
:
performance
spec/models/merge_request_spec.rb
View file @
49dc8215
...
...
@@ -1288,6 +1288,16 @@ describe MergeRequest do
project
.
default_branch
==
branch
)
end
context
'but merged at timestamp cannot be found'
do
before
do
allow
(
subject
).
to
receive
(
:merged_at
)
{
nil
}
end
it
'returns false'
do
expect
(
subject
.
can_be_reverted?
(
current_user
)).
to
be_falsey
end
end
context
'when the revert commit is mentioned in a note after the MR was merged'
do
it
'returns false'
do
expect
(
subject
.
can_be_reverted?
(
current_user
)).
to
be_falsey
...
...
@@ -1327,6 +1337,63 @@ describe MergeRequest do
end
end
describe
'#merged_at'
do
context
'when MR is not merged'
do
let
(
:merge_request
)
{
create
(
:merge_request
,
:closed
)
}
it
'returns nil'
do
expect
(
merge_request
.
merged_at
).
to
be_nil
end
end
context
'when metrics has merged_at data'
do
let
(
:merge_request
)
{
create
(
:merge_request
,
:merged
)
}
before
do
merge_request
.
metrics
.
update!
(
merged_at:
1
.
day
.
ago
)
end
it
'returns metrics merged_at'
do
expect
(
merge_request
.
merged_at
).
to
eq
(
merge_request
.
metrics
.
merged_at
)
end
end
context
'when merged event is persisted, but no metrics merged_at is persisted'
do
let
(
:user
)
{
create
(
:user
)
}
let
(
:merge_request
)
{
create
(
:merge_request
,
:merged
)
}
before
do
EventCreateService
.
new
.
merge_mr
(
merge_request
,
user
)
end
it
'returns merged event creation date'
do
expect
(
merge_request
.
merge_event
).
to
be_persisted
expect
(
merge_request
.
merged_at
).
to
eq
(
merge_request
.
merge_event
.
created_at
)
end
end
context
'when merging note is persisted, but no metrics or merge event exists'
do
let
(
:user
)
{
create
(
:user
)
}
let
(
:merge_request
)
{
create
(
:merge_request
,
:merged
)
}
before
do
merge_request
.
metrics
.
destroy!
SystemNoteService
.
change_status
(
merge_request
,
merge_request
.
target_project
,
user
,
merge_request
.
state
,
nil
)
end
it
'returns merging note creation date'
do
expect
(
merge_request
.
reload
.
metrics
).
to
be_nil
expect
(
merge_request
.
merge_event
).
to
be_nil
expect
(
merge_request
.
notes
.
count
).
to
eq
(
1
)
expect
(
merge_request
.
merged_at
).
to
eq
(
merge_request
.
notes
.
first
.
created_at
)
end
end
end
describe
'#participants'
do
let
(
:project
)
{
create
(
:project
,
:public
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment