2021-10-20 20:12:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Database
|
|
|
|
module EachDatabase
|
|
|
|
class << self
|
2022-04-01 11:08:40 -04:00
|
|
|
def each_database_connection(only: nil, include_shared: true)
|
2022-02-23 07:13:44 -05:00
|
|
|
selected_names = Array.wrap(only)
|
|
|
|
base_models = select_base_models(selected_names)
|
|
|
|
|
|
|
|
base_models.each_pair do |connection_name, model|
|
2022-04-01 11:08:40 -04:00
|
|
|
next if !include_shared && Gitlab::Database.db_config_share_with(model.connection_db_config)
|
|
|
|
|
2021-11-15 10:10:57 -05:00
|
|
|
connection = model.connection
|
2021-10-20 20:12:27 -04:00
|
|
|
|
|
|
|
with_shared_connection(connection, connection_name) do
|
|
|
|
yield connection, connection_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-13 17:09:30 -04:00
|
|
|
def each_model_connection(models, only_on: nil, &blk)
|
|
|
|
selected_databases = Array.wrap(only_on).map(&:to_sym)
|
|
|
|
|
2021-10-20 20:12:27 -04:00
|
|
|
models.each do |model|
|
2022-02-01 13:17:05 -05:00
|
|
|
# If model is shared, iterate all available base connections
|
|
|
|
# Example: `LooseForeignKeys::DeletedRecord`
|
|
|
|
if model < ::Gitlab::Database::SharedModel
|
2022-03-13 17:09:30 -04:00
|
|
|
with_shared_model_connections(model, selected_databases, &blk)
|
2022-02-01 13:17:05 -05:00
|
|
|
else
|
2022-03-13 17:09:30 -04:00
|
|
|
with_model_connection(model, selected_databases, &blk)
|
2021-10-20 20:12:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-02-23 07:13:44 -05:00
|
|
|
def select_base_models(names)
|
|
|
|
base_models = Gitlab::Database.database_base_models
|
|
|
|
|
|
|
|
return base_models if names.empty?
|
|
|
|
|
|
|
|
names.each_with_object(HashWithIndifferentAccess.new) do |name, hash|
|
|
|
|
raise ArgumentError, "#{name} is not a valid database name" unless base_models.key?(name)
|
|
|
|
|
|
|
|
hash[name] = base_models[name]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-13 17:09:30 -04:00
|
|
|
def with_shared_model_connections(shared_model, selected_databases, &blk)
|
2022-02-01 13:17:05 -05:00
|
|
|
Gitlab::Database.database_base_models.each_pair do |connection_name, connection_model|
|
|
|
|
if shared_model.limit_connection_names
|
|
|
|
next unless shared_model.limit_connection_names.include?(connection_name.to_sym)
|
|
|
|
end
|
|
|
|
|
2022-03-13 17:09:30 -04:00
|
|
|
next if selected_databases.present? && !selected_databases.include?(connection_name.to_sym)
|
|
|
|
|
2022-02-01 13:17:05 -05:00
|
|
|
with_shared_connection(connection_model.connection, connection_name) do
|
|
|
|
yield shared_model, connection_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-13 17:09:30 -04:00
|
|
|
def with_model_connection(model, selected_databases, &blk)
|
|
|
|
connection_name = model.connection_db_config.name
|
|
|
|
|
|
|
|
return if selected_databases.present? && !selected_databases.include?(connection_name.to_sym)
|
2022-02-01 13:17:05 -05:00
|
|
|
|
|
|
|
with_shared_connection(model.connection, connection_name) do
|
|
|
|
yield model, connection_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-20 20:12:27 -04:00
|
|
|
def with_shared_connection(connection, connection_name)
|
|
|
|
Gitlab::Database::SharedModel.using_connection(connection) do
|
|
|
|
Gitlab::AppLogger.debug(message: 'Switched database connection', connection_name: connection_name)
|
|
|
|
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|