-
Notifications
You must be signed in to change notification settings - Fork 21.6k
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
Add support for hash and url configs to be used in connected_to #34196
Add support for hash and url configs to be used in connected_to #34196
Conversation
r? @eileencodes |
8a8d8a4
to
cd80e72
Compare
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. |
Ok, I assumed it was different because you can't currently use a connection hash because of this |
Ohhhh I see - I was thinking about |
The underlying problem is that there are a billion ways to make a configuration and connect to the database. Take a look at this test https://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
to
634f546
Compare
Done! I added a test for both hash configs and URLs since they both fail on master. |
|
||
ActiveRecord::Base.connected_to(database: config) do | ||
handler = ActiveRecord::Base.connection_handler | ||
assert_equal handler, ActiveRecord::Base.connection_handlers[config] |
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 in database.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 be postgres://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 db
end
Otherwise 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 use database
if its a symbol or role
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
to
8ad5850
Compare
|
||
ActiveRecord::Base.connected_to(database: config) do | ||
handler = ActiveRecord::Base.connection_handler | ||
assert_equal handler, ActiveRecord::Base.connection_handlers[config] |
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 db
end
Otherwise a symbol is expected.
config_hash = resolve_config_for_connection(database) | ||
handler = lookup_connection_handler(database.to_sym) | ||
handler = lookup_connection_handler(hanlder_key) |
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
to
d468d48
Compare
Add support for hash and url configs in database hash of `ActiveRecord::Base.connected_to`.
d468d48
to
abf5184
Compare
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.
Summary
I noticed when using the new connection switching API, if I use a string database name, it bubbles down to this 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_name
return a symbol instead of a string.