1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Deprecate to_h and to_legacy_hash

These should have been deprecated when I added them but for some reason
I didn't.

As we move away from passing hashes around we no longer need these
methods, and since we no longer use configuration hashes as database
configuration we can deprecate this in favor of using the database
configuration objects and access the connection hashes from there.

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
This commit is contained in:
eileencodes 2019-09-17 14:27:41 -04:00
parent 84496b85cb
commit 4e01932d43
4 changed files with 116 additions and 126 deletions

View file

@ -79,12 +79,11 @@ module ActiveRecord
# Returns the DatabaseConfigurations object as a Hash. # Returns the DatabaseConfigurations object as a Hash.
def to_h def to_h
configs = configurations.reverse.inject({}) do |memo, db_config| configurations.inject({}) do |memo, db_config|
memo.merge(db_config.to_legacy_hash) memo.merge(db_config.env_name => db_config.configuration_hash.stringify_keys)
end end
Hash[configs.to_a.reverse]
end end
deprecate to_h: "You can use `ActiveRecord::Base.configurations.configs_for(env_name: 'env', spec_name: 'primary').configuration_hash` to get the configuration hashes."
# Checks if the application's configurations are empty. # Checks if the application's configurations are empty.
# #

View file

@ -38,10 +38,6 @@ module ActiveRecord
false false
end end
def to_legacy_hash
{ env_name => configuration_hash.stringify_keys }
end
def for_current_env? def for_current_env?
env_name == ActiveRecord::ConnectionHandling::DEFAULT_ENV.call env_name == ActiveRecord::ConnectionHandling::DEFAULT_ENV.call
end end

View file

@ -17,9 +17,9 @@ module ActiveRecord
ENV["RAILS_ENV"] = @previous_rails_env ENV["RAILS_ENV"] = @previous_rails_env
end end
def resolve_config(config) def resolve_config(config, env_name = ActiveRecord::ConnectionHandling::DEFAULT_ENV.call)
configs = ActiveRecord::DatabaseConfigurations.new(config) configs = ActiveRecord::DatabaseConfigurations.new(config)
configs.to_h configs.configs_for(env_name: env_name, spec_name: "primary")&.configuration_hash
end end
def resolve_spec(spec, config) def resolve_spec(spec, config)
@ -116,16 +116,21 @@ module ActiveRecord
def test_jdbc_url def test_jdbc_url
config = { "production" => { "url" => "jdbc:postgres://localhost/foo" } } config = { "production" => { "url" => "jdbc:postgres://localhost/foo" } }
actual = resolve_config(config) actual = resolve_config(config, "production")
assert_equal config, actual assert_equal config["production"].symbolize_keys, actual
end end
def test_environment_does_not_exist_in_config_url_does_exist def test_environment_does_not_exist_in_config_url_does_exist
ENV["DATABASE_URL"] = "postgres://localhost/foo" ENV["DATABASE_URL"] = "postgres://localhost/foo"
config = { "not_default_env" => { "adapter" => "not_postgres", "database" => "not_foo" } } config = { "not_default_env" => { "adapter" => "not_postgres", "database" => "not_foo" } }
actual = resolve_config(config) actual = resolve_config(config, "default_env")
expect_prod = { "adapter" => "postgresql", "database" => "foo", "host" => "localhost" } expect_prod = {
assert_equal expect_prod, actual["default_env"] adapter: "postgresql",
database: "foo",
host: "localhost"
}
assert_equal expect_prod, actual
end end
def test_url_with_hyphenated_scheme def test_url_with_hyphenated_scheme
@ -138,38 +143,38 @@ module ActiveRecord
def test_string_connection def test_string_connection
config = { "default_env" => "postgres://localhost/foo" } config = { "default_env" => "postgres://localhost/foo" }
actual = resolve_config(config) actual = resolve_config(config, "default_env")
expected = { "default_env" => expected = {
{ "adapter" => "postgresql", adapter: "postgresql",
"database" => "foo", database: "foo",
"host" => "localhost" host: "localhost"
}
} }
assert_equal expected, actual assert_equal expected, actual
end end
def test_url_sub_key def test_url_sub_key
config = { "default_env" => { "url" => "postgres://localhost/foo" } } config = { "default_env" => { "url" => "postgres://localhost/foo" } }
actual = resolve_config(config) actual = resolve_config(config)
expected = { "default_env" => expected = {
{ "adapter" => "postgresql", adapter: "postgresql",
"database" => "foo", database: "foo",
"host" => "localhost" host: "localhost"
}
} }
assert_equal expected, actual assert_equal expected, actual
end end
def test_hash def test_hash
config = { "production" => { "adapter" => "postgres", "database" => "foo" } } config = { "production" => { "adapter" => "postgres", "database" => "foo" } }
actual = resolve_config(config) actual = resolve_config(config, "production")
assert_equal config, actual assert_equal config["production"].symbolize_keys, actual
end end
def test_blank def test_blank
config = {} config = {}
actual = resolve_config(config) actual = resolve_config(config, "default_env")
assert_equal config, actual assert_nil actual
end end
def test_blank_with_database_url def test_blank_with_database_url
@ -177,17 +182,13 @@ module ActiveRecord
config = {} config = {}
actual = resolve_config(config) actual = resolve_config(config)
expected = { "adapter" => "postgresql", expected = {
"database" => "foo", adapter: "postgresql",
"host" => "localhost" } database: "foo",
assert_equal expected, actual["default_env"] host: "localhost"
assert_nil actual["production"] }
assert_nil actual["development"]
assert_nil actual["test"] assert_equal expected, actual
assert_nil actual[:default_env]
assert_nil actual[:production]
assert_nil actual[:development]
assert_nil actual[:test]
end end
def test_blank_with_database_url_with_rails_env def test_blank_with_database_url_with_rails_env
@ -196,20 +197,13 @@ module ActiveRecord
config = {} config = {}
actual = resolve_config(config) actual = resolve_config(config)
expected = { "adapter" => "postgresql", expected = {
"database" => "foo", adapter: "postgresql",
"host" => "localhost" } database: "foo",
host: "localhost"
}
assert_equal expected, actual["not_production"] assert_equal expected, actual
assert_nil actual["production"]
assert_nil actual["default_env"]
assert_nil actual["development"]
assert_nil actual["test"]
assert_nil actual[:default_env]
assert_nil actual[:not_production]
assert_nil actual[:production]
assert_nil actual[:development]
assert_nil actual[:test]
end end
def test_blank_with_database_url_with_rack_env def test_blank_with_database_url_with_rack_env
@ -218,20 +212,13 @@ module ActiveRecord
config = {} config = {}
actual = resolve_config(config) actual = resolve_config(config)
expected = { "adapter" => "postgresql", expected = {
"database" => "foo", adapter: "postgresql",
"host" => "localhost" } database: "foo",
host: "localhost"
}
assert_equal expected, actual["not_production"] assert_equal expected, actual
assert_nil actual["production"]
assert_nil actual["default_env"]
assert_nil actual["development"]
assert_nil actual["test"]
assert_nil actual[:default_env]
assert_nil actual[:not_production]
assert_nil actual[:production]
assert_nil actual[:development]
assert_nil actual[:test]
end end
def test_database_url_with_ipv6_host_and_port def test_database_url_with_ipv6_host_and_port
@ -239,11 +226,14 @@ module ActiveRecord
config = {} config = {}
actual = resolve_config(config) actual = resolve_config(config)
expected = { "adapter" => "postgresql", expected = {
"database" => "foo", adapter: "postgresql",
"host" => "::1", database: "foo",
"port" => 5454 } host: "::1",
assert_equal expected, actual["default_env"] port: 5454
}
assert_equal expected, actual
end end
def test_url_sub_key_with_database_url def test_url_sub_key_with_database_url
@ -251,12 +241,12 @@ module ActiveRecord
config = { "default_env" => { "url" => "postgres://localhost/foo" } } config = { "default_env" => { "url" => "postgres://localhost/foo" } }
actual = resolve_config(config) actual = resolve_config(config)
expected = { "default_env" => expected = {
{ "adapter" => "postgresql", adapter: "postgresql",
"database" => "foo", database: "foo",
"host" => "localhost" host: "localhost"
}
} }
assert_equal expected, actual assert_equal expected, actual
end end
@ -264,19 +254,21 @@ module ActiveRecord
ENV["DATABASE_URL"] = "postgres://localhost/baz" ENV["DATABASE_URL"] = "postgres://localhost/baz"
config = { "default_env" => { "database" => "foo" }, "other_env" => { "url" => "postgres://foohost/bardb" } } config = { "default_env" => { "database" => "foo" }, "other_env" => { "url" => "postgres://foohost/bardb" } }
actual = resolve_config(config) expected = {
expected = { "default_env" => default_env: {
{ "database" => "baz", database: "baz",
"adapter" => "postgresql", adapter: "postgresql",
"host" => "localhost" host: "localhost"
}, },
"other_env" => other_env: {
{ "adapter" => "postgresql", adapter: "postgresql",
"database" => "bardb", database: "bardb",
"host" => "foohost" host: "foohost"
} }
} }
assert_equal expected, actual
assert_equal expected[:default_env], resolve_config(config, "default_env")
assert_equal expected[:other_env], resolve_config(config, "other_env")
end end
def test_merge_no_conflicts_with_database_url def test_merge_no_conflicts_with_database_url
@ -284,13 +276,13 @@ module ActiveRecord
config = { "default_env" => { "pool" => "5" } } config = { "default_env" => { "pool" => "5" } }
actual = resolve_config(config) actual = resolve_config(config)
expected = { "default_env" => expected = {
{ "adapter" => "postgresql", adapter: "postgresql",
"database" => "foo", database: "foo",
"host" => "localhost", host: "localhost",
"pool" => "5" pool: "5"
}
} }
assert_equal expected, actual assert_equal expected, actual
end end
@ -299,13 +291,13 @@ module ActiveRecord
config = { "default_env" => { "adapter" => "NOT-POSTGRES", "database" => "NOT-FOO", "pool" => "5" } } config = { "default_env" => { "adapter" => "NOT-POSTGRES", "database" => "NOT-FOO", "pool" => "5" } }
actual = resolve_config(config) actual = resolve_config(config)
expected = { "default_env" => expected = {
{ "adapter" => "postgresql", adapter: "postgresql",
"database" => "foo", database: "foo",
"host" => "localhost", host: "localhost",
"pool" => "5" pool: "5"
}
} }
assert_equal expected, actual assert_equal expected, actual
end end
@ -314,13 +306,13 @@ module ActiveRecord
config = { "default_env" => { "adapter" => "postgresql", "pool" => "5" } } config = { "default_env" => { "adapter" => "postgresql", "pool" => "5" } }
actual = resolve_config(config) actual = resolve_config(config)
expected = { "default_env" => expected = {
{ "adapter" => "postgresql", adapter: "postgresql",
"database" => "foo", database: "foo",
"host" => "localhost", host: "localhost",
"pool" => "5" pool: "5"
}
} }
assert_equal expected, actual assert_equal expected, actual
end end
@ -329,12 +321,11 @@ module ActiveRecord
config = { "default_env" => { "pool" => 5 } } config = { "default_env" => { "pool" => 5 } }
actual = resolve_config(config) actual = resolve_config(config)
expected = { "default_env" => expected = {
{ "adapter" => "postgresql", adapter: "postgresql",
"database" => "foo", database: "foo",
"host" => "localhost", host: "localhost",
"pool" => 5 pool: 5
}
} }
assert_equal expected, actual assert_equal expected, actual

View file

@ -48,13 +48,15 @@ class DatabaseConfigurationsTest < ActiveRecord::TestCase
assert_equal "primary", config.spec_name assert_equal "primary", config.spec_name
end end
def test_to_h_turns_db_config_object_back_into_a_hash def test_to_h_turns_db_config_object_back_into_a_hash_and_is_deprecated
configs = ActiveRecord::Base.configurations configs = ActiveRecord::Base.configurations
assert_equal "ActiveRecord::DatabaseConfigurations", configs.class.name assert_equal "ActiveRecord::DatabaseConfigurations", configs.class.name
assert_deprecated do
assert_equal "Hash", configs.to_h.class.name assert_equal "Hash", configs.to_h.class.name
assert_equal ["arunit", "arunit2", "arunit_without_prepared_statements"], ActiveRecord::Base.configurations.to_h.keys.sort assert_equal ["arunit", "arunit2", "arunit_without_prepared_statements"], ActiveRecord::Base.configurations.to_h.keys.sort
end end
end end
end
class LegacyDatabaseConfigurationsTest < ActiveRecord::TestCase class LegacyDatabaseConfigurationsTest < ActiveRecord::TestCase
unless in_memory_db? unless in_memory_db?
@ -73,10 +75,12 @@ class LegacyDatabaseConfigurationsTest < ActiveRecord::TestCase
end end
end end
def test_can_turn_configurations_into_a_hash def test_can_turn_configurations_into_a_hash_and_is_deprecated
assert_deprecated do
assert ActiveRecord::Base.configurations.to_h.is_a?(Hash), "expected to be a hash but was not." assert ActiveRecord::Base.configurations.to_h.is_a?(Hash), "expected to be a hash but was not."
assert_equal ["arunit", "arunit2", "arunit_without_prepared_statements"].sort, ActiveRecord::Base.configurations.to_h.keys.sort assert_equal ["arunit", "arunit2", "arunit_without_prepared_statements"].sort, ActiveRecord::Base.configurations.to_h.keys.sort
end end
end
def test_each_is_deprecated def test_each_is_deprecated
assert_deprecated do assert_deprecated do