diff --git a/.github/workflows/test-postgresql.yml b/.github/workflows/test-postgresql.yml index f395bec..7c30962 100644 --- a/.github/workflows/test-postgresql.yml +++ b/.github/workflows/test-postgresql.yml @@ -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 diff --git a/CHANGELOG.md b/CHANGELOG.md index c40f8d7..4be8fc1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/Gemfile b/Gemfile index 1401663..0eefdf4 100644 --- a/Gemfile +++ b/Gemfile @@ -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']." @@ -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' diff --git a/lib/slack-ruby-bot-server/config/database_adapters/activerecord.rb b/lib/slack-ruby-bot-server/config/database_adapters/activerecord.rb index d7ea4ad..c3e8e0f 100644 --- a/lib/slack-ruby-bot-server/config/database_adapters/activerecord.rb +++ b/lib/slack-ruby-bot-server/config/database_adapters/activerecord.rb @@ -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}" diff --git a/spec/slack-ruby-bot-server/app_spec.rb b/spec/slack-ruby-bot-server/app_spec.rb index c66cb5d..07bd35c 100644 --- a/spec/slack-ruby-bot-server/app_spec.rb +++ b/spec/slack-ruby-bot-server/app_spec.rb @@ -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!)