-
-
Notifications
You must be signed in to change notification settings - Fork 418
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
Fix: Respect Rails defaults in an initializer by delaying loading ActiveRecord adapter until framework is loaded #832
Fix: Respect Rails defaults in an initializer by delaying loading ActiveRecord adapter until framework is loaded #832
Conversation
I moved the 3 commits that fix the tests to this other PR #833 |
35c29cc
to
9ab2d7e
Compare
@pbstriker38 thanks for the PR. I'd love to figure out a test for this. We used to wait to load the AR adaptar with |
I could see if I can get it to reproduce with one of the Rails executable test case examples https://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#create-an-executable-test-case |
We've been running this fork for 2 months now because without it many of our tests that use |
9ab2d7e
to
93be628
Compare
@@ -215,18 +215,27 @@ | |||
end | |||
end | |||
|
|||
it "works when table doesn't exist" do |
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 noticed that this test wasn't actually testing the table not existing, but the fact that it's not even connected to the DB. Without the silence
, you can see the message:
No connection pool for 'ActiveRecord::Base' found.. You likely need to run `rails g flipper:active_record` and/or `rails db:migrate`.
Ok. So I created a new rails app to create a minimal reproduction. I could not get it to reproduce. The one difference from this new app and ours is the configuration defaults. A new app has Flipper is causing ActiveRecord to load too soon and it ignores our Rails.application.config.active_record.belongs_to_required_validates_foreign_key = false Without my fix irb(main):001> ActiveRecord.belongs_to_required_validates_foreign_key
=> true with my fix irb(main):001> ActiveRecord.belongs_to_required_validates_foreign_key
=> false |
Using gem 'flipper-active_record', require: false |
class Model < ::ActiveRecord::Base | ||
self.abstract_class = true | ||
end | ||
ActiveSupport.on_load(:active_record) do |
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.
Would be simpler to just move all the things that depends on Active Record into another file and then require it when AR is loaded, or something like that.
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.
There's already a generator for adding the DB migrations, so another option is to just have the generator add these models to the app/models
directory.
93be628
to
bad0fc1
Compare
bad0fc1
to
9e62a45
Compare
@bkeepers you have more experience with these bits. Any thoughts? Good to merge? |
Yes, I'm fine moving forward with this as-is, but do want to highlight a concern: I'm a little nervious that there could be unintended consequences with this specific solution. For example, what happens if this adapter gets loaded in the app boot process before AR is loaded? Previously, it would work fine. Now, it will raise a It's probably a non-issue since AR gets loaded so early in the app boot process and before any user code runs. I'm just trying to think through scenarios. If it causes any issues we could explore a couple other options:
|
The Flipper models are loading too soon and causing weirdness with saving models that have a belongs_to. The belongs_to association is being loaded on save even when there have not been any changes to the association.
BEFORE
AFTER