Skip to content

Commit

Permalink
fix(ActiveRecord): correctly connect to the database in Rails 7.2+
Browse files Browse the repository at this point in the history
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
  • Loading branch information
markokajzer committed Nov 30, 2024
1 parent c2bc038 commit 8423592
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test-postgresql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jobs:
- { ruby: 2.6.2, postgresql: 11, active_record: '~> 6.0.0' }
- { ruby: 3.1.1, postgresql: 11, active_record: '~> 6.1.0' }
- { ruby: 3.1.1, postgresql: 14, active_record: '~> 7.0.0' }
- { ruby: 3.1.1, postgresql: 14, active_record: '~> 7.2.0' }
name: test (ruby=${{ matrix.entry.ruby }}, postgresql=${{ matrix.entry.postgresql }}, active_record=${{ matrix.entry.active_record }})
steps:
- uses: actions/checkout@v2
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### 2.1.2 (Next)

* Your contribution here.
* [#175](https://github.com/slack-ruby/slack-ruby-bot-server/pull/175): Fix(activerecord): correctly check for database in rails 7.2+ - [@markokajzer](https://github.com/markokajzer).

### 2.1.1 (2023/07/25)

Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ when 'mongoid' then
when 'activerecord' then
gem 'activerecord', ENV['ACTIVERECORD_VERSION'] || '~> 6.0.0'
gem 'otr-activerecord'
gem 'pagy_cursor'
gem 'pagy_cursor', '~> 0.6.1'
gem 'pg'
when nil
warn "Missing ENV['DATABASE_ADAPTER']."
Expand All @@ -23,7 +23,7 @@ group :development, :test do
gem 'bundler'
gem 'byebug'
gem 'capybara', '~> 3.36.0'
gem 'database_cleaner', '~> 1.8.5'
gem 'database_cleaner', '~> 2.1.0'
gem 'fabrication'
gem 'faker'
gem 'faraday', '0.17.5'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
module SlackRubyBotServer
module DatabaseAdapter
def self.check!
ActiveRecord::Base.connection_pool.with_connection(&:active?)
if ActiveRecord.version >= Gem::Version.new('7.2')
ActiveRecord::Base.connection.database_exists?
else
ActiveRecord::Base.connection_pool.with_connection(&:active?)
end

raise 'Unexpected error.' unless ActiveRecord::Base.connected?
rescue StandardError => e
warn "Error connecting to PostgreSQL: #{e.message}"
Expand Down
40 changes: 40 additions & 0 deletions spec/slack-ruby-bot-server/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,46 @@
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
before do
skip 'incorrect ActiveRecord version' if ActiveRecord.version < Gem::Version.new('7.2')

# Make sure ActiveRecord is not connected in any way before the spec starts
ActiveRecord::Base.connection_pool.disconnect!
end

after do
skip 'incorrect ActiveRecord version' if ActiveRecord.version < Gem::Version.new('7.2')

RSpec::Mocks.space.proxy_for(ActiveRecord::Base).reset
ActiveRecord::Base.connection.reconnect!
end

it 'raises' do
# Simulate that #database_exists? does not connect to the database
allow(ActiveRecord::Base).to receive_message_chain(:connection, :database_exists?)
expect { subject.prepare! }
.to raise_error('Unexpected error.')
end
end

context 'with ActiveRecord < 7.2' do
before do
skip 'incorrect ActiveRecord version' if ActiveRecord.version >= Gem::Version.new('7.2')
end

it 'raises' do
# In ActiveRecord < 7.1, `disconnect!` + `reconnect!` closes the connection that has already been leased by
# DatabaseCleaner, so we cannot do that trick here to simulate a not established connection.
allow(ActiveRecord::Base).to receive(:connected?).and_return(false)
expect { subject.prepare! }
.to raise_error('Unexpected error.')
end
end
end
end
context '#purge_inactive_teams!' do
it 'purges teams' do
expect(Team).to receive(:purge!)
Expand Down

0 comments on commit 8423592

Please sign in to comment.