Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
3610997b
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,发现更多精彩内容 >>
提交
3610997b
编写于
6月 15, 2008
作者:
D
Daniel Morrison
提交者:
Jeremy Kemper
6月 22, 2008
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Partial updates don't update lock_version if nothing changed. [#426 state:resolved]
上级
0fd3e4cd
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
42 addition
and
0 deletion
+42
-0
activerecord/CHANGELOG
activerecord/CHANGELOG
+2
-0
activerecord/lib/active_record/locking/optimistic.rb
activerecord/lib/active_record/locking/optimistic.rb
+1
-0
activerecord/test/cases/dirty_test.rb
activerecord/test/cases/dirty_test.rb
+19
-0
activerecord/test/cases/locking_test.rb
activerecord/test/cases/locking_test.rb
+20
-0
未找到文件。
activerecord/CHANGELOG
浏览文件 @
3610997b
*Edge*
* Partial updates don't update lock_version if nothing changed. #426 [Daniel Morrison]
* Fix column collision with named_scope and :joins. #46 [Duncan Beevers, Mark Catley]
* db:migrate:down and :up update schema_migrations. #369 [Michael Raidel, RaceCondition]
...
...
activerecord/lib/active_record/locking/optimistic.rb
浏览文件 @
3610997b
...
...
@@ -68,6 +68,7 @@ def attributes_from_column_definition_with_lock
def
update_with_lock
(
attribute_names
=
@attributes
.
keys
)
#:nodoc:
return
update_without_lock
(
attribute_names
)
unless
locking_enabled?
return
0
if
attribute_names
.
empty?
lock_col
=
self
.
class
.
locking_column
previous_value
=
send
(
lock_col
).
to_i
...
...
activerecord/test/cases/dirty_test.rb
浏览文件 @
3610997b
...
...
@@ -2,6 +2,7 @@
require
'models/topic'
# For booleans
require
'models/pirate'
# For timestamps
require
'models/parrot'
require
'models/person'
# For optimistic locking
class
Pirate
# Just reopening it, not defining it
attr_accessor
:detected_changes_in_after_update
# Boolean for if changes are detected
...
...
@@ -125,6 +126,24 @@ def test_partial_update
end
end
def
test_partial_update_with_optimistic_locking
person
=
Person
.
new
(
:first_name
=>
'foo'
)
old_lock_version
=
1
with_partial_updates
Person
,
false
do
assert_queries
(
2
)
{
2
.
times
{
person
.
save!
}
}
Person
.
update_all
({
:first_name
=>
'baz'
},
:id
=>
person
.
id
)
end
with_partial_updates
Person
,
true
do
assert_queries
(
0
)
{
2
.
times
{
person
.
save!
}
}
assert_equal
old_lock_version
,
person
.
reload
.
lock_version
assert_queries
(
1
)
{
person
.
first_name
=
'bar'
;
person
.
save!
}
assert_not_equal
old_lock_version
,
person
.
reload
.
lock_version
end
end
def
test_changed_attributes_should_be_preserved_if_save_failure
pirate
=
Pirate
.
new
pirate
.
parrot_id
=
1
...
...
activerecord/test/cases/locking_test.rb
浏览文件 @
3610997b
...
...
@@ -29,10 +29,12 @@ def test_lock_existing
assert_equal
0
,
p1
.
lock_version
assert_equal
0
,
p2
.
lock_version
p1
.
first_name
=
'stu'
p1
.
save!
assert_equal
1
,
p1
.
lock_version
assert_equal
0
,
p2
.
lock_version
p2
.
first_name
=
'sue'
assert_raises
(
ActiveRecord
::
StaleObjectError
)
{
p2
.
save!
}
end
...
...
@@ -42,11 +44,14 @@ def test_lock_repeating
assert_equal
0
,
p1
.
lock_version
assert_equal
0
,
p2
.
lock_version
p1
.
first_name
=
'stu'
p1
.
save!
assert_equal
1
,
p1
.
lock_version
assert_equal
0
,
p2
.
lock_version
p2
.
first_name
=
'sue'
assert_raises
(
ActiveRecord
::
StaleObjectError
)
{
p2
.
save!
}
p2
.
first_name
=
'sue2'
assert_raises
(
ActiveRecord
::
StaleObjectError
)
{
p2
.
save!
}
end
...
...
@@ -54,15 +59,18 @@ def test_lock_new
p1
=
Person
.
new
(
:first_name
=>
'anika'
)
assert_equal
0
,
p1
.
lock_version
p1
.
first_name
=
'anika2'
p1
.
save!
p2
=
Person
.
find
(
p1
.
id
)
assert_equal
0
,
p1
.
lock_version
assert_equal
0
,
p2
.
lock_version
p1
.
first_name
=
'anika3'
p1
.
save!
assert_equal
1
,
p1
.
lock_version
assert_equal
0
,
p2
.
lock_version
p2
.
first_name
=
'sue'
assert_raises
(
ActiveRecord
::
StaleObjectError
)
{
p2
.
save!
}
end
...
...
@@ -81,10 +89,12 @@ def test_lock_column_name_existing
assert_equal
0
,
t1
.
version
assert_equal
0
,
t2
.
version
t1
.
tps_report_number
=
700
t1
.
save!
assert_equal
1
,
t1
.
version
assert_equal
0
,
t2
.
version
t2
.
tps_report_number
=
800
assert_raises
(
ActiveRecord
::
StaleObjectError
)
{
t2
.
save!
}
end
...
...
@@ -93,6 +103,7 @@ def test_lock_column_is_mass_assignable
assert_equal
0
,
p1
.
lock_version
assert_equal
p1
.
lock_version
,
Person
.
new
(
p1
.
attributes
).
lock_version
p1
.
first_name
=
'bianca2'
p1
.
save!
assert_equal
1
,
p1
.
lock_version
assert_equal
p1
.
lock_version
,
Person
.
new
(
p1
.
attributes
).
lock_version
...
...
@@ -146,6 +157,15 @@ def test_quote_table_name
assert
ref
.
save
end
# Useful for partial updates, don't only update the lock_version if there
# is nothing else being updated.
def
test_update_without_attributes_does_not_only_update_lock_version
assert_nothing_raised
do
p1
=
Person
.
new
(
:first_name
=>
'anika'
)
p1
.
send
(
:update_with_lock
,
[])
end
end
private
def
add_counter_column_to
(
model
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录