summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/shared_model_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/lib/gitlab/database/shared_model_spec.rb')
-rw-r--r--spec/lib/gitlab/database/shared_model_spec.rb55
1 files changed, 39 insertions, 16 deletions
diff --git a/spec/lib/gitlab/database/shared_model_spec.rb b/spec/lib/gitlab/database/shared_model_spec.rb
index 7e0ba3397d1..2ae6ccf6c6a 100644
--- a/spec/lib/gitlab/database/shared_model_spec.rb
+++ b/spec/lib/gitlab/database/shared_model_spec.rb
@@ -2,7 +2,7 @@
require 'spec_helper'
-RSpec.describe Gitlab::Database::SharedModel do
+RSpec.describe Gitlab::Database::SharedModel, feature_category: :database do
describe 'using an external connection' do
let!(:original_connection) { described_class.connection }
let(:new_connection) { double('connection') }
@@ -85,28 +85,51 @@ RSpec.describe Gitlab::Database::SharedModel do
end
end
end
-
- def expect_original_connection_around
- # For safety, ensure our original connection is distinct from our double
- # This should be the case, but in case of something leaking we should verify
- expect(original_connection).not_to be(new_connection)
- expect(described_class.connection).to be(original_connection)
-
- yield
-
- expect(described_class.connection).to be(original_connection)
- end
end
describe '#connection_db_config' do
- it 'returns the class connection_db_config' do
- shared_model_class = Class.new(described_class) do
+ let!(:original_connection) { shared_model_class.connection }
+ let!(:original_connection_db_config) { shared_model_class.connection_db_config }
+ let(:shared_model) { shared_model_class.new }
+ let(:shared_model_class) do
+ Class.new(described_class) do
self.table_name = 'postgres_async_indexes'
end
+ end
- shared_model = shared_model_class.new
-
+ it 'returns the class connection_db_config' do
expect(shared_model.connection_db_config).to eq(described_class.connection_db_config)
end
+
+ context 'when switching the class connection' do
+ before do
+ skip_if_multiple_databases_not_setup
+ end
+
+ let(:new_base_model) { Ci::ApplicationRecord }
+ let(:new_connection) { new_base_model.connection }
+
+ it 'returns the db_config of the used connection when using load balancing' do
+ expect_original_connection_around do
+ described_class.using_connection(new_connection) do
+ expect(shared_model.connection_db_config).to eq(new_base_model.connection_db_config)
+ end
+ end
+
+ # it restores the connection_db_config afterwards
+ expect(shared_model.connection_db_config).to eq(original_connection_db_config)
+ end
+ end
+ end
+
+ def expect_original_connection_around
+ # For safety, ensure our original connection is distinct from our double
+ # This should be the case, but in case of something leaking we should verify
+ expect(original_connection).not_to be(new_connection)
+ expect(described_class.connection).to be(original_connection)
+
+ yield
+
+ expect(described_class.connection).to be(original_connection)
end
end