diff options
Diffstat (limited to 'config/initializers/00_connection_logger.rb')
-rw-r--r-- | config/initializers/00_connection_logger.rb | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/config/initializers/00_connection_logger.rb b/config/initializers/00_connection_logger.rb new file mode 100644 index 00000000000..9f03d13f95f --- /dev/null +++ b/config/initializers/00_connection_logger.rb @@ -0,0 +1,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) |