Skip to content

Commit 94256a0

Browse files
Resolve Hash with url key as a UrlConfig
In `DatabaseConfigurations#configs_for`, `Hash` configs that contain a `:url` key resolve to a `UrlConfig`. Since we weren't mirroring that behavior in `Resolver`, we had to add explicit handling for `url` keys to the `HashConfig` implementation. Now we're going to always resolve these hashes as `UrlConfig` and can remove the extra implementation of URL handling from `HashConfig`. Of course, this does introduce some duplication between `DatabaseConfigurations` and `Resolver` which we plan to address in a future PR. Co-authored-by: eileencodes <[email protected]>
1 parent 2c14a0f commit 94256a0

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

activerecord/lib/active_record/connection_adapters/resolver.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def resolve(config_or_env, pool_name = nil)
8686
when String
8787
DatabaseConfigurations::UrlConfig.new(env, "primary", config_or_env)
8888
when Hash
89-
DatabaseConfigurations::HashConfig.new(env, "primary", config_or_env)
89+
resolve_hash_configuration(env, config_or_env.symbolize_keys)
9090
when DatabaseConfigurations::DatabaseConfig
9191
config_or_env
9292
else
@@ -95,6 +95,20 @@ def resolve(config_or_env, pool_name = nil)
9595
end
9696

9797
private
98+
# Resolve a hash to a valid configuration object. This method will
99+
# either return a HashConfig, or a UrlConfig if the passed Hash
100+
# contains a `:url` key.
101+
def resolve_hash_configuration(env, config)
102+
if config.has_key?(:url)
103+
url = config[:url]
104+
config_without_url = config.dup
105+
config_without_url.delete :url
106+
DatabaseConfigurations::UrlConfig.new(env, "primary", url, config)
107+
else
108+
DatabaseConfigurations::HashConfig.new(env, "primary", config)
109+
end
110+
end
111+
98112
# Takes the environment such as +:production+ or +:development+ and a
99113
# pool name the corresponds to the name given by the connection pool
100114
# to the connection. That pool name is merged into the hash with the

activerecord/lib/active_record/database_configurations/hash_config.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ class HashConfig < DatabaseConfig
2828
def initialize(env_name, spec_name, config)
2929
super(env_name, spec_name)
3030
@config = config.symbolize_keys
31-
32-
resolve_url_key
3331
end
3432

3533
def configuration_hash
@@ -49,14 +47,6 @@ def replica?
4947
def migrations_paths
5048
configuration_hash[:migrations_paths]
5149
end
52-
53-
private
54-
def resolve_url_key
55-
if configuration_hash[:url] && !configuration_hash[:url].match?(/^jdbc:/)
56-
connection_hash = ConnectionUrlResolver.new(configuration_hash[:url]).to_hash
57-
configuration_hash.merge!(connection_hash)
58-
end
59-
end
6050
end
6151
end
6252
end

0 commit comments

Comments
 (0)