Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
b2556fd8
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,发现更多精彩内容 >>
提交
b2556fd8
编写于
5月 26, 2017
作者:
M
Matthew Draper
提交者:
GitHub
5月 26, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #29216 from matthewd/threadsafe-load-schema
Add a Monitor to ModelSchema#load_schema
上级
aee5ef3d
6a5a814e
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
56 addition
and
3 deletion
+56
-3
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+6
-0
activerecord/lib/active_record/model_schema.rb
activerecord/lib/active_record/model_schema.rb
+22
-3
activerecord/test/cases/serialized_attribute_test.rb
activerecord/test/cases/serialized_attribute_test.rb
+28
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
b2556fd8
*
Loading model schema from database is now thread-safe.
Fixes #28589.
*Vikrant Chaudhary*, *David Abdemoulaie*
*
Add
`ActiveRecord::Base#cache_version`
to support recyclable cache keys via the new versioned entries
in
`ActiveSupport::Cache`
. This also means that
`ActiveRecord::Base#cache_key`
will now return a stable key
that does not include a timestamp any more.
...
...
activerecord/lib/active_record/model_schema.rb
浏览文件 @
b2556fd8
require
"monitor"
module
ActiveRecord
module
ModelSchema
extend
ActiveSupport
::
Concern
...
...
@@ -152,6 +154,8 @@ module ModelSchema
self
.
inheritance_column
=
"type"
delegate
:type_for_attribute
,
to: :class
initialize_load_schema_monitor
end
# Derives the join table name for +first_table+ and +second_table+. The
...
...
@@ -435,15 +439,27 @@ def reset_column_information
initialize_find_by_cache
end
protected
def
initialize_load_schema_monitor
@load_schema_monitor
=
Monitor
.
new
end
private
def
inherited
(
child_class
)
super
child_class
.
initialize_load_schema_monitor
end
def
schema_loaded?
defined?
(
@
columns_hash
)
&&
@columns_hash
defined?
(
@
schema_loaded
)
&&
@schema_loaded
end
def
load_schema
unless
schema_loaded?
load_schema!
return
if
schema_loaded?
@load_schema_monitor
.
synchronize
do
load_schema!
unless
defined?
(
@columns_hash
)
&&
@columns_hash
end
end
...
...
@@ -457,6 +473,8 @@ def load_schema!
user_provided_default:
false
)
end
@schema_loaded
=
true
end
def
reload_schema_from_cache
...
...
@@ -470,6 +488,7 @@ def reload_schema_from_cache
@attributes_builder
=
nil
@columns
=
nil
@columns_hash
=
nil
@schema_loaded
=
false
@attribute_names
=
nil
@yaml_encoder
=
nil
direct_descendants
.
each
do
|
descendant
|
...
...
activerecord/test/cases/serialized_attribute_test.rb
浏览文件 @
b2556fd8
...
...
@@ -349,4 +349,32 @@ def deserialize(value)
topic
.
foo
refute
topic
.
changed?
end
def
test_serialized_attribute_works_under_concurrent_initial_access
model
=
Topic
.
dup
topic
=
model
.
last
topic
.
update
group:
"1"
model
.
serialize
:group
,
JSON
model
.
reset_column_information
# This isn't strictly necessary for the test, but a little bit of
# knowledge of internals allows us to make failures far more likely.
model
.
define_singleton_method
(
:define_attribute
)
do
|*
args
|
Thread
.
pass
super
(
*
args
)
end
threads
=
4
.
times
.
map
do
Thread
.
new
do
topic
.
reload
.
group
end
end
# All the threads should retrieve the value knowing it is JSON, and
# thus decode it. If this fails, some threads will instead see the
# raw string ("1"), or raise an exception.
assert_equal
[
1
]
*
threads
.
size
,
threads
.
map
(
&
:value
)
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录