summaryrefslogtreecommitdiff
path: root/lib/gitlab/database.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gitlab/database.rb')
-rw-r--r--lib/gitlab/database.rb30
1 files changed, 27 insertions, 3 deletions
diff --git a/lib/gitlab/database.rb b/lib/gitlab/database.rb
index 9b32d285ec0..1b16873f737 100644
--- a/lib/gitlab/database.rb
+++ b/lib/gitlab/database.rb
@@ -195,6 +195,16 @@ module Gitlab
MAX_TIMESTAMP_VALUE > timestamp ? timestamp : MAX_TIMESTAMP_VALUE.dup
end
+ def self.all_uncached(&block)
+ # Calls to #uncached only disable caching for the current connection. Since the load balancer
+ # can potentially upgrade from read to read-write mode (using a different connection), we specify
+ # up-front that we'll explicitly use the primary for the duration of the operation.
+ Gitlab::Database::LoadBalancing::Session.current.use_primary do
+ base_models = database_base_models.values
+ base_models.reduce(block) { |blk, model| -> { model.uncached(&blk) } }.call
+ end
+ end
+
def self.allow_cross_joins_across_databases(url:)
# this method is implemented in:
# spec/support/database/prevent_cross_joins.rb
@@ -221,12 +231,26 @@ module Gitlab
::ActiveRecord::Base.configurations.configs_for(env_name: Rails.env).map(&:name)
end
+ # This returns all matching schemas that a given connection can use
+ # Since the `ActiveRecord::Base` might change the connection (from main to ci)
+ # This does not look at literal connection names, but rather compares
+ # models that are holders for a given db_config_name
+ def self.gitlab_schemas_for_connection(connection)
+ connection_name = self.db_config_name(connection)
+ primary_model = self.database_base_models.fetch(connection_name)
+
+ self.schemas_to_base_models
+ .select { |_, models| models.include?(primary_model) }
+ .keys
+ .map!(&:to_sym)
+ end
+
def self.db_config_for_connection(connection)
return unless connection
- # The LB connection proxy does not have a direct db_config
- # that can be referenced
- return if connection.is_a?(::Gitlab::Database::LoadBalancing::ConnectionProxy)
+ if connection.is_a?(::Gitlab::Database::LoadBalancing::ConnectionProxy)
+ return connection.load_balancer.configuration.primary_db_config
+ end
# During application init we might receive `NullPool`
return unless connection.respond_to?(:pool) &&