Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use symbols everywhere for database configurations #37185

Merged
merged 1 commit into from Sep 13, 2019

Conversation

seejohnrun
Copy link
Member

@seejohnrun seejohnrun commented Sep 12, 2019

Previously in some places we used symbol keys, and in some places we used
string keys. That made it pretty confusing to figure out in a particular
place what type of configuration object you were working with.

Now internally, all configuration hashes are keyed by symbols and
converted to such on the way in.

A few exceptions:

  • DatabaseConfigurations#to_h still returns strings for backward compatibility
  • Same for legacy_hash
  • default_hash previously could return strings, but the associated
    comment mentions it returns symbol-key Hash and now it always does

Because this is a change in behavior, a few method renames have happened:

  • DatabaseConfig#config is now DatabaseConfig#configuration_hash and returns a symbol-key Hash
  • ConnectionSpecification#config is now ConnectionSpecification#underlying_configuration_hash and returns the Hash of the underlying DatabaseConfig
  • DatabaseConfig#config was added back, returns String-keys for backward compatibility, and is deprecated in favor of the new configuration_hash

cc / @eileencodes

@seejohnrun seejohnrun force-pushed the config-symbols branch 14 times, most recently from 99943e7 to 0927160 Compare September 13, 2019 12:28
@seejohnrun seejohnrun marked this pull request as ready for review September 13, 2019 12:43
@eileencodes
Copy link
Member

@eileencodes eileencodes removed the request for review from rafaelfranca September 13, 2019 12:45
@eileencodes eileencodes self-assigned this Sep 13, 2019
@eileencodes eileencodes added this to the 6.1.0 milestone Sep 13, 2019
Copy link
Member

@eileencodes eileencodes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a CHANGELOG in ActiveRecord too?

@@ -13,6 +13,11 @@ def initialize(env_name, spec_name)
@spec_name = spec_name
end

def config
ActiveSupport::Deprecation.warn("DatabaseConfig#config will be removed in favor of DatabaseConfigurations#configuration_hash which returns a hash with symbol keys")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should update this to say what version it will be removed in which I guess since this is going into 6.1.0 it should be removed in 6.2.0

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All set!

Previously in some places we used symbol keys, and in some places we used
string keys. That made it pretty confusing to figure out in a particular
place what type of configuration object you were working with.

Now internally, all configuration hashes are keyed by symbols and
converted to such on the way in.

A few exceptions:

- `DatabaseConfigurations#to_h` still returns strings for backward compatibility
- Same for `legacy_hash`
- `default_hash` previously could return strings, but the associated
  comment mentions it returns symbol-key `Hash` and now it always does

Because this is a change in behavior, a few method renames have happened:

- `DatabaseConfig#config` is now `DatabaseConfig#configuration_hash` and returns a symbol-key `Hash`
- `ConnectionSpecification#config` is now `ConnectionSpecification#underlying_configuration_hash` and returns the `Hash` of the underlying `DatabaseConfig`
- `DatabaseConfig#config` was added back, returns `String`-keys for backward compatibility, and is deprecated in favor of the new `configuration_hash`

Co-authored-by: eileencodes <eileencodes@gmail.com>
@seejohnrun
Copy link
Member Author

CHANGELOG entry added! 🎉

Copy link
Contributor

@casperisfine casperisfine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -13,6 +13,11 @@ def initialize(env_name, spec_name)
@spec_name = spec_name
end

def config
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This object is private API. Do we need to deprecate and have a Changelog entry?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This object is private but the other two HashConfig and UrlConfig which inherit from this aren't. They shouldn't be used directly but when you loop through the configurations it will call config and 💥

Some apps may be looping through the configs and then calling config on it. I know we are in GitHub - and eventually all that code will go away as we remove the multi-db stuff but it's not impossible someone is relying on it.

ActiveRecord::Base.configurations.configs_for(env_name: "development").each do |db_config|
  db_config.config # this would blow up
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops I totally wrote the wrong thing here. I'm going back and forth between our app and Rails work and I dunno.

The reason I want to deprecate the string access is because I know at least our app depends on it. It's likely apps are doing db_config.config["database"] because they used to access that with a hash. I'm more comfortable deprecating than potentially breaking apps and gems that rely on the old hash behavior. Especially as we move away from hashes internally (and maybe one day in apps themselves).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@eileencodes eileencodes merged commit 42dea6b into rails:master Sep 13, 2019
@eileencodes eileencodes deleted the config-symbols branch September 13, 2019 17:03
eileencodes added a commit to seejohnrun/rails that referenced this pull request Apr 10, 2020
Calling `db_config.config` was deprecated in rails#37185 in favor of
`db_config.configuration_hash`. When returning the objects from
`configurations` we should ensure the object returns the non-deprecated
method.

Before:

```
<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007ff0f61696a0
@env_name="development", @name="primary", @config={:adapter=>"mysql2",
:database=>"recipes_development"}>
```

After:

```
<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007ff0f61696a0
@env_name="development", @name="primary", @configuration_hash={:adapter=>"mysql2",
:database=>"recipes_development"}>
```

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
nanophate added a commit to yasslab/dumper that referenced this pull request Jan 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants