Skip to content

Commit ab3c1c6

Browse files
authored
Merge pull request rails#43391 from eileencodes/dont-require-role-when-passing-shrad
Don't require role when passing shard to connected_to
2 parents 3c9a76d + d71e484 commit ab3c1c6

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

activerecord/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Don't require `role` when passing `shard` to `connected_to`.
2+
3+
`connected_to` can now be called with a `shard` only. Note that `role` is still inherited if `connected_to` calls are nested.
4+
5+
*Eileen M. Uchitelle*
6+
17
* Add option to lazily load the schema cache on the connection.
28

39
Previously, the only way to load the schema cache in Active Record was through the Railtie on boot. This option provides the ability to load the schema cache on the connection after it's been established. Loading the cache lazily on the connection can be beneficial for Rails applications that use multiple databases because it will load the cache at the time the connection is established. Currently Railties doesn't have access to the connections before boot.

activerecord/lib/active_record/connection_handling.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,6 @@ def connected_to(role: nil, shard: nil, prevent_writes: false, &blk)
153153
raise ArgumentError, "must provide a `shard` and/or `role`."
154154
end
155155

156-
unless role
157-
raise ArgumentError, "`connected_to` cannot accept a `shard` argument without a `role`."
158-
end
159-
160156
with_role_and_shard(role, shard, prevent_writes, &blk)
161157
end
162158

activerecord/test/cases/connection_adapters/connection_swapping_nested_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,17 @@ def test_shards_can_be_swapped_granularly
156156
assert_equal "secondary", SecondaryBase.connection_pool.db_config.name
157157

158158
# Switch only primary to shard_one
159-
PrimaryBase.connected_to(role: global_role, shard: :shard_one) do
159+
PrimaryBase.connected_to(shard: :shard_one) do
160160
assert_equal "primary_shard_one", PrimaryBase.connection_pool.db_config.name
161161
assert_equal "secondary", SecondaryBase.connection_pool.db_config.name
162162

163163
# Switch global to shard_one
164-
ActiveRecord::Base.connected_to(role: global_role, shard: :shard_one) do
164+
ActiveRecord::Base.connected_to(shard: :shard_one) do
165165
assert_equal "primary_shard_one", PrimaryBase.connection_pool.db_config.name
166166
assert_equal "secondary_shard_one", SecondaryBase.connection_pool.db_config.name
167167

168168
# Switch only secondary to shard_two
169-
SecondaryBase.connected_to(role: global_role, shard: :shard_two) do
169+
SecondaryBase.connected_to(shard: :shard_two) do
170170
assert_equal "primary_shard_one", PrimaryBase.connection_pool.db_config.name
171171
assert_equal "secondary_shard_two", SecondaryBase.connection_pool.db_config.name
172172
end
@@ -183,7 +183,7 @@ def test_shards_can_be_swapped_granularly
183183
end
184184

185185
# Switch everything to default
186-
ActiveRecord::Base.connected_to(role: global_role, shard: :default) do
186+
ActiveRecord::Base.connected_to(shard: :default) do
187187
assert_equal "primary", PrimaryBase.connection_pool.db_config.name
188188
assert_equal "secondary", SecondaryBase.connection_pool.db_config.name
189189
end

0 commit comments

Comments
 (0)