Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
ae2d36cf
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
ae2d36cf
编写于
3月 03, 2018
作者:
R
Ryuta Kamizono
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Introduce `_update_row` to decouple optimistic locking concern from `Persistence` module
上级
ea45d56d
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
45 addition
and
28 deletion
+45
-28
activerecord/lib/active_record/locking/optimistic.rb
activerecord/lib/active_record/locking/optimistic.rb
+31
-0
activerecord/lib/active_record/persistence.rb
activerecord/lib/active_record/persistence.rb
+14
-28
未找到文件。
activerecord/lib/active_record/locking/optimistic.rb
浏览文件 @
ae2d36cf
...
...
@@ -111,6 +111,37 @@ def _update_record(attribute_names = self.attribute_names)
end
end
def
_update_row
(
attribute_names
,
attempted_action
)
return
super
unless
locking_enabled?
begin
locking_column
=
self
.
class
.
locking_column
previous_lock_value
=
read_attribute_before_type_cast
(
locking_column
)
attribute_names
<<
locking_column
self
[
locking_column
]
+=
1
affected_rows
=
self
.
class
.
_update_record
(
attributes_with_values
(
attribute_names
),
self
.
class
.
primary_key
=>
id_in_database
,
locking_column
=>
previous_lock_value
)
if
affected_rows
!=
1
raise
ActiveRecord
::
StaleObjectError
.
new
(
self
,
attempted_action
)
end
affected_rows
# If something went wrong, revert the locking_column value.
rescue
Exception
self
[
locking_column
]
=
previous_lock_value
raise
ensure
clear_attribute_change
(
locking_column
)
end
end
def
destroy_row
return
super
unless
locking_enabled?
...
...
activerecord/lib/active_record/persistence.rb
浏览文件 @
ae2d36cf
...
...
@@ -663,31 +663,12 @@ def touch(*names, time: nil)
unless
attribute_names
.
empty?
attribute_names
.
each
do
|
attr_name
|
write_attribute
(
attr_name
,
time
)
clear_attribute_change
(
attr_name
)
end
constraints
=
{
self
.
class
.
primary_key
=>
id_in_database
}
affected_rows
=
_update_row
(
attribute_names
,
"touch"
)
if
locking_enabled?
locking_column
=
self
.
class
.
locking_column
constraints
[
locking_column
]
=
read_attribute_before_type_cast
(
locking_column
)
attribute_names
<<
locking_column
increment_lock
end
clear_attribute_changes
(
attribute_names
)
affected_rows
=
self
.
class
.
_update_record
(
attributes_with_values_for_update
(
attribute_names
),
constraints
)
result
=
affected_rows
==
1
if
!
result
&&
locking_enabled?
raise
ActiveRecord
::
StaleObjectError
.
new
(
self
,
"touch"
)
end
@_trigger_update_callback
=
result
@_trigger_update_callback
=
affected_rows
==
1
else
true
end
...
...
@@ -707,6 +688,13 @@ def _delete_row
self
.
class
.
_delete_record
(
self
.
class
.
primary_key
=>
id_in_database
)
end
def
_update_row
(
attribute_names
,
attempted_action
)
self
.
class
.
_update_record
(
attributes_with_values
(
attribute_names
),
self
.
class
.
primary_key
=>
id_in_database
)
end
def
create_or_update
(
*
args
,
&
block
)
_raise_readonly_record_error
if
readonly?
return
false
if
destroyed?
...
...
@@ -718,15 +706,13 @@ def create_or_update(*args, &block)
# Returns the number of affected rows.
def
_update_record
(
attribute_names
=
self
.
attribute_names
)
attribute_names
&=
self
.
class
.
column_names
attributes_values
=
attributes_with_values_for_update
(
attribute_names
)
if
attributes_values
.
empty?
attribute_names
=
attributes_for_update
(
attribute_names
)
if
attribute_names
.
empty?
affected_rows
=
0
@_trigger_update_callback
=
true
else
affected_rows
=
self
.
class
.
_update_record
(
attributes_values
,
self
.
class
.
primary_key
=>
id_in_database
)
affected_rows
=
_update_row
(
attribute_names
,
"update"
)
@_trigger_update_callback
=
affected_rows
==
1
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录