Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitabf5184

Browse files
committed
Add support for hash and url configs in connected_to
Add support for hash and url configs in database hashof `ActiveRecord::Base.connected_to`.
1 parent5431e17 commitabf5184

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

‎activerecord/CHANGELOG.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
* Add support for hash and url configs in database hash of`ActiveRecord::Base.connected_to`.
2+
3+
````
4+
User.connected_to(database: { writing: "postgres://foo" }) do
5+
User.create!(name: "Gannon")
6+
end
7+
8+
config = { "adapter" => "sqlite3", "database" => "db/readonly.sqlite3" }
9+
User.connected_to(database: { reading: config }) do
10+
User.count
11+
end
12+
````
13+
14+
*Gannon McGibbon*
15+
116
* Support default expression for MySQL.
217
318
MySQL 8.0.13 and higher supports default value to be a function or expression.

‎activerecord/lib/active_record/connection_handling.rb‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,15 @@ def connected_to(database: nil, role: nil, &blk)
109109
ifdatabase &&role
110110
raiseArgumentError,"connected_to can only accept a database or role argument, but not both arguments."
111111
elsifdatabase
112-
config_hash=resolve_config_for_connection(database)
113-
handler=lookup_connection_handler(database.to_sym)
114-
115-
with_handler(database.to_sym)do
116-
handler.establish_connection(config_hash)
117-
returnyield
112+
database={database=>database}ifdatabase.is_a?(Symbol)
113+
database.eachdo |role,database_key|
114+
config_hash=resolve_config_for_connection(database_key)
115+
handler=lookup_connection_handler(role.to_sym)
116+
117+
with_handler(role.to_sym)do
118+
handler.establish_connection(config_hash)
119+
returnyield
120+
end
118121
end
119122
elsifrole
120123
with_handler(role.to_sym, &blk)

‎activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,45 @@ def test_switching_connections_via_handler
120120
ENV["RAILS_ENV"]=previous_env
121121
end
122122

123+
deftest_switching_connections_with_database_url
124+
previous_env,ENV["RAILS_ENV"]=ENV["RAILS_ENV"],"default_env"
125+
previous_url,ENV["DATABASE_URL"]=ENV["DATABASE_URL"],"postgres://localhost/foo"
126+
127+
ActiveRecord::Base.connected_to(database:{writing:"postgres://localhost/bar"})do
128+
handler=ActiveRecord::Base.connection_handler
129+
assert_equalhandler,ActiveRecord::Base.connection_handlers[:writing]
130+
end
131+
ensure
132+
ActiveRecord::Base.establish_connection(:arunit)
133+
ENV["RAILS_ENV"]=previous_env
134+
ENV["DATABASE_URL"]=previous_url
135+
end
136+
137+
deftest_switching_connections_with_database_config_hash
138+
previous_env,ENV["RAILS_ENV"]=ENV["RAILS_ENV"],"default_env"
139+
config={"adapter"=>"sqlite3","database"=>"db/readonly.sqlite3"}
140+
141+
ActiveRecord::Base.connected_to(database:{writing:config})do
142+
handler=ActiveRecord::Base.connection_handler
143+
assert_equalhandler,ActiveRecord::Base.connection_handlers[:writing]
144+
end
145+
ensure
146+
ActiveRecord::Base.establish_connection(:arunit)
147+
ENV["RAILS_ENV"]=previous_env
148+
end
149+
150+
deftest_switching_connections_with_database_symbol
151+
previous_env,ENV["RAILS_ENV"]=ENV["RAILS_ENV"],"default_env"
152+
153+
ActiveRecord::Base.connected_to(database::arunit2)do
154+
handler=ActiveRecord::Base.connection_handler
155+
assert_equalhandler,ActiveRecord::Base.connection_handlers[:arunit2]
156+
end
157+
ensure
158+
ActiveRecord::Base.establish_connection(:arunit)
159+
ENV["RAILS_ENV"]=previous_env
160+
end
161+
123162
deftest_connects_to_with_single_configuration
124163
config={
125164
"development"=>{"adapter"=>"sqlite3","database"=>"db/primary.sqlite3"},

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp