Skip to content

Commit bd3e97c

Browse files
authored
fix(ActiveRecord): correctly connect to database in Rails 7.2+ (#175)
* fix(ActiveRecord): correctly connect to the database in Rails 7.2+ 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 * chore: skip activerecord specs on mongoid * deps: use correct database_cleaner adapter per database * deps: pin mongoid-scroll to v1
1 parent c2bc038 commit bd3e97c

File tree

7 files changed

+70
-5
lines changed

7 files changed

+70
-5
lines changed

.github/workflows/test-postgresql.yml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ jobs:
1010
- { ruby: 2.6.2, postgresql: 11, active_record: '~> 6.0.0' }
1111
- { ruby: 3.1.1, postgresql: 11, active_record: '~> 6.1.0' }
1212
- { ruby: 3.1.1, postgresql: 14, active_record: '~> 7.0.0' }
13+
- { ruby: 3.1.1, postgresql: 14, active_record: '~> 7.2.0' }
1314
name: test (ruby=${{ matrix.entry.ruby }}, postgresql=${{ matrix.entry.postgresql }}, active_record=${{ matrix.entry.active_record }})
1415
steps:
1516
- uses: actions/checkout@v2

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### 2.1.2 (Next)
44

55
* Your contribution here.
6+
* [#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).
67

78
### 2.1.1 (2023/07/25)
89

Gemfile

+10-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@ case ENV.fetch('DATABASE_ADAPTER', nil)
44
when 'mongoid' then
55
gem 'kaminari-mongoid'
66
gem 'mongoid', ENV['MONGOID_VERSION'] || '~> 7.3.0'
7-
gem 'mongoid-scroll'
7+
gem 'mongoid-scroll', '~> 1.0.1'
88
gem 'mongoid-shell'
9+
10+
group :development, :test do
11+
gem 'database_cleaner-mongoid', '~> 2.0.1'
12+
end
913
when 'activerecord' then
1014
gem 'activerecord', ENV['ACTIVERECORD_VERSION'] || '~> 6.0.0'
1115
gem 'otr-activerecord'
12-
gem 'pagy_cursor'
16+
gem 'pagy_cursor', '~> 0.6.1'
1317
gem 'pg'
18+
19+
group :development, :test do
20+
gem 'database_cleaner-active_record', '~> 2.2.0'
21+
end
1422
when nil
1523
warn "Missing ENV['DATABASE_ADAPTER']."
1624
else
@@ -23,7 +31,6 @@ group :development, :test do
2331
gem 'bundler'
2432
gem 'byebug'
2533
gem 'capybara', '~> 3.36.0'
26-
gem 'database_cleaner', '~> 1.8.5'
2734
gem 'fabrication'
2835
gem 'faker'
2936
gem 'faraday', '0.17.5'

lib/slack-ruby-bot-server/config/database_adapters/activerecord.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
module SlackRubyBotServer
77
module DatabaseAdapter
88
def self.check!
9-
ActiveRecord::Base.connection_pool.with_connection(&:active?)
9+
if ActiveRecord.version >= Gem::Version.new('7.2')
10+
ActiveRecord::Base.connection.database_exists?
11+
else
12+
ActiveRecord::Base.connection_pool.with_connection(&:active?)
13+
end
14+
1015
raise 'Unexpected error.' unless ActiveRecord::Base.connected?
1116
rescue StandardError => e
1217
warn "Error connecting to PostgreSQL: #{e.message}"

spec/support/database_cleaner.rb spec/database_adapters/activerecord/database_cleaner.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'database_cleaner'
1+
require 'database_cleaner/active_record'
22

33
RSpec.configure do |config|
44
config.before :suite do
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require 'database_cleaner/mongoid'
2+
3+
RSpec.configure do |config|
4+
config.before :suite do
5+
DatabaseCleaner.strategy = :deletion
6+
DatabaseCleaner.clean_with :deletion
7+
end
8+
9+
config.around :each do |example|
10+
DatabaseCleaner.cleaning do
11+
example.run
12+
end
13+
end
14+
end

spec/slack-ruby-bot-server/app_spec.rb

+37
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,43 @@
1313
expect(app.instance).to be_an_instance_of(app)
1414
end
1515
end
16+
context '#prepare!' do
17+
context 'when connection cannot be established' do
18+
context 'with ActiveRecord >= 7.2' do
19+
before do
20+
skip 'incorrect database adapter' unless ENV['DATABASE_ADAPTER'] == 'activerecord'
21+
skip 'incorrect ActiveRecord version' if ActiveRecord.version < Gem::Version.new('7.2')
22+
23+
# Make sure ActiveRecord is not connected in any way before the spec starts
24+
ActiveRecord::Base.connection_pool.disconnect!
25+
end
26+
27+
it 'raises' do
28+
expect(ActiveRecord::Base).not_to be_connected
29+
30+
# Simulate that #database_exists? does not connect to the database
31+
allow(ActiveRecord::Base).to receive_message_chain(:connection, :database_exists?)
32+
expect { subject.prepare! }
33+
.to raise_error('Unexpected error.')
34+
end
35+
end
36+
37+
context 'with ActiveRecord < 7.2' do
38+
before do
39+
skip 'incorrect database adapter' unless ENV['DATABASE_ADAPTER'] == 'activerecord'
40+
skip 'incorrect ActiveRecord version' if ActiveRecord.version >= Gem::Version.new('7.2')
41+
end
42+
43+
it 'raises' do
44+
# In ActiveRecord < 7.1, `disconnect!` closes the connection that has already been leased by
45+
# DatabaseCleaner, so we cannot do that trick here to simulate a not established connection.
46+
allow(ActiveRecord::Base).to receive(:connected?).and_return(false)
47+
expect { subject.prepare! }
48+
.to raise_error('Unexpected error.')
49+
end
50+
end
51+
end
52+
end
1653
context '#purge_inactive_teams!' do
1754
it 'purges teams' do
1855
expect(Team).to receive(:purge!)

0 commit comments

Comments
 (0)