Commit 38b2bcf9 authored by Michael Kozono's avatar Michael Kozono

Merge branch...

Merge branch '348356-use-connects_to-to-make-geo-models-use-the-tracking-database-instead-of-def-self-connection' into 'master'

Use connects_to to connect to the Geo tracking DB

See merge request gitlab-org/gitlab!76700
parents f9b4c92b 235cfc03
...@@ -10,13 +10,7 @@ module Geo ...@@ -10,13 +10,7 @@ module Geo
SecondaryNotConfigured = Class.new(StandardError) SecondaryNotConfigured = Class.new(StandardError)
if ::Gitlab::Geo.geo_database_configured? if ::Gitlab::Geo.geo_database_configured?
# Mark current model as a `connection_class` connects_to database: { writing: :geo, reading: :geo }
# This is the behavior when executing `connects_to`
# which indicates that given class is holding a connection object
# and should be treated accordingly.
self.connection_class = true
establish_connection Rails.configuration.geo_database
end end
def self.connected? def self.connected?
...@@ -37,5 +31,13 @@ module Geo ...@@ -37,5 +31,13 @@ module Geo
rescue ActiveRecord::NoDatabaseError rescue ActiveRecord::NoDatabaseError
raise SecondaryNotConfigured, NOT_CONFIGURED_MSG raise SecondaryNotConfigured, NOT_CONFIGURED_MSG
end end
class SchemaMigration < TrackingBase
class << self
def all_versions
order(:version).pluck(:version)
end
end
end
end end
end end
...@@ -75,7 +75,7 @@ module Gitlab ...@@ -75,7 +75,7 @@ module Gitlab
end end
def self.geo_database_configured? def self.geo_database_configured?
Rails.configuration.respond_to?(:geo_database) ::Gitlab::Database.has_config?(:geo)
end end
def self.primary_node_configured? def self.primary_node_configured?
......
...@@ -61,7 +61,27 @@ module SystemCheck ...@@ -61,7 +61,27 @@ module SystemCheck
end end
def tables_present? def tables_present?
Gitlab::Geo::DatabaseTasks.with_geo_db { !ActiveRecord::Base.connection.migration_context.needs_migration? } !needs_migration?
end
def needs_migration?
(migrations.collect(&:version) - get_all_versions).size > 0 # rubocop:disable Style/ZeroLengthPredicate
end
def get_all_versions
if schema_migration.table_exists?
schema_migration.all_versions.map(&:to_i)
else
[]
end
end
def migrations
::Geo::TrackingBase.connection.migration_context.migrations
end
def schema_migration
::Geo::TrackingBase::SchemaMigration
end end
def geo_health_check def geo_health_check
......
...@@ -7,15 +7,14 @@ RSpec.describe SystemCheck::Geo::GeoDatabaseConfiguredCheck, :silence_stdout do ...@@ -7,15 +7,14 @@ RSpec.describe SystemCheck::Geo::GeoDatabaseConfiguredCheck, :silence_stdout do
describe '#multi_check', :reestablished_active_record_base do describe '#multi_check', :reestablished_active_record_base do
it "checks database configuration" do it "checks database configuration" do
stub_configuration_check(false) stub_database_state(subject, configured: false)
expect(subject).to receive(:try_fixing_it).with(described_class::WRONG_CONFIGURATION_MESSAGE) expect(subject).to receive(:try_fixing_it).with(described_class::WRONG_CONFIGURATION_MESSAGE)
expect(subject.multi_check).to be_falsey expect(subject.multi_check).to be_falsey
end end
it "checks database configuration" do it "checks database configuration" do
stub_configuration_check(true) stub_database_state(subject, active: false)
stub_connection_state(false)
expect(subject).to receive(:try_fixing_it).with(described_class::UNHEALTHY_CONNECTION_MESSAGE) expect(subject).to receive(:try_fixing_it).with(described_class::UNHEALTHY_CONNECTION_MESSAGE)
...@@ -23,9 +22,7 @@ RSpec.describe SystemCheck::Geo::GeoDatabaseConfiguredCheck, :silence_stdout do ...@@ -23,9 +22,7 @@ RSpec.describe SystemCheck::Geo::GeoDatabaseConfiguredCheck, :silence_stdout do
end end
it "checks table existence" do it "checks table existence" do
stub_configuration_check(true) stub_database_state(subject, tables: false)
stub_connection_state(true)
stub_tables_existence(false)
expect(subject).to receive(:try_fixing_it).with(described_class::NO_TABLES_MESSAGE) expect(subject).to receive(:try_fixing_it).with(described_class::NO_TABLES_MESSAGE)
...@@ -33,10 +30,7 @@ RSpec.describe SystemCheck::Geo::GeoDatabaseConfiguredCheck, :silence_stdout do ...@@ -33,10 +30,7 @@ RSpec.describe SystemCheck::Geo::GeoDatabaseConfiguredCheck, :silence_stdout do
end end
it "checks if existing database is being reused" do it "checks if existing database is being reused" do
stub_configuration_check(true) stub_database_state(subject, fresh: false)
stub_connection_state(true)
stub_tables_existence(true)
stub_fresh_database(false)
expect(subject).to receive(:try_fixing_it).with(described_class::REUSING_EXISTING_DATABASE_MESSAGE) expect(subject).to receive(:try_fixing_it).with(described_class::REUSING_EXISTING_DATABASE_MESSAGE)
...@@ -44,10 +38,7 @@ RSpec.describe SystemCheck::Geo::GeoDatabaseConfiguredCheck, :silence_stdout do ...@@ -44,10 +38,7 @@ RSpec.describe SystemCheck::Geo::GeoDatabaseConfiguredCheck, :silence_stdout do
end end
it "returns true when all checks passed" do it "returns true when all checks passed" do
stub_configuration_check(true) stub_database_state(subject)
stub_connection_state(true)
stub_tables_existence(true)
stub_fresh_database(true)
expect(subject).not_to receive(:try_fixing_it) expect(subject).not_to receive(:try_fixing_it)
...@@ -55,23 +46,14 @@ RSpec.describe SystemCheck::Geo::GeoDatabaseConfiguredCheck, :silence_stdout do ...@@ -55,23 +46,14 @@ RSpec.describe SystemCheck::Geo::GeoDatabaseConfiguredCheck, :silence_stdout do
end end
end end
def stub_configuration_check(state) def stub_database_state(subject, configured: true, active: true, tables: true, fresh: true)
expect(Gitlab::Geo).to receive(:geo_database_configured?).and_return(state) allow(subject).to receive(:needs_migration?).and_return(!tables)
end
def stub_connection_state(state)
connection = double
expect(connection).to receive(:active?).and_return(state)
expect(::Geo::TrackingBase).to receive(:connection).and_return(connection)
end
def stub_tables_existence(state) allow(::Gitlab::Geo).to receive(:geo_database_configured?).and_return(configured)
expect_any_instance_of(ActiveRecord::MigrationContext).to receive(:needs_migration?).and_return(!state) allow(::Geo::TrackingBase).to receive(:connection).and_return(double(active?: active))
end
def stub_fresh_database(state) allow_next_instance_of(::Gitlab::Geo::HealthCheck) do |health_check|
expect_next_instance_of(Gitlab::Geo::HealthCheck) do |geo_health_check| allow(health_check).to receive(:reusing_existing_tracking_database?).and_return(!fresh)
expect(geo_health_check).to receive(:reusing_existing_tracking_database?).and_return(!state)
end end
end end
end end
...@@ -45,7 +45,7 @@ module EE ...@@ -45,7 +45,7 @@ module EE
def with_db_config(&block) def with_db_config(&block)
if geo_migration? if geo_migration?
::Gitlab::Geo::DatabaseTasks.with_geo_db { yield } ::Geo::TrackingBase.connected_to(database: :geo) { yield }
else else
yield yield
end end
......
Markdown is supported
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