Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
cfa5aa79
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,发现更多精彩内容 >>
未验证
提交
cfa5aa79
编写于
5月 02, 2020
作者:
R
Ryuta Kamizono
提交者:
GitHub
5月 02, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #39106 from kamipo/deprecate_passing_column_to_type_cast
Deprecate passing a column to `type_cast`
上级
7669dc21
92360e9e
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
72 addition
and
33 deletion
+72
-33
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+4
-0
activerecord/lib/active_record/connection_adapters/abstract/quoting.rb
...lib/active_record/connection_adapters/abstract/quoting.rb
+10
-13
activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb
...b/active_record/connection_adapters/postgresql/quoting.rb
+2
-2
activerecord/lib/active_record/log_subscriber.rb
activerecord/lib/active_record/log_subscriber.rb
+9
-4
activerecord/test/cases/adapter_test.rb
activerecord/test/cases/adapter_test.rb
+44
-13
activerecord/test/cases/bind_parameter_test.rb
activerecord/test/cases/bind_parameter_test.rb
+3
-1
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
cfa5aa79
*
Deprecate passing a column to
`type_cast`
.
*Ryuta Kamizono*
*
Deprecate
`in_clause_length`
and
`allowed_index_name_length`
in
`DatabaseLimits`
.
*Ryuta Kamizono*
...
...
activerecord/lib/active_record/connection_adapters/abstract/quoting.rb
浏览文件 @
cfa5aa79
...
...
@@ -21,7 +21,11 @@ def type_cast(value, column = nil)
value
=
id_value_for_database
(
value
)
if
value
.
is_a?
(
Base
)
if
column
value
=
type_cast_from_column
(
column
,
value
)
ActiveSupport
::
Deprecation
.
warn
(
<<~
MSG
.
squish
)
Passing a column to `type_cast` is deprecated and will be removed in Rails 6.2.
MSG
type
=
lookup_cast_type_from_column
(
column
)
value
=
type
.
serialize
(
value
)
end
_type_cast
(
value
)
...
...
@@ -39,16 +43,6 @@ def type_cast(value, column = nil)
# represent the type doesn't sufficiently reflect the differences
# (varchar vs binary) for example. The type used to get this primitive
# should have been provided before reaching the connection adapter.
def
type_cast_from_column
(
column
,
value
)
# :nodoc:
if
column
type
=
lookup_cast_type_from_column
(
column
)
type
.
serialize
(
value
)
else
value
end
end
# See docs for #type_cast_from_column
def
lookup_cast_type_from_column
(
column
)
# :nodoc:
lookup_cast_type
(
column
.
sql_type
)
end
...
...
@@ -193,10 +187,13 @@ def column_name_with_order_matcher # :nodoc:
private
def
type_casted_binds
(
binds
)
if
binds
.
first
.
is_a?
(
Array
)
case
binds
.
first
when
ActiveModel
::
Attribute
binds
.
map
{
|
attr
|
type_cast
(
attr
.
value_for_database
)
}
when
Array
binds
.
map
{
|
column
,
value
|
type_cast
(
value
,
column
)
}
else
binds
.
map
{
|
attr
|
type_cast
(
attr
.
value_for_databas
e
)
}
binds
.
map
{
|
value
|
type_cast
(
valu
e
)
}
end
end
...
...
activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb
浏览文件 @
cfa5aa79
...
...
@@ -67,8 +67,8 @@ def quote_default_expression(value, column) # :nodoc:
elsif
column
.
type
==
:uuid
&&
value
.
is_a?
(
String
)
&&
/\(\)/
.
match?
(
value
)
value
# Does not quote function default values for UUID columns
elsif
column
.
respond_to?
(
:array?
)
value
=
type_cast_from_column
(
column
,
value
)
quote
(
value
)
type
=
lookup_cast_type_from_column
(
column
)
quote
(
type
.
serialize
(
value
)
)
else
super
end
...
...
activerecord/lib/active_record/log_subscriber.rb
浏览文件 @
cfa5aa79
...
...
@@ -55,13 +55,18 @@ def type_casted_binds(casted_binds)
end
def
render_bind
(
attr
,
value
)
if
attr
.
is_a?
(
Array
)
case
attr
when
ActiveModel
::
Attribute
if
attr
.
type
.
binary?
&&
attr
.
value
value
=
"<
#{
attr
.
value_for_database
.
to_s
.
bytesize
}
bytes of binary data>"
end
when
Array
attr
=
attr
.
first
els
if
attr
.
type
.
binary?
&&
attr
.
valu
e
value
=
"<
#{
attr
.
value_for_database
.
to_s
.
bytesize
}
bytes of binary data>"
else
attr
=
nil
end
[
attr
&&
attr
.
name
,
value
]
[
attr
&
.
name
,
value
]
end
def
colorize_payload_name
(
name
,
payload_name
)
...
...
activerecord/test/cases/adapter_test.rb
浏览文件 @
cfa5aa79
...
...
@@ -312,41 +312,72 @@ def test_select_all_always_return_activerecord_result
end
if
ActiveRecord
::
Base
.
connection
.
prepared_statements
def
test_select_all_with_legacy_binds
post
=
Post
.
create!
(
title:
"foo"
,
body:
"bar"
)
expected
=
@connection
.
select_all
(
"SELECT * FROM posts WHERE id =
#{
post
.
id
}
"
)
result
=
@connection
.
select_all
(
"SELECT * FROM posts WHERE id =
#{
Arel
::
Nodes
::
BindParam
.
new
(
nil
).
to_sql
}
"
,
nil
,
[[
nil
,
post
.
id
]])
assert_equal
expected
.
to_a
,
result
.
to_a
def
test_select_all_insert_update_delete_with_legacy_binds
binds
=
[[
Event
.
column_for_attribute
(
"id"
),
1
]]
bind_param
=
Arel
::
Nodes
::
BindParam
.
new
(
nil
)
assert_deprecated
do
id
=
@connection
.
insert
(
"INSERT INTO events(id) VALUES (
#{
bind_param
.
to_sql
}
)"
,
nil
,
nil
,
nil
,
nil
,
binds
)
assert_equal
1
,
id
end
assert_deprecated
do
updated
=
@connection
.
update
(
"UPDATE events SET title = 'foo' WHERE id =
#{
bind_param
.
to_sql
}
"
,
nil
,
binds
)
assert_equal
1
,
updated
end
assert_deprecated
do
result
=
@connection
.
select_all
(
"SELECT * FROM events WHERE id =
#{
bind_param
.
to_sql
}
"
,
nil
,
binds
)
assert_equal
({
"id"
=>
1
,
"title"
=>
"foo"
},
result
.
first
)
end
assert_deprecated
do
deleted
=
@connection
.
delete
(
"DELETE FROM events WHERE id =
#{
bind_param
.
to_sql
}
"
,
nil
,
binds
)
assert_equal
1
,
deleted
end
assert_deprecated
do
result
=
@connection
.
select_all
(
"SELECT * FROM events WHERE id =
#{
bind_param
.
to_sql
}
"
,
nil
,
binds
)
assert_nil
result
.
first
end
end
def
test_
insert_update_delete_with_legacy
_binds
binds
=
[
[
nil
,
1
]
]
def
test_
select_all_insert_update_delete_with_casted
_binds
binds
=
[
Event
.
type_for_attribute
(
"id"
).
serialize
(
1
)
]
bind_param
=
Arel
::
Nodes
::
BindParam
.
new
(
nil
)
id
=
@connection
.
insert
(
"INSERT INTO events(id) VALUES (
#{
bind_param
.
to_sql
}
)"
,
nil
,
nil
,
nil
,
nil
,
binds
)
assert_equal
1
,
id
@connection
.
update
(
"UPDATE events SET title = 'foo' WHERE id =
#{
bind_param
.
to_sql
}
"
,
nil
,
binds
)
updated
=
@connection
.
update
(
"UPDATE events SET title = 'foo' WHERE id =
#{
bind_param
.
to_sql
}
"
,
nil
,
binds
)
assert_equal
1
,
updated
result
=
@connection
.
select_all
(
"SELECT * FROM events WHERE id =
#{
bind_param
.
to_sql
}
"
,
nil
,
binds
)
assert_equal
({
"id"
=>
1
,
"title"
=>
"foo"
},
result
.
first
)
@connection
.
delete
(
"DELETE FROM events WHERE id =
#{
bind_param
.
to_sql
}
"
,
nil
,
binds
)
deleted
=
@connection
.
delete
(
"DELETE FROM events WHERE id =
#{
bind_param
.
to_sql
}
"
,
nil
,
binds
)
assert_equal
1
,
deleted
result
=
@connection
.
select_all
(
"SELECT * FROM events WHERE id =
#{
bind_param
.
to_sql
}
"
,
nil
,
binds
)
assert_nil
result
.
first
end
def
test_insert_update_delete_with_binds
binds
=
[
Relation
::
QueryAttribute
.
new
(
"id"
,
1
,
Type
.
default_value
)]
def
test_
select_all_
insert_update_delete_with_binds
binds
=
[
Relation
::
QueryAttribute
.
new
(
"id"
,
1
,
Event
.
type_for_attribute
(
"id"
)
)]
bind_param
=
Arel
::
Nodes
::
BindParam
.
new
(
nil
)
id
=
@connection
.
insert
(
"INSERT INTO events(id) VALUES (
#{
bind_param
.
to_sql
}
)"
,
nil
,
nil
,
nil
,
nil
,
binds
)
assert_equal
1
,
id
@connection
.
update
(
"UPDATE events SET title = 'foo' WHERE id =
#{
bind_param
.
to_sql
}
"
,
nil
,
binds
)
updated
=
@connection
.
update
(
"UPDATE events SET title = 'foo' WHERE id =
#{
bind_param
.
to_sql
}
"
,
nil
,
binds
)
assert_equal
1
,
updated
result
=
@connection
.
select_all
(
"SELECT * FROM events WHERE id =
#{
bind_param
.
to_sql
}
"
,
nil
,
binds
)
assert_equal
({
"id"
=>
1
,
"title"
=>
"foo"
},
result
.
first
)
@connection
.
delete
(
"DELETE FROM events WHERE id =
#{
bind_param
.
to_sql
}
"
,
nil
,
binds
)
deleted
=
@connection
.
delete
(
"DELETE FROM events WHERE id =
#{
bind_param
.
to_sql
}
"
,
nil
,
binds
)
assert_equal
1
,
deleted
result
=
@connection
.
select_all
(
"SELECT * FROM events WHERE id =
#{
bind_param
.
to_sql
}
"
,
nil
,
binds
)
assert_nil
result
.
first
end
...
...
activerecord/test/cases/bind_parameter_test.rb
浏览文件 @
cfa5aa79
...
...
@@ -159,7 +159,9 @@ def test_logs_binds_after_type_cast
def
test_logs_legacy_binds_after_type_cast
binds
=
[[
@pk
,
"10"
]]
assert_logs_binds
(
binds
)
assert_deprecated
do
assert_logs_binds
(
binds
)
end
end
private
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录