summaryrefslogtreecommitdiff
path: root/lib/initializer_connections.rb
blob: ae2809b7604b3aca9a0a1c859fe1b32a8dd6357e (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
# frozen_string_literal: true

module InitializerConnections
  # Raises if new database connections established within the block
  #
  # NOTE: this does not prevent existing connections that is already checked out
  # from being used. You will need other means to prevent that such as by
  # clearing all connections as implemented in the
  # `:clear_active_connections_again` initializer for routes
  #
  def self.raise_if_new_database_connection
    return yield if Gitlab::Utils.to_boolean(ENV['SKIP_RAISE_ON_INITIALIZE_CONNECTIONS'])

    previous_connection_counts = ActiveRecord::Base.connection_handler.connection_pool_list.to_h do |pool|
      [pool.db_config.name, pool.connections.size]
    end

    yield

    new_connection_counts = ActiveRecord::Base.connection_handler.connection_pool_list.to_h do |pool|
      [pool.db_config.name, pool.connections.size]
    end

    raise_database_connection_made_error unless previous_connection_counts == new_connection_counts
  end

  def self.raise_database_connection_made_error
    message = "Database connection should not be called during initializers. Read more at https://docs.gitlab.com/ee/development/rails_initializers.html#database-connections-in-initializers"

    raise message
  end
end