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 a fallback database config when loading schema cache #38383
Conversation
The schema cache defaults to loading the 'primary' database config. However, if an app doesn't have a db config with a spec name of 'primary' the filename lookup will blow up. This adds a fallback for this case.
I thought the database configuration loading code guaranteed that one of the db configs would have a spec name of "primary". Is that not the case? Do you have a test app that can reproduce the issue? |
Not yet; let me make one |
I created a test app: https://github.com/kytrinyx/rails-test-app-pumpkin The database config uses the three tier config style that we use at GitHub: In the Rails console I did this:
|
Got it. Instead of loading the cache just for the "primary" connection, would it make sense to loop through each configuration, looking for schema caches, then loading those caches? Then we don't depend on a "primary" connection. |
I think that's what this PR does, which didn't get merged: #34449 I know the multi db stuff is broken, but the change in that PR might actually be "more correct" than the current state. Thoughts? |
@kytrinyx ya, that PR does look like what we should be doing. Can you test it out, and try fixing if it's broken? |
@casperisfine do you have opinion about this one? I think we have this problem solved in our codebase. |
I tested looping through all the configs and a bunch of tests broke, so I backed that out. |
We simply don't use that code at all, and added support for multidb schema cache. Meaning each connection go grab it's schema cache in a different path (it's more complicated because many connection have similar schemas, so they share their cache as well, but it doesn't matter here). As for Rails users in general, my worry about this PR is that by falling back to the first connection, we might load a cache that doesn't match the connection. But note that I haven't double checked wether it's actually possible or not. IMHO It would be preferable to actually implement multi-schema cache support, e.g. one cache dump per IIRC I tried to implement that a few months back, but it was very hard. It might be easier now that some refactorings have been done. |
Agreed. I pushed up a change that will simply not load a cache if we don't find a primary connection, which I think is safer. I will start working on a new PR to see if I can work out how to load all the specs for all the connections. Last time I tried that a bunch of tests broke, so that is probably not going to be a quick fix. |
Summary
The schema cache defaults to loading the 'primary' database config.
However, if an app doesn't have a db config with a spec name of
'primary' the filename lookup will blow up.
This adds a fallback for this case.
Other Information
The filename for the schema cache used to be hard-coded, but Rails allows people to override the default path. In #38348 I made a change to take overrides into account, but failed to account for apps without a database config containing a spec named
primary
.It turns out, at GitHub we don't have a database named
primary
, so even though we don't rely on this active record initializer, it blows up for us.