Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
e30d701a
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,发现更多精彩内容 >>
提交
e30d701a
编写于
6月 17, 2014
作者:
S
Sean Griffin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Don't mess with `column_defaults` when optimistic locking is enabled
上级
e7a8fda0
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
32 addition
and
12 deletion
+32
-12
activerecord/lib/active_record/base.rb
activerecord/lib/active_record/base.rb
+2
-2
activerecord/lib/active_record/locking/optimistic.rb
activerecord/lib/active_record/locking/optimistic.rb
+23
-10
activerecord/test/cases/locking_test.rb
activerecord/test/cases/locking_test.rb
+7
-0
未找到文件。
activerecord/lib/active_record/base.rb
浏览文件 @
e30d701a
...
...
@@ -308,10 +308,10 @@ class Base
include
Integration
include
Validations
include
CounterCache
include
Locking
::
Optimistic
include
Locking
::
Pessimistic
include
Attributes
include
AttributeDecorators
include
Locking
::
Optimistic
include
Locking
::
Pessimistic
include
AttributeMethods
include
Callbacks
include
Timestamp
...
...
activerecord/lib/active_record/locking/optimistic.rb
浏览文件 @
e30d701a
...
...
@@ -53,6 +53,11 @@ module Optimistic
included
do
class_attribute
:lock_optimistically
,
instance_writer:
false
self
.
lock_optimistically
=
true
is_lock_column
=
->
(
name
,
_
)
{
lock_optimistically
&&
name
==
locking_column
}
decorate_matching_attribute_types
(
is_lock_column
,
:_optimistic_locking
)
do
|
type
|
LockingType
.
new
(
type
)
end
end
def
locking_enabled?
#:nodoc:
...
...
@@ -141,7 +146,7 @@ def locking_enabled?
# Set the column to use for optimistic locking. Defaults to +lock_version+.
def
locking_column
=
(
value
)
@column_defaults
=
nil
clear_caches_calculated_from_columns
@locking_column
=
value
.
to_s
end
...
...
@@ -162,18 +167,26 @@ def update_counters(id, counters)
counters
=
counters
.
merge
(
locking_column
=>
1
)
if
locking_enabled?
super
end
end
end
def
column_defaults
@column_defaults
||=
begin
defaults
=
super
class
LockingType
<
SimpleDelegator
def
type_cast_from_database
(
value
)
# `nil` *should* be changed to 0
super
.
to_i
end
if
defaults
.
key?
(
locking_column
)
&&
lock_optimistically
defaults
[
locking_column
]
||=
0
end
def
changed?
(
old_value
,
*
)
# Ensure we save if the default was `nil`
super
||
old_value
==
0
end
defaults
end
end
def
init_with
(
coder
)
__setobj__
(
coder
[
'subtype'
])
end
def
encode_with
(
coder
)
coder
[
'subtype'
]
=
__getobj__
end
end
end
...
...
activerecord/test/cases/locking_test.rb
浏览文件 @
e30d701a
...
...
@@ -272,6 +272,13 @@ def test_removing_has_and_belongs_to_many_associations_upon_destroy
assert
p
.
treasures
.
empty?
assert
RichPerson
.
connection
.
select_all
(
"SELECT * FROM peoples_treasures WHERE rich_person_id = 1"
).
empty?
end
def
test_yaml_dumping_with_lock_column
t1
=
LockWithoutDefault
.
new
t2
=
YAML
.
load
(
YAML
.
dump
(
t1
))
assert_equal
t1
.
attributes
,
t2
.
attributes
end
end
class
OptimisticLockingWithSchemaChangeTest
<
ActiveRecord
::
TestCase
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录