Rails changes - Allow configuration of Geo tracking database in database.yml
Problem
Currently, Geo manually manages additional DB connections. This results in a number of overwrites to provide this kind of support.
It should rather depend on Rails 6 many databases support to reduce complexity of the implementation. This conflicts to some extent with many databases support and causes additional complexity on dealing with Geo doing something unique.
module Geo
class TrackingBase < ApplicationRecord
self.abstract_class = true
NOT_CONFIGURED_MSG = 'Geo secondary database is not configured'
SecondaryNotConfigured = Class.new(StandardError)
if ::Gitlab::Geo.geo_database_configured?
establish_connection Rails.configuration.geo_database
end
def self.connected?
return false unless ::Gitlab::Geo.geo_database_configured?
connection_handler.connected?(connection_specification_name)
end
def self.connection
unless ::Gitlab::Geo.geo_database_configured?
message = NOT_CONFIGURED_MSG
message = "#{message}\nIn the GDK root, try running `make geo-setup`" if Rails.env.development?
raise SecondaryNotConfigured, message
end
# Don't call super because LoadBalancing::ActiveRecordProxy will intercept it
retrieve_connection
rescue ActiveRecord::NoDatabaseError
raise SecondaryNotConfigured, NOT_CONFIGURED_MSG
end
end
end
Use single database.yml instead of database_geo.yml:
production:
main:
host: postgres
database: gitlabhq_production
...
geo:
host: postgres
database: gitlabhq_geo_production
...
development:
Proposal
-
Configure tracking database in database.ymlinstead of customdatabase_geo.yml -
Use connects_toto make Geo models use the tracking database instead ofdef self.connectionoverride inGeo::TrackingBase -
Use connected_toblock to manually use the tracking database instead ofGitlab::Geo::DatabaseTasks#set_db_env -
Ensure that Gitlab::Geo::HealthCheckstill works -
Deprecate custom rake geo:db:*rake tasks in favor of built-in tasksrake db:*:geo -
Changes must be backwards compatible across updates
References
Edited by Douglas Barbosa Alexandre