debian-mirror-gitlab/lib/gitlab/database/each_database.rb

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

84 lines
2.9 KiB
Ruby
Raw Normal View History

2021-12-11 22:18:48 +05:30
# frozen_string_literal: true
module Gitlab
module Database
module EachDatabase
class << self
2022-06-21 17:19:12 +05:30
def each_database_connection(only: nil, include_shared: true)
2022-05-07 20:08:51 +05:30
selected_names = Array.wrap(only)
base_models = select_base_models(selected_names)
base_models.each_pair do |connection_name, model|
2022-06-21 17:19:12 +05:30
next if !include_shared && Gitlab::Database.db_config_share_with(model.connection_db_config)
2021-12-11 22:18:48 +05:30
connection = model.connection
with_shared_connection(connection, connection_name) do
yield connection, connection_name
end
end
end
2022-05-07 20:08:51 +05:30
def each_model_connection(models, only_on: nil, &blk)
selected_databases = Array.wrap(only_on).map(&:to_sym)
2021-12-11 22:18:48 +05:30
models.each do |model|
2022-04-04 11:22:00 +05:30
# If model is shared, iterate all available base connections
# Example: `LooseForeignKeys::DeletedRecord`
if model < ::Gitlab::Database::SharedModel
2022-05-07 20:08:51 +05:30
with_shared_model_connections(model, selected_databases, &blk)
2022-04-04 11:22:00 +05:30
else
2022-05-07 20:08:51 +05:30
with_model_connection(model, selected_databases, &blk)
2021-12-11 22:18:48 +05:30
end
end
end
private
2022-05-07 20:08:51 +05:30
def select_base_models(names)
2022-08-13 15:12:31 +05:30
base_models = Gitlab::Database.database_base_models_with_gitlab_shared
2022-05-07 20:08:51 +05:30
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
def with_shared_model_connections(shared_model, selected_databases, &blk)
2022-08-13 15:12:31 +05:30
Gitlab::Database.database_base_models_with_gitlab_shared.each_pair do |connection_name, connection_model|
2022-04-04 11:22:00 +05:30
if shared_model.limit_connection_names
next unless shared_model.limit_connection_names.include?(connection_name.to_sym)
end
2022-05-07 20:08:51 +05:30
next if selected_databases.present? && !selected_databases.include?(connection_name.to_sym)
2022-04-04 11:22:00 +05:30
with_shared_connection(connection_model.connection, connection_name) do
yield shared_model, connection_name
end
end
end
2022-05-07 20:08:51 +05:30
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-04-04 11:22:00 +05:30
with_shared_connection(model.connection, connection_name) do
yield model, connection_name
end
end
2021-12-11 22:18:48 +05:30
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