diff --git a/config/initializers/active_record_connection_pool_patches.rb b/config/initializers/active_record_connection_pool_patches.rb index 160fab02d81b76a7a8d02bd5e191d4478c197184..a7704b28be6f00a4383d7cf3324bace3ec8eb44f 100644 --- a/config/initializers/active_record_connection_pool_patches.rb +++ b/config/initializers/active_record_connection_pool_patches.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -if Gem::Version.new(ActiveRecord.gem_version) >= Gem::Version.new('7.1.0') - raise "This patch is not needed in Rails 7.1 and up" +unless Gitlab.next_rails? + ActiveRecord::ConnectionAdapters::ConnectionPool.prepend(Gitlab::Patch::ActiveRecordConnectionPool) end - -ActiveRecord::ConnectionAdapters::ConnectionPool.prepend(Gitlab::Patch::ActiveRecordConnectionPool) diff --git a/lib/gitlab/database/load_balancing/host.rb b/lib/gitlab/database/load_balancing/host.rb index ef32c8222a59965fa3db061851b04b0fdbeae5c0..0814e2b1172f91578ff780c9e6d8899a960bf620 100644 --- a/lib/gitlab/database/load_balancing/host.rb +++ b/lib/gitlab/database/load_balancing/host.rb @@ -111,11 +111,10 @@ def force_disconnect! end def pool_disconnect! - if Feature.enabled?(:load_balancing_disconnect_without_verify) - pool.disconnect_without_verify! - else - pool.disconnect! - end + return pool.disconnect! if ::Gitlab.next_rails? + return pool.disconnect_without_verify! if Feature.enabled?(:load_balancing_disconnect_without_verify) + + pool.disconnect! end def offline! diff --git a/spec/lib/gitlab/database/load_balancing/host_spec.rb b/spec/lib/gitlab/database/load_balancing/host_spec.rb index 54f912a11a87ba1659df6f99c152dd11b001c929..08ea4c39b656ff9060abf4d88d7e5fcaf54789db 100644 --- a/spec/lib/gitlab/database/load_balancing/host_spec.rb +++ b/spec/lib/gitlab/database/load_balancing/host_spec.rb @@ -82,20 +82,26 @@ def expect_next_replica_connection end end - context 'with load_balancing_disconnect_without_verify feature flag disabled' do - let(:disconnect_method) { :disconnect! } - - before do - stub_feature_flags(load_balancing_disconnect_without_verify: false) - end + let(:disconnect_method) { :disconnect! } + if ::Gitlab.next_rails? it_behaves_like 'disconnects the pool' - end + else + context 'with load_balancing_disconnect_without_verify feature flag disabled' do + let(:disconnect_method) { :disconnect! } + + before do + stub_feature_flags(load_balancing_disconnect_without_verify: false) + end - context 'with load_balancing_disconnect_without_verify feature flag enabled' do - let(:disconnect_method) { :disconnect_without_verify! } + it_behaves_like 'disconnects the pool' + end - it_behaves_like 'disconnects the pool' + context 'with load_balancing_disconnect_without_verify feature flag enabled' do + let(:disconnect_method) { :disconnect_without_verify! } + + it_behaves_like 'disconnects the pool' + end end end @@ -109,7 +115,11 @@ def expect_next_replica_connection describe '#offline!' do it 'marks the host as offline' do - expect(host.pool).to receive(:disconnect_without_verify!) + if ::Gitlab.next_rails? + expect(host.pool).to receive(:disconnect!) + else + expect(host.pool).to receive(:disconnect_without_verify!) + end expect(Gitlab::Database::LoadBalancing::Logger).to receive(:warn) .with(hash_including(event: :host_offline)) diff --git a/spec/lib/gitlab/patch/active_record_connection_pool_spec.rb b/spec/lib/gitlab/patch/active_record_connection_pool_spec.rb index 66de7199bf6c055a70dcc6c056bd2a9e71ecd77c..8d34261582883440d59a8c05dafb2bdf2f684c58 100644 --- a/spec/lib/gitlab/patch/active_record_connection_pool_spec.rb +++ b/spec/lib/gitlab/patch/active_record_connection_pool_spec.rb @@ -21,22 +21,34 @@ subject(:pool) { ActiveRecord::ConnectionAdapters::ConnectionPool.new(pool_config) } describe '#disconnect_without_verify!' do - it 'does not call verify!' do - expect(done_connection).not_to receive(:verify!) + unless Gitlab.next_rails? + it 'does not call verify!' do + expect(done_connection).not_to receive(:verify!) - pool.disconnect_without_verify! + pool.disconnect_without_verify! - expect(pool.connections.count).to eq(0) + expect(pool.connections.count).to eq(0) + end end end describe '#disconnect!' do - it 'calls verify on the connection' do - expect(done_connection).to receive(:verify!).and_call_original + if Gitlab.next_rails? + it 'does not call verify on the connection' do + expect(done_connection).not_to receive(:verify!) - pool.disconnect! + pool.disconnect! - expect(pool.connections.count).to eq(0) + expect(pool.connections.count).to eq(0) + end + else + it 'calls verify on the connection' do + expect(done_connection).to receive(:verify!).and_call_original + + pool.disconnect! + + expect(pool.connections.count).to eq(0) + end end end end