-
-
Notifications
You must be signed in to change notification settings - Fork 75
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(ActiveRecord): correctly connect to database in Rails 7.2+ #175
Conversation
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.
Thanks!
Don't compare versions of Ruby like this though.
https://code.dblock.org/2020/07/16/comparing-version-numbers-in-ruby.html
Let's add a test that uses >= 7.2?
|
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 assume the new CI fails without the fix?
One more nit, sorry!
8423592
to
47b4fd6
Compare
ci: add active_record 7.2 to ci matrix deps: pin pagy_cursor to prevent incompatible version deps: pin activerecord 7.2+ compatible version of database_cleaner ran into DatabaseCleaner/database_cleaner-active_record#83
@@ -13,6 +13,41 @@ | |||
expect(app.instance).to be_an_instance_of(app) | |||
end | |||
end | |||
context '#prepare!' do | |||
context 'when connection cannot be established' do | |||
context 'with ActiveRecord >= 7.2' 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.
added a spec for 7.2+ vs pre-7.2.
quick explanation:
DatabaseCleaner
leases the connection, so ActiveRecord::Base.connected?
is true
when the spec starts, but we want to simulate what should happen in prod:
ActiveRecord::Base.connected?
is falseActiveRecord::Base.connection.database_exists?
(orActiveRecord::Base.connection_pool.with_connection(&:active?)
) connects to the databaseActiveRecord::Base.connected?
is true
in pre-7.2 that does not seem to work, the specs ultimately fail because DatabaseCleaner
tries to operate on a closed connection. i've not been able to come up with a good spec for pre-7.2 so not sure if we should keep the spec, lmkwyt!
i added comments as well because it's maybe not obvious what is going on
along the way i had to do the following changes
- pin
pagy_cursor
to~> 0.6.1
- the method
pagy_cursor
only supports 2 parameters on 0.8, see here
- the method
- pin
database_cleaner
to a higher version to support ActiveRecord 7.2
i ran the full spec suite locally with all versions from the matrix and all passed, so let's hope ci does us the honor as well
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 ran the full spec suite locally with all versions from the matrix and all passed, so let's hope ci does us the honor as well
First time contributor PRs marked as pending is a little annoying (a 1-time problem), but you can always enable CI on your fork for next time you run into this.
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.
but you can always enable CI on your fork
thanks for the tip, i somehow overlooked this lol
Looks like some of the CI is failing :( |
301607d
to
30e2fcf
Compare
cab8570
to
d2612ec
Compare
the fork is now green: https://github.com/markokajzer/slack-ruby-bot-server 😤 there were two more things to do
lastly, i noticed a flake in the spec sometimes fails both for
no additional changes were made between each of those runs. sorry for the hassle by the way, this took quite a bit longer than i had hoped for myself as well 😅 |
Thank you so much for doing the work! No hassle at all, and looking forward to more :) |
There was a failure in ruby=3.1.1, postgresql=14, active_record=~> 7.2.0, but it looked like a fluke and restarting passed. I'll watch it, it's probably a flaky test. |
@@ -4,13 +4,21 @@ case ENV.fetch('DATABASE_ADAPTER', nil) | |||
when 'mongoid' then | |||
gem 'kaminari-mongoid' | |||
gem 'mongoid', ENV['MONGOID_VERSION'] || '~> 7.3.0' | |||
gem 'mongoid-scroll' | |||
gem 'mongoid-scroll', '~> 1.0.1' |
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 one had a breaking change in 2.0 that's easy to fix. It's something like this (from dblock/slack-strava@88af16b#diff-633cee3ca2d39648d625e12b921138aa0a932ebe9375ba53102cbabc339e5afe).
ActiveRecord::Base.connection_pool.with_connection(&:active?)
was used to establish a connection to the database, so that subsequentlyActiveRecord::Base.connected?
becomes true.In Rails 7.2, this is not the case anymore, and
ActiveRecord::Base.connection_pool.with_connection(&:active?)
does not flipActiveRecord::Base.connected?
Instead, what we can do is perform a direct check like
ActiveRecord::Base.connection.database_exists?
. Also see rails/rails#52946.