Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
2c78c7f4
G
gitlab-foss
项目概览
李少辉-开发者
/
gitlab-foss
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-foss
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
2c78c7f4
编写于
7月 04, 2017
作者:
D
Douwe Maan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Revert "Merge branch 'revert-12499' into 'master'"
This reverts merge request !12557
上级
d453bb86
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
52 addition
and
4 deletion
+52
-4
app/controllers/projects/issues_controller.rb
app/controllers/projects/issues_controller.rb
+1
-1
app/models/concerns/sortable.rb
app/models/concerns/sortable.rb
+23
-0
app/models/project.rb
app/models/project.rb
+3
-3
changelogs/unreleased/dm-drop-default-scope-on-sortable-finders.yml
.../unreleased/dm-drop-default-scope-on-sortable-finders.yml
+4
-0
spec/models/concerns/sortable_spec.rb
spec/models/concerns/sortable_spec.rb
+21
-0
未找到文件。
app/controllers/projects/issues_controller.rb
浏览文件 @
2c78c7f4
...
@@ -227,7 +227,7 @@ class Projects::IssuesController < Projects::ApplicationController
...
@@ -227,7 +227,7 @@ class Projects::IssuesController < Projects::ApplicationController
def
issue
def
issue
return
@issue
if
defined?
(
@issue
)
return
@issue
if
defined?
(
@issue
)
# The Sortable default scope causes performance issues when used with find_by
# The Sortable default scope causes performance issues when used with find_by
@noteable
=
@issue
||=
@project
.
issues
.
where
(
iid:
params
[
:id
]).
reorder
(
nil
).
take!
@noteable
=
@issue
||=
@project
.
issues
.
find_by!
(
iid:
params
[
:id
])
return
render_404
unless
can?
(
current_user
,
:read_issue
,
@issue
)
return
render_404
unless
can?
(
current_user
,
:read_issue
,
@issue
)
...
...
app/models/concerns/sortable.rb
浏览文件 @
2c78c7f4
...
@@ -5,6 +5,25 @@
...
@@ -5,6 +5,25 @@
module
Sortable
module
Sortable
extend
ActiveSupport
::
Concern
extend
ActiveSupport
::
Concern
module
DropDefaultScopeOnFinders
# Override these methods to drop the `ORDER BY id DESC` default scope.
# See http://dba.stackexchange.com/a/110919 for why we do this.
%i[find find_by find_by!]
.
each
do
|
meth
|
define_method
meth
do
|*
args
,
&
block
|
return
super
(
*
args
,
&
block
)
if
block
unordered_relation
=
unscope
(
:order
)
# We cannot simply call `meth` on `unscope(:order)`, since that is also
# an instance of the same relation class this module is included into,
# which means we'd get infinite recursion.
# We explicitly use the original implementation to prevent this.
original_impl
=
method
(
__method__
).
super_method
.
unbind
original_impl
.
bind
(
unordered_relation
).
call
(
*
args
)
end
end
end
included
do
included
do
# By default all models should be ordered
# By default all models should be ordered
# by created_at field starting from newest
# by created_at field starting from newest
...
@@ -18,6 +37,10 @@ module Sortable
...
@@ -18,6 +37,10 @@ module Sortable
scope
:order_updated_asc
,
->
{
reorder
(
updated_at: :asc
)
}
scope
:order_updated_asc
,
->
{
reorder
(
updated_at: :asc
)
}
scope
:order_name_asc
,
->
{
reorder
(
name: :asc
)
}
scope
:order_name_asc
,
->
{
reorder
(
name: :asc
)
}
scope
:order_name_desc
,
->
{
reorder
(
name: :desc
)
}
scope
:order_name_desc
,
->
{
reorder
(
name: :desc
)
}
# All queries (relations) on this model are instances of this `relation_klass`.
relation_klass
=
relation_delegate_class
(
ActiveRecord
::
Relation
)
relation_klass
.
prepend
DropDefaultScopeOnFinders
end
end
module
ClassMethods
module
ClassMethods
...
...
app/models/project.rb
浏览文件 @
2c78c7f4
...
@@ -815,7 +815,7 @@ class Project < ActiveRecord::Base
...
@@ -815,7 +815,7 @@ class Project < ActiveRecord::Base
end
end
def
ci_service
def
ci_service
@ci_service
||=
ci_services
.
reorder
(
nil
).
find_by
(
active:
true
)
@ci_service
||=
ci_services
.
find_by
(
active:
true
)
end
end
def
deployment_services
def
deployment_services
...
@@ -823,7 +823,7 @@ class Project < ActiveRecord::Base
...
@@ -823,7 +823,7 @@ class Project < ActiveRecord::Base
end
end
def
deployment_service
def
deployment_service
@deployment_service
||=
deployment_services
.
reorder
(
nil
).
find_by
(
active:
true
)
@deployment_service
||=
deployment_services
.
find_by
(
active:
true
)
end
end
def
monitoring_services
def
monitoring_services
...
@@ -831,7 +831,7 @@ class Project < ActiveRecord::Base
...
@@ -831,7 +831,7 @@ class Project < ActiveRecord::Base
end
end
def
monitoring_service
def
monitoring_service
@monitoring_service
||=
monitoring_services
.
reorder
(
nil
).
find_by
(
active:
true
)
@monitoring_service
||=
monitoring_services
.
find_by
(
active:
true
)
end
end
def
jira_tracker?
def
jira_tracker?
...
...
changelogs/unreleased/dm-drop-default-scope-on-sortable-finders.yml
0 → 100644
浏览文件 @
2c78c7f4
---
title
:
Improve performance of lookups of issues, merge requests etc by dropping unnecessary ORDER BY clause
merge_request
:
author
:
spec/models/concerns/sortable_spec.rb
0 → 100644
浏览文件 @
2c78c7f4
require
'spec_helper'
describe
Sortable
do
let
(
:relation
)
{
Issue
.
all
}
describe
'#where'
do
it
'orders by id, descending'
do
order_node
=
relation
.
where
(
iid:
1
).
order_values
.
first
expect
(
order_node
).
to
be_a
(
Arel
::
Nodes
::
Descending
)
expect
(
order_node
.
expr
.
name
).
to
eq
(
:id
)
end
end
describe
'#find_by'
do
it
'does not order'
do
expect
(
relation
).
to
receive
(
:unscope
).
with
(
:order
).
and_call_original
relation
.
find_by
(
iid:
1
)
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录