debian-mirror-gitlab/lib/initializer_connections.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

33 lines
1.2 KiB
Ruby
Raw Permalink Normal View History

2022-08-13 15:12:31 +05:30
# frozen_string_literal: true
module InitializerConnections
2023-04-23 21:23:45 +05:30
# 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
2022-08-13 15:12:31 +05:30
return yield if Gitlab::Utils.to_boolean(ENV['SKIP_RAISE_ON_INITIALIZE_CONNECTIONS'])
2023-04-23 21:23:45 +05:30
previous_connection_counts = ActiveRecord::Base.connection_handler.connection_pool_list.to_h do |pool|
[pool.db_config.name, pool.connections.size]
end
2022-08-13 15:12:31 +05:30
yield
2023-04-23 21:23:45 +05:30
new_connection_counts = ActiveRecord::Base.connection_handler.connection_pool_list.to_h do |pool|
[pool.db_config.name, pool.connections.size]
2022-08-13 15:12:31 +05:30
end
2023-04-23 21:23:45 +05:30
raise_database_connection_made_error unless previous_connection_counts == new_connection_counts
end
def self.raise_database_connection_made_error
2022-08-13 15:12:31 +05:30
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