Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
a60a20bb
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,发现更多精彩内容 >>
提交
a60a20bb
编写于
8月 22, 2016
作者:
B
bogdanvlviv
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added ability update locking_column value
上级
22a822e5
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
90 addition
and
7 deletion
+90
-7
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+5
-0
activerecord/lib/active_record/locking/optimistic.rb
activerecord/lib/active_record/locking/optimistic.rb
+8
-6
activerecord/test/cases/dirty_test.rb
activerecord/test/cases/dirty_test.rb
+1
-1
activerecord/test/cases/locking_test.rb
activerecord/test/cases/locking_test.rb
+76
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
a60a20bb
*
Optimistic locking: Added ability update locking_column value.
Ignore optimistic locking if update with new locking_column value.
*bogdanvlviv*
*
Fixed: Optimistic locking does not work well with null in the database.
*
Fixed: Optimistic locking does not work well with null in the database.
Fixes #26024
Fixes #26024
...
...
activerecord/lib/active_record/locking/optimistic.rb
浏览文件 @
a60a20bb
...
@@ -78,17 +78,19 @@ def _create_record(attribute_names = self.attribute_names, *) # :nodoc:
...
@@ -78,17 +78,19 @@ def _create_record(attribute_names = self.attribute_names, *) # :nodoc:
def
_update_record
(
attribute_names
=
self
.
attribute_names
)
#:nodoc:
def
_update_record
(
attribute_names
=
self
.
attribute_names
)
#:nodoc:
return
super
unless
locking_enabled?
return
super
unless
locking_enabled?
return
0
if
attribute_names
.
empty?
lock_col
=
self
.
class
.
locking_column
lock_col
=
self
.
class
.
locking_column
return
super
if
attribute_names
.
include?
(
lock_col
)
return
0
if
attribute_names
.
empty?
begin
previous_lock_value
=
read_attribute_before_type_cast
(
lock_col
)
previous_lock_value
=
read_attribute_before_type_cast
(
lock_col
)
increment_lock
increment_lock
attribute_names
+=
[
lock_col
]
attribute_names
.
push
(
lock_col
)
attribute_names
.
uniq!
begin
relation
=
self
.
class
.
unscoped
relation
=
self
.
class
.
unscoped
affected_rows
=
relation
.
where
(
affected_rows
=
relation
.
where
(
...
...
activerecord/test/cases/dirty_test.rb
浏览文件 @
a60a20bb
...
@@ -341,7 +341,7 @@ def test_partial_update
...
@@ -341,7 +341,7 @@ def test_partial_update
def
test_partial_update_with_optimistic_locking
def
test_partial_update_with_optimistic_locking
person
=
Person
.
new
(
first_name:
"foo"
)
person
=
Person
.
new
(
first_name:
"foo"
)
old_lock_version
=
1
old_lock_version
=
person
.
lock_version
with_partial_writes
Person
,
false
do
with_partial_writes
Person
,
false
do
assert_queries
(
2
)
{
2
.
times
{
person
.
save!
}
}
assert_queries
(
2
)
{
2
.
times
{
person
.
save!
}
}
...
...
activerecord/test/cases/locking_test.rb
浏览文件 @
a60a20bb
...
@@ -247,6 +247,44 @@ def test_lock_without_default_should_work_with_null_in_the_database
...
@@ -247,6 +247,44 @@ def test_lock_without_default_should_work_with_null_in_the_database
assert_equal
"new title2"
,
t2
.
title
assert_equal
"new title2"
,
t2
.
title
end
end
def
test_lock_without_default_should_update_with_lock_col
t1
=
LockWithoutDefault
.
create
(
title:
"title1"
,
lock_version:
6
)
assert_equal
6
,
t1
.
lock_version
t1
.
update
(
lock_version:
0
)
t1
.
reload
assert_equal
0
,
t1
.
lock_version
end
def
test_lock_without_default_queries_count
t1
=
LockWithoutDefault
.
create
(
title:
"title1"
)
assert_equal
"title1"
,
t1
.
title
assert_equal
0
,
t1
.
lock_version
assert_queries
(
1
)
{
t1
.
update
(
title:
"title2"
)
}
t1
.
reload
assert_equal
"title2"
,
t1
.
title
assert_equal
1
,
t1
.
lock_version
assert_queries
(
1
)
{
t1
.
update
(
title:
"title3"
,
lock_version:
6
)
}
t1
.
reload
assert_equal
"title3"
,
t1
.
title
assert_equal
6
,
t1
.
lock_version
t2
=
LockWithoutDefault
.
new
(
title:
"title1"
)
assert_queries
(
1
)
{
t2
.
save!
}
t2
.
reload
assert_equal
"title1"
,
t2
.
title
assert_equal
0
,
t2
.
lock_version
end
def
test_lock_with_custom_column_without_default_sets_version_to_zero
def
test_lock_with_custom_column_without_default_sets_version_to_zero
t1
=
LockWithCustomColumnWithoutDefault
.
new
t1
=
LockWithCustomColumnWithoutDefault
.
new
...
@@ -283,6 +321,44 @@ def test_lock_with_custom_column_without_default_should_work_with_null_in_the_da
...
@@ -283,6 +321,44 @@ def test_lock_with_custom_column_without_default_should_work_with_null_in_the_da
assert_equal
"new title2"
,
t2
.
title
assert_equal
"new title2"
,
t2
.
title
end
end
def
test_lock_with_custom_column_without_default_should_update_with_lock_col
t1
=
LockWithCustomColumnWithoutDefault
.
create
(
title:
"title1"
,
custom_lock_version:
6
)
assert_equal
6
,
t1
.
custom_lock_version
t1
.
update
(
custom_lock_version:
0
)
t1
.
reload
assert_equal
0
,
t1
.
custom_lock_version
end
def
test_lock_with_custom_column_without_default_queries_count
t1
=
LockWithCustomColumnWithoutDefault
.
create
(
title:
"title1"
)
assert_equal
"title1"
,
t1
.
title
assert_equal
0
,
t1
.
custom_lock_version
assert_queries
(
1
)
{
t1
.
update
(
title:
"title2"
)
}
t1
.
reload
assert_equal
"title2"
,
t1
.
title
assert_equal
1
,
t1
.
custom_lock_version
assert_queries
(
1
)
{
t1
.
update
(
title:
"title3"
,
custom_lock_version:
6
)
}
t1
.
reload
assert_equal
"title3"
,
t1
.
title
assert_equal
6
,
t1
.
custom_lock_version
t2
=
LockWithCustomColumnWithoutDefault
.
new
(
title:
"title1"
)
assert_queries
(
1
)
{
t2
.
save!
}
t2
.
reload
assert_equal
"title1"
,
t2
.
title
assert_equal
0
,
t2
.
custom_lock_version
end
def
test_readonly_attributes
def
test_readonly_attributes
assert_equal
Set
.
new
([
"name"
]),
ReadonlyNameShip
.
readonly_attributes
assert_equal
Set
.
new
([
"name"
]),
ReadonlyNameShip
.
readonly_attributes
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录