Module: InitializerConnections
- Defined in:
- lib/initializer_connections.rb
Class Method Summary collapse
- .debug_database_queries ⇒ Object
- .raise_database_connection_made_error ⇒ Object
-
.raise_if_new_database_connection ⇒ Object
Raises if new database connections established within the block.
Class Method Details
.debug_database_queries ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/initializer_connections.rb', line 33 def self.debug_database_queries return yield if Gitlab::Utils.to_boolean(ENV['SKIP_DEBUG_INITIALIZE_CONNECTIONS'], default: Rails.env.production?) callback = ->(_name, _started, _finished, _unique_id, payload) do # rubocop:disable Gitlab/RailsLogger -- development/test only Rails.logger.debug("InitializerConnections Query: #{payload[:sql]}") Gitlab::BacktraceCleaner.clean_backtrace(caller).each do |line| Rails.logger.debug("InitializerConnections Backtrace: #{line}") end # rubocop:enable Gitlab/RailsLogger end ActiveSupport::Notifications.subscribed(callback, "sql.active_record") do yield end end |
.raise_database_connection_made_error ⇒ Object
51 52 53 54 55 |
# File 'lib/initializer_connections.rb', line 51 def self.raise_database_connection_made_error = "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 end |
.raise_if_new_database_connection ⇒ Object
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
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/initializer_connections.rb', line 11 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(ApplicationRecord.current_role).map do |pool| pool.connections.size end results = debug_database_queries do yield end new_connection_counts = ActiveRecord::Base.connection_handler.connection_pool_list(ApplicationRecord.current_role).map do |pool| pool.connections.size end raise_database_connection_made_error unless previous_connection_counts == new_connection_counts results end |