summaryrefslogtreecommitdiff
path: root/config/initializers/00_connection_logger.rb
blob: 9f03d13f95f75161d9e2773b55c2f34f69e51311 (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
# frozen_string_literal: true

module NewConnectionLogger
  extend ActiveSupport::Concern

  prepended do |base|
    base.class_attribute :warn_on_new_connection, default: false
  end

  class_methods do
    def new_client(...)
      if warn_on_new_connection && !ENV['SKIP_LOG_INITIALIZER_CONNECTIONS']
        cleaned_caller = Gitlab::BacktraceCleaner.clean_backtrace(caller)
        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"

        ActiveSupport::Deprecation.warn(message, cleaned_caller)

        warn caller if ENV['DEBUG_INITIALIZER_CONNECTIONS']
      end

      super
    end
  end
end

ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(NewConnectionLogger)