Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
6889e720
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,发现更多精彩内容 >>
提交
6889e720
编写于
10月 30, 2018
作者:
E
Eileen M. Uchitelle
提交者:
GitHub
10月 30, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #34196 from gmcgibbon/connection_switch_string_name
Add support for hash and url configs to be used in connected_to
上级
f5ddf817
abf51844
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
63 addition
and
6 deletion
+63
-6
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+15
-0
activerecord/lib/active_record/connection_handling.rb
activerecord/lib/active_record/connection_handling.rb
+9
-6
activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb
.../connection_adapters/connection_handlers_multi_db_test.rb
+39
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
6889e720
*
Add support for hash and url configs in database hash of
`ActiveRecord::Base.connected_to`
.
````
User.connected_to(database: { writing: "postgres://foo" }) do
User.create!(name: "Gannon")
end
config = { "adapter" => "sqlite3", "database" => "db/readonly.sqlite3" }
User.connected_to(database: { reading: config }) do
User.count
end
````
*Gannon McGibbon*
*
Support default expression for MySQL.
*
Support default expression for MySQL.
MySQL 8.0.13 and higher supports default value to be a function or expression.
MySQL 8.0.13 and higher supports default value to be a function or expression.
...
...
activerecord/lib/active_record/connection_handling.rb
浏览文件 @
6889e720
...
@@ -109,12 +109,15 @@ def connected_to(database: nil, role: nil, &blk)
...
@@ -109,12 +109,15 @@ def connected_to(database: nil, role: nil, &blk)
if
database
&&
role
if
database
&&
role
raise
ArgumentError
,
"connected_to can only accept a database or role argument, but not both arguments."
raise
ArgumentError
,
"connected_to can only accept a database or role argument, but not both arguments."
elsif
database
elsif
database
config_hash
=
resolve_config_for_connection
(
database
)
database
=
{
database
=>
database
}
if
database
.
is_a?
(
Symbol
)
handler
=
lookup_connection_handler
(
database
.
to_sym
)
database
.
each
do
|
role
,
database_key
|
config_hash
=
resolve_config_for_connection
(
database_key
)
with_handler
(
database
.
to_sym
)
do
handler
=
lookup_connection_handler
(
role
.
to_sym
)
handler
.
establish_connection
(
config_hash
)
return
yield
with_handler
(
role
.
to_sym
)
do
handler
.
establish_connection
(
config_hash
)
return
yield
end
end
end
elsif
role
elsif
role
with_handler
(
role
.
to_sym
,
&
blk
)
with_handler
(
role
.
to_sym
,
&
blk
)
...
...
activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb
浏览文件 @
6889e720
...
@@ -120,6 +120,45 @@ def test_switching_connections_via_handler
...
@@ -120,6 +120,45 @@ def test_switching_connections_via_handler
ENV
[
"RAILS_ENV"
]
=
previous_env
ENV
[
"RAILS_ENV"
]
=
previous_env
end
end
def
test_switching_connections_with_database_url
previous_env
,
ENV
[
"RAILS_ENV"
]
=
ENV
[
"RAILS_ENV"
],
"default_env"
previous_url
,
ENV
[
"DATABASE_URL"
]
=
ENV
[
"DATABASE_URL"
],
"postgres://localhost/foo"
ActiveRecord
::
Base
.
connected_to
(
database:
{
writing:
"postgres://localhost/bar"
})
do
handler
=
ActiveRecord
::
Base
.
connection_handler
assert_equal
handler
,
ActiveRecord
::
Base
.
connection_handlers
[
:writing
]
end
ensure
ActiveRecord
::
Base
.
establish_connection
(
:arunit
)
ENV
[
"RAILS_ENV"
]
=
previous_env
ENV
[
"DATABASE_URL"
]
=
previous_url
end
def
test_switching_connections_with_database_config_hash
previous_env
,
ENV
[
"RAILS_ENV"
]
=
ENV
[
"RAILS_ENV"
],
"default_env"
config
=
{
"adapter"
=>
"sqlite3"
,
"database"
=>
"db/readonly.sqlite3"
}
ActiveRecord
::
Base
.
connected_to
(
database:
{
writing:
config
})
do
handler
=
ActiveRecord
::
Base
.
connection_handler
assert_equal
handler
,
ActiveRecord
::
Base
.
connection_handlers
[
:writing
]
end
ensure
ActiveRecord
::
Base
.
establish_connection
(
:arunit
)
ENV
[
"RAILS_ENV"
]
=
previous_env
end
def
test_switching_connections_with_database_symbol
previous_env
,
ENV
[
"RAILS_ENV"
]
=
ENV
[
"RAILS_ENV"
],
"default_env"
ActiveRecord
::
Base
.
connected_to
(
database: :arunit2
)
do
handler
=
ActiveRecord
::
Base
.
connection_handler
assert_equal
handler
,
ActiveRecord
::
Base
.
connection_handlers
[
:arunit2
]
end
ensure
ActiveRecord
::
Base
.
establish_connection
(
:arunit
)
ENV
[
"RAILS_ENV"
]
=
previous_env
end
def
test_connects_to_with_single_configuration
def
test_connects_to_with_single_configuration
config
=
{
config
=
{
"development"
=>
{
"adapter"
=>
"sqlite3"
,
"database"
=>
"db/primary.sqlite3"
},
"development"
=>
{
"adapter"
=>
"sqlite3"
,
"database"
=>
"db/primary.sqlite3"
},
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录