Skip to content

Commit 5d0e443

Browse files
authored
Merge pull request rails#47536 from eileencodes/allow-configs_for-to-accept-custom-hash-key-for-lookup
Allow configs_for to accept a custom hash key
2 parents b8a399c + 232aed8 commit 5d0e443

File tree

4 files changed

+50
-24
lines changed

4 files changed

+50
-24
lines changed

activerecord/CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
* Remove the `:include_replicas` argument from `configs_for`. Use `:include_hidden` argument instead.
2+
3+
*Eileen M. Uchitelle*
4+
5+
* Allow applications to lookup a config via a custom hash key.
6+
7+
If you have registered a custom config or want to find configs where the hash matches a specific key, now you can pass `config_key` to `configs_for`. For example if you have a `db_config` with the key `vitess` you can look up a database configuration hash by matching that key.
8+
9+
```ruby
10+
ActiveRecord::Base.configurations.configs_for(env_name: "development", name: "primary", config_key: :vitess)
11+
ActiveRecord::Base.configurations.configs_for(env_name: "development", config_key: :vitess)
12+
```
13+
14+
*Eileen M. Uchitelle*
15+
116
* Allow applications to register a custom database configuration handler.
217

318
Adds a mechanism for registering a custom handler for cases where you want database configurations to respond to custom methods. This is useful for non-Rails database adapters or tools like Vitess that you may want to configure differently from a standard `HashConfig` or `UrlConfig`.

activerecord/lib/active_record/database_configurations.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,14 @@ def initialize(configurations = {})
8282
# * <tt>name:</tt> The db config name (i.e. primary, animals, etc.). Defaults
8383
# to +nil+. If no +env_name+ is specified the config for the default env and the
8484
# passed +name+ will be returned.
85-
# * <tt>include_replicas:</tt> Deprecated. Determines whether to include replicas in
86-
# the returned list. Most of the time we're only iterating over the write
87-
# connection (i.e. migrations don't need to run for the write and read connection).
88-
# Defaults to +false+.
85+
# * <tt>config_key:</tt> Selects configs that contain a particular key in the configuration
86+
# hash. Useful for selecting configs that use a custom db config handler or finding
87+
# configs with hashes that contain a particular key.
8988
# * <tt>include_hidden:</tt> Determines whether to include replicas and configurations
9089
# hidden by +database_tasks: false+ in the returned list. Most of the time we're only
9190
# iterating over the primary connections (i.e. migrations don't need to run for the
9291
# write and read connection). Defaults to +false+.
93-
def configs_for(env_name: nil, name: nil, include_replicas: false, include_hidden: false)
94-
if include_replicas
95-
include_hidden = include_replicas
96-
ActiveRecord.deprecator.warn("The kwarg `include_replicas` is deprecated in favor of `include_hidden`. When `include_hidden` is passed, configurations with `replica: true` or `database_tasks: false` will be returned. `include_replicas` will be removed in Rails 7.1.")
97-
end
98-
92+
def configs_for(env_name: nil, name: nil, config_key: nil, include_hidden: false)
9993
env_name ||= default_env if name
10094
configs = env_with_configs(env_name)
10195

@@ -105,6 +99,12 @@ def configs_for(env_name: nil, name: nil, include_replicas: false, include_hidde
10599
end
106100
end
107101

102+
if config_key
103+
configs = configs.select do |db_config|
104+
db_config.configuration_hash.key?(config_key)
105+
end
106+
end
107+
108108
if name
109109
configs.find do |db_config|
110110
db_config.name == name

activerecord/test/cases/database_configurations_test.rb

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,33 @@ def test_registering_a_custom_config_object
128128
ensure
129129
ActiveRecord::DatabaseConfigurations.db_config_handlers = previous_handlers
130130
end
131-
end
132131

133-
class LegacyDatabaseConfigurationsTest < ActiveRecord::TestCase
134-
def test_unsupported_method_raises
135-
assert_raises NoMethodError do
136-
ActiveRecord::Base.configurations.fetch(:foo)
132+
def test_configs_for_with_custom_key
133+
previous_handlers = ActiveRecord::DatabaseConfigurations.db_config_handlers
134+
135+
ActiveRecord::DatabaseConfigurations.register_db_config_handler do |env_name, name, _, config|
136+
next unless config.key?(:custom_config)
137+
CustomHashConfig.new(env_name, name, config)
137138
end
139+
140+
config = {
141+
"default_env" => {
142+
"primary" => { "adapter" => "sqlite3", "database" => "test/db/primary.sqlite3", "custom_config" => { "sharded" => 1 } },
143+
"replica" => { "adapter" => "sqlite3", "database" => "test/db/hidden.sqlite3", "replica" => true, "custom_config" => { "sharded" => 1 } },
144+
"secondary" => { "adapter" => "sqlite3", "database" => "test/db/secondary.sqlite3" }
145+
}
146+
}
147+
prev_configs, ActiveRecord::Base.configurations = ActiveRecord::Base.configurations, config
148+
149+
assert_equal 1, ActiveRecord::Base.configurations.configs_for(env_name: "default_env", config_key: :custom_config).count
150+
assert_equal 2, ActiveRecord::Base.configurations.configs_for(env_name: "default_env", config_key: :custom_config, include_hidden: true).count
151+
assert_equal 2, ActiveRecord::Base.configurations.configs_for(env_name: "default_env").count
152+
ensure
153+
ActiveRecord::DatabaseConfigurations.db_config_handlers = previous_handlers
154+
ActiveRecord::Base.configurations = prev_configs
138155
end
139156

140-
def test_hidden_returns_replicas
157+
def test_configs_for_with_include_hidden
141158
config = {
142159
"default_env" => {
143160
"readonly" => { "adapter" => "sqlite3", "database" => "test/db/readonly.sqlite3", "replica" => true },
@@ -152,12 +169,4 @@ def test_hidden_returns_replicas
152169
ensure
153170
ActiveRecord::Base.configurations = prev_configs
154171
end
155-
156-
def test_include_replicas_is_deprecated
157-
assert_deprecated(ActiveRecord.deprecator) do
158-
db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary", include_replicas: true)
159-
160-
assert_equal "primary", db_config.name
161-
end
162-
end
163172
end

guides/source/7_1_release_notes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ Please refer to the [Changelog][active-record] for detailed changes.
8989

9090
* Remove support for `ActiveRecord.legacy_connection_handling`.
9191

92+
* Remove support for `:include_replicas` on `configs_for`. Use `:include_hidden` instead.
93+
9294
### Deprecations
9395

9496
### Notable changes

0 commit comments

Comments
 (0)