Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
3fafc1cc
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,发现更多精彩内容 >>
提交
3fafc1cc
编写于
4月 03, 2014
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #14570 from matthewd/uri_deprecation_warning
Avoid a spurious deprecation warning for database URLs
上级
7531404a
ad141538
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
76 addition
and
24 deletion
+76
-24
activerecord/lib/active_record/connection_adapters/connection_specification.rb
...ve_record/connection_adapters/connection_specification.rb
+28
-24
activerecord/test/cases/connection_adapters/connection_handler_test.rb
...test/cases/connection_adapters/connection_handler_test.rb
+48
-0
未找到文件。
activerecord/lib/active_record/connection_adapters/connection_specification.rb
浏览文件 @
3fafc1cc
...
@@ -124,7 +124,7 @@ def resolve(config)
...
@@ -124,7 +124,7 @@ def resolve(config)
if
config
if
config
resolve_connection
config
resolve_connection
config
elsif
env
=
ActiveRecord
::
ConnectionHandling
::
RAILS_ENV
.
call
elsif
env
=
ActiveRecord
::
ConnectionHandling
::
RAILS_ENV
.
call
resolve_
env
_connection
env
.
to_sym
resolve_
symbol
_connection
env
.
to_sym
else
else
raise
AdapterNotSpecified
raise
AdapterNotSpecified
end
end
...
@@ -193,42 +193,41 @@ def spec(config)
...
@@ -193,42 +193,41 @@ def spec(config)
#
#
def
resolve_connection
(
spec
)
def
resolve_connection
(
spec
)
case
spec
case
spec
when
Symbol
,
String
when
Symbol
resolve_env_connection
spec
resolve_symbol_connection
spec
when
String
resolve_string_connection
spec
when
Hash
when
Hash
resolve_hash_connection
spec
resolve_hash_connection
spec
end
end
end
end
def
resolve_string_connection
(
spec
)
# Rails has historically accepted a string to mean either
# an environment key or a URL spec, so we have deprecated
# this ambiguous behaviour and in the future this function
# can be removed in favor of resolve_url_connection.
if
configurations
.
key?
(
spec
)
ActiveSupport
::
Deprecation
.
warn
"Passing a string to ActiveRecord::Base.establish_connection "
\
"for a configuration lookup is deprecated, please pass a symbol (
#{
spec
.
to_sym
.
inspect
}
) instead"
resolve_connection
(
configurations
[
spec
])
else
resolve_url_connection
(
spec
)
end
end
# Takes the environment such as `:production` or `:development`.
# Takes the environment such as `:production` or `:development`.
# This requires that the @configurations was initialized with a key that
# This requires that the @configurations was initialized with a key that
# matches.
# matches.
#
#
#
# Resolver.new("production" => {}).resolve_symbol_connection(:production)
# Resolver.new("production" => {}).resolve_env_connection(:production)
# # => {}
# # => {}
#
#
# Takes a connection URL.
def
resolve_symbol_connection
(
spec
)
#
# Resolver.new({}).resolve_env_connection("postgresql://localhost/foo")
# # => { "host" => "localhost", "database" => "foo", "adapter" => "postgresql" }
#
def
resolve_env_connection
(
spec
)
# Rails has historically accepted a string to mean either
# an environment key or a URL spec, so we have deprecated
# this ambiguous behaviour and in the future this function
# can be removed in favor of resolve_string_connection and
# resolve_symbol_connection.
if
config
=
configurations
[
spec
.
to_s
]
if
config
=
configurations
[
spec
.
to_s
]
if
spec
.
is_a?
(
String
)
ActiveSupport
::
Deprecation
.
warn
"Passing a string to ActiveRecord::Base.establish_connection "
\
"for a configuration lookup is deprecated, please pass a symbol (
#{
spec
.
to_sym
.
inspect
}
) instead"
end
resolve_connection
(
config
)
resolve_connection
(
config
)
elsif
spec
.
is_a?
(
String
)
resolve_string_connection
(
spec
)
else
else
raise
(
AdapterNotSpecified
,
"'
#{
spec
}
' database is not configured. Available
configuration:
#{
configuration
s
.
inspect
}
"
)
raise
(
AdapterNotSpecified
,
"'
#{
spec
}
' database is not configured. Available
:
#{
configurations
.
key
s
.
inspect
}
"
)
end
end
end
end
...
@@ -244,7 +243,12 @@ def resolve_hash_connection(spec)
...
@@ -244,7 +243,12 @@ def resolve_hash_connection(spec)
spec
spec
end
end
def
resolve_string_connection
(
url
)
# Takes a connection URL.
#
# Resolver.new({}).resolve_url_connection("postgresql://localhost/foo")
# # => { "host" => "localhost", "database" => "foo", "adapter" => "postgresql" }
#
def
resolve_url_connection
(
url
)
ConnectionUrlResolver
.
new
(
url
).
to_hash
ConnectionUrlResolver
.
new
(
url
).
to_hash
end
end
end
end
...
...
activerecord/test/cases/connection_adapters/connection_handler_test.rb
浏览文件 @
3fafc1cc
...
@@ -17,6 +17,54 @@ def setup
...
@@ -17,6 +17,54 @@ def setup
ENV
[
"DATABASE_URL"
]
=
@previous_database_url
ENV
[
"DATABASE_URL"
]
=
@previous_database_url
end
end
def
resolve
(
spec
,
config
)
ConnectionSpecification
::
Resolver
.
new
(
klass
.
new
(
config
).
resolve
).
resolve
(
spec
)
end
def
spec
(
spec
,
config
)
ConnectionSpecification
::
Resolver
.
new
(
klass
.
new
(
config
).
resolve
).
spec
(
spec
)
end
def
test_resolver_with_database_uri_and_known_key
ENV
[
'DATABASE_URL'
]
=
"postgres://localhost/foo"
config
=
{
"production"
=>
{
"adapter"
=>
"not_postgres"
,
"database"
=>
"not_foo"
}
}
actual
=
resolve
(
:production
,
config
)
expected
=
{
"adapter"
=>
"postgresql"
,
"database"
=>
"foo"
,
"host"
=>
"localhost"
}
assert_equal
expected
,
actual
end
def
test_resolver_with_database_uri_and_known_string_key
ENV
[
'DATABASE_URL'
]
=
"postgres://localhost/foo"
config
=
{
"production"
=>
{
"adapter"
=>
"not_postgres"
,
"database"
=>
"not_foo"
}
}
actual
=
assert_deprecated
{
resolve
(
"production"
,
config
)
}
expected
=
{
"adapter"
=>
"postgresql"
,
"database"
=>
"foo"
,
"host"
=>
"localhost"
}
assert_equal
expected
,
actual
end
def
test_resolver_with_database_uri_and_unknown_symbol_key
ENV
[
'DATABASE_URL'
]
=
"postgres://localhost/foo"
config
=
{
"not_production"
=>
{
"adapter"
=>
"not_postgres"
,
"database"
=>
"not_foo"
}
}
actual
=
resolve
(
:production
,
config
)
expected
=
{
"adapter"
=>
"postgresql"
,
"database"
=>
"foo"
,
"host"
=>
"localhost"
}
assert_equal
expected
,
actual
end
def
test_resolver_with_database_uri_and_unknown_string_key
ENV
[
'DATABASE_URL'
]
=
"postgres://localhost/foo"
config
=
{
"not_production"
=>
{
"adapter"
=>
"not_postgres"
,
"database"
=>
"not_foo"
}
}
assert_raises
AdapterNotSpecified
do
spec
(
"production"
,
config
)
end
end
def
test_resolver_with_database_uri_and_supplied_url
ENV
[
'DATABASE_URL'
]
=
"not-postgres://not-localhost/not_foo"
config
=
{
"production"
=>
{
"adapter"
=>
"also_not_postgres"
,
"database"
=>
"also_not_foo"
}
}
actual
=
resolve
(
"postgres://localhost/foo"
,
config
)
expected
=
{
"adapter"
=>
"postgresql"
,
"database"
=>
"foo"
,
"host"
=>
"localhost"
}
assert_equal
expected
,
actual
end
def
test_jdbc_url
def
test_jdbc_url
config
=
{
"production"
=>
{
"url"
=>
"jdbc:postgres://localhost/foo"
}
}
config
=
{
"production"
=>
{
"url"
=>
"jdbc:postgres://localhost/foo"
}
}
actual
=
klass
.
new
(
config
).
resolve
actual
=
klass
.
new
(
config
).
resolve
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录