summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/migrations/reestablished_connection_stack_spec.rb
blob: c6327de98d14b5e9b9b40cb938c8ce0adc80eeec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Database::Migrations::ReestablishedConnectionStack do
  let(:base_class) { ActiveRecord::Migration }

  let(:model) do
    base_class.new
      .extend(described_class)
  end

  describe '#with_restored_connection_stack' do
    Gitlab::Database.database_base_models_with_gitlab_shared.each do |db_config_name, _|
      context db_config_name do
        it_behaves_like "reconfigures connection stack", db_config_name do
          it 'does restore connection hierarchy' do
            model.with_restored_connection_stack do
              validate_connections_stack!
            end
          end

          primary_db_config = ActiveRecord::Base.configurations.primary?(db_config_name)

          it 'does reconfigure connection handler', unless: primary_db_config do
            original_handler = ActiveRecord::Base.connection_handler
            new_handler = nil

            model.with_restored_connection_stack do
              new_handler = ActiveRecord::Base.connection_handler

              # establish connection
              ApplicationRecord.connection.select_one("SELECT 1 FROM projects LIMIT 1")
              Ci::ApplicationRecord.connection.select_one("SELECT 1 FROM ci_builds LIMIT 1")
            end

            expect(new_handler).not_to eq(original_handler), "is reconnected"
            expect(new_handler).not_to be_active_connections
            expect(ActiveRecord::Base.connection_handler).to eq(original_handler), "is restored"
          end

          it 'does keep original connection handler', if: primary_db_config do
            original_handler = ActiveRecord::Base.connection_handler
            new_handler = nil

            model.with_restored_connection_stack do
              new_handler = ActiveRecord::Base.connection_handler
            end

            expect(new_handler).to eq(original_handler)
            expect(ActiveRecord::Base.connection_handler).to eq(original_handler)
          end
        end
      end
    end
  end
end