Skip to content

Commit 564a26d

Browse files
authored
Merge pull request rails#41859 from eileencodes/deprecate-legacy-connection-handling
Deprecate legacy_connection_handling
2 parents da795a6 + 634bf89 commit 564a26d

File tree

9 files changed

+83
-22
lines changed

9 files changed

+83
-22
lines changed

activerecord/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Deprecate `legacy_connection_handling`.
2+
3+
*Eileen M. Uchitelle*
4+
15
* Add attribute encryption support.
26

37
Encrypted attributes are declared at the model level. These

activerecord/lib/active_record/core.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,26 @@ def self.connection_handler=(handler)
216216
end
217217

218218
def self.connection_handlers
219-
unless legacy_connection_handling
219+
if legacy_connection_handling
220+
else
220221
raise NotImplementedError, "The new connection handling does not support accessing multiple connection handlers."
221222
end
222223

223224
@@connection_handlers ||= {}
224225
end
225226

226227
def self.connection_handlers=(handlers)
227-
unless legacy_connection_handling
228+
if legacy_connection_handling
229+
ActiveSupport::Deprecation.warn(<<~MSG)
230+
Using legacy connection handling is deprecated. Please set
231+
`legacy_connection_handling` to `false` in your application.
232+
233+
The new connection handling does not support `connection_handlers`
234+
getter and setter.
235+
236+
Read more about how to migrate at:
237+
MSG
238+
else
228239
raise NotImplementedError, "The new connection handling does not setting support multiple connection handlers."
229240
end
230241

activerecord/test/cases/connection_adapters/legacy_connection_handlers_multi_db_test.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ class LegacyConnectionHandlersMultiDbTest < ActiveRecord::TestCase
1313
def setup
1414
@old_value = ActiveRecord::Base.legacy_connection_handling
1515
ActiveRecord::Base.legacy_connection_handling = true
16-
ActiveRecord::Base.connection_handlers = { writing: ActiveRecord::Base.default_connection_handler }
16+
assert_deprecated do
17+
ActiveRecord::Base.connection_handlers = { writing: ActiveRecord::Base.default_connection_handler }
18+
end
1719

1820
@handlers = { writing: ConnectionHandler.new, reading: ConnectionHandler.new }
1921
@rw_handler = @handlers[:writing]
@@ -379,7 +381,9 @@ def test_retrieve_connection_pool_with_invalid_id
379381
end
380382

381383
def test_connection_handlers_are_per_thread_and_not_per_fiber
382-
ActiveRecord::Base.connection_handlers = { writing: ActiveRecord::Base.default_connection_handler, reading: ActiveRecord::ConnectionAdapters::ConnectionHandler.new }
384+
assert_deprecated do
385+
ActiveRecord::Base.connection_handlers = { writing: ActiveRecord::Base.default_connection_handler, reading: ActiveRecord::ConnectionAdapters::ConnectionHandler.new }
386+
end
383387

384388
reading_handler = ActiveRecord::Base.connection_handlers[:reading]
385389

@@ -392,7 +396,9 @@ def test_connection_handlers_are_per_thread_and_not_per_fiber
392396
end
393397

394398
def test_connection_handlers_swapping_connections_in_fiber
395-
ActiveRecord::Base.connection_handlers = { writing: ActiveRecord::Base.default_connection_handler, reading: ActiveRecord::ConnectionAdapters::ConnectionHandler.new }
399+
assert_deprecated do
400+
ActiveRecord::Base.connection_handlers = { writing: ActiveRecord::Base.default_connection_handler, reading: ActiveRecord::ConnectionAdapters::ConnectionHandler.new }
401+
end
396402

397403
reading_handler = ActiveRecord::Base.connection_handlers[:reading]
398404

activerecord/test/cases/connection_adapters/legacy_connection_handlers_sharding_db_test.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ class LegacyConnectionHandlersShardingDbTest < ActiveRecord::TestCase
1313
def setup
1414
@legacy_setting = ActiveRecord::Base.legacy_connection_handling
1515
ActiveRecord::Base.legacy_connection_handling = true
16-
ActiveRecord::Base.connection_handlers = { writing: ActiveRecord::Base.default_connection_handler }
16+
assert_deprecated do
17+
ActiveRecord::Base.connection_handlers = { writing: ActiveRecord::Base.default_connection_handler }
18+
end
1719

1820
@handlers = { writing: ConnectionHandler.new, reading: ConnectionHandler.new }
1921
@rw_handler = @handlers[:writing]

activerecord/test/cases/fixtures_test.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1523,7 +1523,9 @@ def setup
15231523

15241524
handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new
15251525
handler.establish_connection(db_config)
1526-
ActiveRecord::Base.connection_handlers = {}
1526+
assert_deprecated do
1527+
ActiveRecord::Base.connection_handlers = {}
1528+
end
15271529
ActiveRecord::Base.connection_handler = handler
15281530
ActiveRecord::Base.connects_to(database: { writing: :default, reading: :readonly })
15291531

activerecord/test/cases/helper.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,9 @@ def disable_extension!(extension, connection)
151151

152152
def clean_up_legacy_connection_handlers
153153
handler = ActiveRecord::Base.default_connection_handler
154-
ActiveRecord::Base.connection_handlers = {}
154+
assert_deprecated do
155+
ActiveRecord::Base.connection_handlers = {}
156+
end
155157

156158
handler.connection_pool_names.each do |name|
157159
next if ["ActiveRecord::Base", "ARUnit2Model", "Contact", "ContactSti"].include?(name)

activerecord/test/cases/query_cache_test.rb

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ def test_query_cache_is_applied_to_legacy_connections_in_all_handlers
7878
old_value = ActiveRecord::Base.legacy_connection_handling
7979
ActiveRecord::Base.legacy_connection_handling = true
8080

81-
ActiveRecord::Base.connection_handlers = {
82-
writing: ActiveRecord::Base.default_connection_handler,
83-
reading: ActiveRecord::ConnectionAdapters::ConnectionHandler.new
84-
}
81+
assert_deprecated do
82+
ActiveRecord::Base.connection_handlers = {
83+
writing: ActiveRecord::Base.default_connection_handler,
84+
reading: ActiveRecord::ConnectionAdapters::ConnectionHandler.new
85+
}
86+
end
8587

8688
ActiveRecord::Base.connected_to(role: :reading) do
8789
db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")
@@ -126,10 +128,13 @@ def test_query_cache_is_applied_to_all_connections
126128
def test_query_cache_with_multiple_handlers_and_forked_processes_legacy_handling
127129
old_value = ActiveRecord::Base.legacy_connection_handling
128130
ActiveRecord::Base.legacy_connection_handling = true
129-
ActiveRecord::Base.connection_handlers = {
130-
writing: ActiveRecord::Base.default_connection_handler,
131-
reading: ActiveRecord::ConnectionAdapters::ConnectionHandler.new
132-
}
131+
132+
assert_deprecated do
133+
ActiveRecord::Base.connection_handlers = {
134+
writing: ActiveRecord::Base.default_connection_handler,
135+
reading: ActiveRecord::ConnectionAdapters::ConnectionHandler.new
136+
}
137+
end
133138

134139
ActiveRecord::Base.connected_to(role: :reading) do
135140
db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")
@@ -663,10 +668,12 @@ def test_clear_query_cache_is_called_on_all_legacy_connections
663668
old_value = ActiveRecord::Base.legacy_connection_handling
664669
ActiveRecord::Base.legacy_connection_handling = true
665670

666-
ActiveRecord::Base.connection_handlers = {
667-
writing: ActiveRecord::Base.default_connection_handler,
668-
reading: ActiveRecord::ConnectionAdapters::ConnectionHandler.new
669-
}
671+
assert_deprecated do
672+
ActiveRecord::Base.connection_handlers = {
673+
writing: ActiveRecord::Base.default_connection_handler,
674+
reading: ActiveRecord::ConnectionAdapters::ConnectionHandler.new
675+
}
676+
end
670677

671678
ActiveRecord::Base.connected_to(role: :reading) do
672679
db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")

activerecord/test/cases/test_fixtures_test.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ def test_run_successfully
4848

4949
old_handler = ActiveRecord::Base.connection_handler
5050
ActiveRecord::Base.connection_handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new
51-
ActiveRecord::Base.connection_handlers = {}
51+
assert_deprecated do
52+
ActiveRecord::Base.connection_handlers = {}
53+
end
5254
ActiveRecord::Base.establish_connection(:arunit)
5355

5456
test_result = klass.new("test_run_successfully").run

guides/source/active_record_multiple_databases.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ After reading this guide you will know:
1010
* How to set up your application for multiple databases.
1111
* How automatic connection switching works.
1212
* How to use horizontal sharding for multiple databases.
13+
* How to migrate from `legacy_connection_handling` to the new connection handling.
1314
* What features are supported and what's still a work in progress.
1415

1516
--------------------------------------------------------------------------------
@@ -393,13 +394,37 @@ ActiveRecord::Base.connected_to(role: :reading, shard: :shard_one) do
393394
end
394395
```
395396

397+
## Migrate to the new connection handling
398+
399+
In Rails 6.1+, Active Record provides a new internal API for connection management.
400+
In most cases applications will not need to make any changes except to opt-in to the
401+
new behavior (if upgrading from 6.0 and below) by setting
402+
`config.active_record.legacy_connection_handling = false`. If you have a single database
403+
application, no other changes will be required. If you have a multiple database application
404+
the following changes are required if you application is using these methods:
405+
406+
* `connection_handlers` and `connection_handlers=` no longer works in the new connection
407+
handling. If you were calling a method on one of the connection handlers, for example,
408+
`connection_handlers[:reading].retrieve_connection_pool("ActiveRecord::Base")`
409+
you will now need to update that call to be
410+
`connection_handlers.retrieve_connection_pool("ActiveRecord::Base", role: :reading)`.
411+
* Calls to `ActiveRecord::Base.connection_handler.prevent_writes` will need to be updated
412+
to `ActiveRecord::Base.connection.preventing_writes?`.
413+
* If you need all the pools, including writing and reading, a new method has been provided on
414+
the handler. Call `connection_handler.all_connection_pools` to use this. In most cases though
415+
you'll want writing or reading pools with `connection_handler.connection_pool_list(:writing)` or
416+
`connection_handler.connection_pool_list(:reading)`.
417+
* If you turn off `legacy_connection_handling` in your application, any method that's unsupported
418+
will raise an error (ie `connection_handlers=`).
419+
396420
## Granular Database Connection Switching
397421

398422
In Rails 6.1 it's possible to switch connections for one database instead of
399423
all databases globally. To use this feature you must first set
400424
`config.active_record.legacy_connection_handling` to `false` in your application
401425
configuration. The majority of applications should not need to make any other
402-
changes since the public APIs have the same behavior.
426+
changes since the public APIs have the same behavior. See the above section for
427+
how to enable and migrate away from `legacy_connection_handling`.
403428

404429
With `legacy_connection_handling` set to `false`, any abstract connection class
405430
will be able to switch connections without affecting other connections. This

0 commit comments

Comments
 (0)