Skip to content
Snippets Groups Projects
Commit ffa2465c authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre
Browse files

Dynamically sets the database connection for Geo::BaseRegistry model

If Gitlab::Geo.secondary? changes, we need to reinitialize the  connection properly in the model to avoid requiring a full unicorn restart.
parent bf6920d0
No related merge requests found
class Geo::BaseRegistry < ActiveRecord::Base
self.abstract_class = true
if Gitlab::Geo.configured? && (Gitlab::Geo.secondary? || Rails.env.test?)
establish_connection Rails.configuration.geo_database
# If Gitlab::Geo.secondary? changes, we need to reinitialize the connection
# properly in the model to avoid requiring a full unicorn restart.
def self.retrieve_connection
set_connection! if should_change_connection?
connection_handler.retrieve_connection(self)
end
private
def self.set_connection!
if Gitlab::Geo.tracking_connection_available?
establish_connection Rails.configuration.geo_database
else
establish_connection "#{Rails.env}".to_sym
end
end
def self.should_change_connection?
using_master_connection? && Gitlab::Geo.tracking_connection_available?
end
def self.using_master_connection?
master_connection = ActiveRecord::Base.connection_config[:database]
tracking_connection = Geo::BaseRegistry.connection_config[:database]
master_connection === tracking_connection
end
end
......@@ -46,6 +46,10 @@ def self.configured?
Rails.configuration.respond_to?(:geo_database)
end
def self.tracking_connection_available?
self.cache_value(:tracking_connection_available) { self.configured? && (self.secondary? || Rails.env.test?) }
end
def self.license_allows?
::License.current&.feature_available?(:geo)
end
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment