-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Optional ActiveRecord config #2038
Conversation
next unless Kernel.const_defined? host | ||
Connections.extended(Kernel.const_get(host)) | ||
end | ||
end |
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.
Why?
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.
See the spec.
context "without database available" do
before { clear_active_record_connection }
after { establish_active_record_connection }
context "with use_active_record set to true" do
I wrote a spec which tests exactly this behavior.
establish_active_record_connection
takes care of creating AR connection, but when it runs for the first time the models are not yet defined and later in the middle of the suite the connection is dropped which leads to the fact that the db tables are missing and need to be migrated again.
Maybe switching to a sqlite file will be a better option, then the initial migrations stay in place after dropping the connections.
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.
This should only be done in the specs necessary and not globally.
@Jack12816 any chance that you take a look at the travis build and get it to a passing state? Thank you! |
This is a workaround for ActiveStorage bugs. Ideally it should be fixed there instead of adding new code to rspec-rails but I'd support this to save people time if we don't think they'll fix it |
@mrgordon If you'd like to use this as a base and fix it up I'm sure @Jack12816 won't mind, its gone rather stale. Please note the existing feedback still needs taking into account and a build passing. |
@mrgordon you said
Do you have more infos? Maybe it is fixed and we don't need this patch anymore? Especially with rspec-rails 4 |
I have no other info. Just seems logical that ActiveStorage should either not require ActiveRecord at all or should automatically require it if necessary so it functions. Instead there is an unstated (?) / unclear dependency that is only noticed if you remove ActiveRecord because things break. |
@mrgordon thanks @Jack12816 Do you think you can rebase. |
9cde8aa
to
85e23f3
Compare
@benoittgt I've rebased the branch and did some changes to make it work with Rails 3. |
89ba3a0
to
f5740fc
Compare
@Jack12816 are you up to wrap this up? |
@Jack12816 a friendly reminder. |
e0d8a8b
to
562940e
Compare
Signed-off-by: Hermann Mayer <hermann.mayer92@gmail.com>
@pirj I've rebased it to upstream master and now all tests passing. |
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 had actually started looking at this myself to pick up the slack and I'm not convinced this solution works as I'm not convinced the specs work. I'd like to see a test that fails without modifying the code, and without modifying the global specs.
next unless Kernel.const_defined? host | ||
Connections.extended(Kernel.const_get(host)) | ||
end | ||
end |
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.
This should only be done in the specs necessary and not globally.
end | ||
end | ||
|
||
establish_active_record_connection |
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.
This should be removed, we don't want every other test having a database connection.
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.
Good point, but it's not a subject of the PR. Actually all tests require a database connection due to the *Model
classes at ar_classes.rb
. When disabling AR support on the specs overall, everything is broken. Disabling the database connection on all tests sounds like a bigger refactoring.
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.
Actually all tests require a database connection due to the *Model classes at ar_classes.rb. When disabling AR support on the specs overall, everything is broken.
Currently there is no database configured so I'm not sure thats true, if your changes cause things to fail your changes are at fault, applying a global config to the specs is not a valid fix.
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.
Currently there is no database configured so I'm not sure thats true
https://github.com/rspec/rspec-rails/blob/master/spec/support/ar_classes.rb#L1-L19
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.
Hmm ok, well make it fail with 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.
We have in_sub_process
helpers if you need to change something, but I don't want global configuration changes for just this spec, it shouldn't be necessary.
|
||
after { RSpec.configuration.use_active_record = true } | ||
|
||
include_examples "unrelated example does not raise" |
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.
This spec does nothing to demonstrate that the support works when active record is available but not configured, you should be able to demonstrate the issue without adding database support as its supposed to be broken without database 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.
Unfortunately, I don't get your point. When use_active_record
is set to true
and no AR connection is available, it raises an ActiveRecord::ConnectionNotEstablished
. When use_active_record
is set to false
and no AR connection is available it does not raise. Could you explain in detail what you have in mind for another test?
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.
Its artificial because you have configured a database then manually cleared it.
The default is no configuration, the changes adding config should be removed and these spec should demonstrate that requiring' active_record'
causes an error, and that setting use_active_record
false prevents it.
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.
Sorry, I've been trying to find time to work on this myself because I believe strongly that adding database configuration for all the specs is not the way to go! :)
Thanks for your hard work getting this started, closing in favour of #2266, would you be able to check that works for you? Once green I can ship as a beta if you'd like, this is one of the last things we'd like included in |
- What is it good for
This PR ships a solution for #1876.
- What I did
I added a new option
use_active_record
to the RSpec config. This option is also added to the generator for newspec/rails_helper.rb
files. The config option is read on theFixtureSupport
module before the ActiveRecord loading happens. Tests with and without this setting were added in the case a database connection is not established.- A picture of a cute animal (not mandatory but encouraged)