- Notifications
You must be signed in to change notification settings - Fork22.1k
Add support for hash and url configs to be used in connected_to#34196
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
gmcgibbon commentedOct 11, 2018
8a8d8a4 tocd80e72Compareeileencodes commentedOct 11, 2018 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Right now To me a string that's not a URL causing an error is the correct behavior. I do think however this error message could be improved to note it expected a database URL instead. |
gmcgibbon commentedOct 11, 2018 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Ok, I assumed it was different because you can't currently use a connection hash because of this |
eileencodes commentedOct 12, 2018
Ohhhh I see - I was thinking about |
eileencodes commentedOct 12, 2018
The underlying problem is that there are a billion ways to make a configuration and connect to the database. Take a look at this testhttps://github.com/rails/rails/blob/master/activerecord/test/cases/connection_adapters/merge_and_resolve_default_url_config_test.rb and you'll get an idea of some of the possible scenarios. We may eventually want to say some of these are not going to be possible with a multi-db application but I don't think we're there yet. For example, I'm not sure how a DATABASE_URL would work with multiple databases - since you can only pass one at a time. |
cd80e72 to634f546Comparegmcgibbon commentedOct 12, 2018
Done! I added a test for both hash configs and URLs since they both fail on master. |
gmcgibbonOct 12, 2018 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I'm not sure how I feel about this. Should we try to lookup the config to store the handler with a symbol key and fallback to this behaviour if its not mentioned indatabase.yml?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Oh 💩 it just occurred to me this isn't going to work. It's fine if it's a symbol but what would the handler be if it was a url? We definitely don't want the handler to bepostgres://blah so I think we need to change connects to to take a role and a database at the same time and only if it's a symbol use the database name as a handler. Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Yeah, I think if we always lookup handlers using symbols, that makes the most sense. Does it make sense to always use roles for handler lookup or do we need to still try using the database spec/env/config/url/??? for legacy support?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
We need the handler/role for when you're connected already and we need the database for connecting to a database that your model doesn't always want to be connected to (for example, readonly_slow). Role should always be symbol. I'm not sure what legacy support we would need? This is a new method so we can change it until 6 is released.
I've been thinking about this and I think we should either accept a symbol or a hash for the database option. If you need to pass a string then you need to also pass a role / name / handler with it.
ActiveRecord::Base.connected_to(database: { my_url_db: "postgres://blah" }) do # do something with this dbendOtherwise a symbol is expected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I changed the logic around to usedatabase if its a symbol orrole if it isn't. That should solve the issue AFAICT.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I've been thinking about this and I think we should either accept a symbol or a hash for the database option. If you need to pass a string then you need to also pass a role / name / handler with it.
I've refactored the method to behave this way for both URLs and config hashes. WDYT?
195dc38 to8ad5850CompareThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
We need the handler/role for when you're connected already and we need the database for connecting to a database that your model doesn't always want to be connected to (for example, readonly_slow). Role should always be symbol. I'm not sure what legacy support we would need? This is a new method so we can change it until 6 is released.
I've been thinking about this and I think we should either accept a symbol or a hash for the database option. If you need to pass a string then you need to also pass a role / name / handler with it.
ActiveRecord::Base.connected_to(database: { my_url_db: "postgres://blah" }) do # do something with this dbendOtherwise a symbol is expected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
You have a typo in handler 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
🤦♂️
c2d0a58 tod468d48CompareAdd support for hash and url configs in database hashof `ActiveRecord::Base.connected_to`.
d468d48 toabf5184Compareeileencodes commentedOct 30, 2018
This looks great! Thank you! |
| @@ -1,3 +1,18 @@ | |||
| * Add support for hash and url configs in database hash of`ActiveRecord::Base.connected_to`. | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Great addition. Thank you! Just want to note that we don't add changelog entries about complementing features that haven't been released.
Uh oh!
There was an error while loading.Please reload this page.
Summary
I noticed when using the new connection switching API, if I use a string database name, it bubbles down tothis connection resolution method which thinks strings are database URLs. Do we want to support URLs, or is this acceptable behaviour? If not, we should make
DatabaseConfig#spec_namereturn a symbol instead of a string.