Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
05a6cd55
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,发现更多精彩内容 >>
未验证
提交
05a6cd55
编写于
12月 03, 2019
作者:
E
Eileen M. Uchitelle
提交者:
GitHub
12月 03, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #37873 from eileencodes/fix-bug-in-configs-for
Fix bug in configs_for
上级
cf27cfa1
2791b7c2
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
23 addition
and
9 deletion
+23
-9
activerecord/lib/active_record/database_configurations.rb
activerecord/lib/active_record/database_configurations.rb
+12
-9
activerecord/test/cases/database_configurations_test.rb
activerecord/test/cases/database_configurations_test.rb
+11
-0
未找到文件。
activerecord/lib/active_record/database_configurations.rb
浏览文件 @
05a6cd55
...
...
@@ -31,12 +31,14 @@ def initialize(configurations = {})
# * <tt>env_name:</tt> The environment name. Defaults to +nil+ which will collect
# configs for all environments.
# * <tt>spec_name:</tt> The specification name (i.e. primary, animals, etc.). Defaults
# to +nil+.
# to +nil+. If no +env_name+ is specified the config for the default env and the
# passed +spec_name+ will be returned.
# * <tt>include_replicas:</tt> Determines whether to include replicas in
# the returned list. Most of the time we're only iterating over the write
# connection (i.e. migrations don't need to run for the write and read connection).
# Defaults to +false+.
def
configs_for
(
env_name:
nil
,
spec_name:
nil
,
include_replicas:
false
)
env_name
||=
default_env
if
spec_name
configs
=
env_with_configs
(
env_name
)
unless
include_replicas
...
...
@@ -60,7 +62,7 @@ def configs_for(env_name: nil, spec_name: nil, include_replicas: false)
# return the first config hash for the environment.
#
# { database: "my_db", adapter: "mysql2" }
def
default_hash
(
env
=
ActiveRecord
::
ConnectionHandling
::
DEFAULT_ENV
.
call
.
to_s
)
def
default_hash
(
env
=
default_env
)
default
=
find_db_config
(
env
)
default
.
configuration_hash
if
default
end
...
...
@@ -132,14 +134,17 @@ def resolve(config, pool_name = nil) # :nodoc:
when
Symbol
resolve_symbol_connection
(
config
,
pool_name
)
when
Hash
,
String
env
=
ActiveRecord
::
ConnectionHandling
::
DEFAULT_ENV
.
call
.
to_s
build_db_config_from_raw_config
(
env
,
"primary"
,
config
)
build_db_config_from_raw_config
(
default_env
,
"primary"
,
config
)
else
raise
TypeError
,
"Invalid type for configuration. Expected Symbol, String, or Hash. Got
#{
config
.
inspect
}
"
end
end
private
def
default_env
ActiveRecord
::
ConnectionHandling
::
DEFAULT_ENV
.
call
.
to_s
end
def
env_with_configs
(
env
=
nil
)
if
env
configurations
.
select
{
|
db_config
|
db_config
.
env_name
==
env
}
...
...
@@ -160,13 +165,11 @@ def build_configs(configs)
end
end
current_env
=
ActiveRecord
::
ConnectionHandling
::
DEFAULT_ENV
.
call
.
to_s
unless
db_configs
.
find
(
&
:for_current_env?
)
db_configs
<<
environment_url_config
(
curren
t_env
,
"primary"
,
{})
db_configs
<<
environment_url_config
(
defaul
t_env
,
"primary"
,
{})
end
merge_db_environment_variables
(
curren
t_env
,
db_configs
.
compact
)
merge_db_environment_variables
(
defaul
t_env
,
db_configs
.
compact
)
end
def
walk_configs
(
env_name
,
config
)
...
...
@@ -183,7 +186,7 @@ def resolve_symbol_connection(env_name, pool_name)
DatabaseConfigurations
::
HashConfig
.
new
(
db_config
.
env_name
,
db_config
.
spec_name
,
config
)
else
raise
AdapterNotSpecified
,
<<~
MSG
The `
#{
env_name
}
` database is not configured for the `
#{
ActiveRecord
::
ConnectionHandling
::
DEFAULT_ENV
.
call
}
` environment.
The `
#{
env_name
}
` database is not configured for the `
#{
default_env
}
` environment.
Available databases configurations are:
...
...
activerecord/test/cases/database_configurations_test.rb
浏览文件 @
05a6cd55
...
...
@@ -25,6 +25,17 @@ def test_configs_for_getter_with_env_name
assert_equal
[
"arunit"
],
configs
.
map
(
&
:env_name
)
end
def
test_configs_for_getter_with_spec_name
previous_env
,
ENV
[
"RAILS_ENV"
]
=
ENV
[
"RAILS_ENV"
],
"arunit2"
config
=
ActiveRecord
::
Base
.
configurations
.
configs_for
(
spec_name:
"primary"
)
assert_equal
"arunit2"
,
config
.
env_name
assert_equal
"primary"
,
config
.
spec_name
ensure
ENV
[
"RAILS_ENV"
]
=
previous_env
end
def
test_configs_for_getter_with_env_and_spec_name
config
=
ActiveRecord
::
Base
.
configurations
.
configs_for
(
env_name:
"arunit"
,
spec_name:
"primary"
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录