geo_database_configured_check.rb 1.66 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5 6 7 8
module SystemCheck
  module Geo
    class GeoDatabaseConfiguredCheck < SystemCheck::BaseCheck
      set_name 'GitLab Geo secondary database is correctly configured'
      set_skip_reason 'not a secondary node'

9 10 11 12
      WRONG_CONFIGURATION_MESSAGE = 'Check if you enabled the `geo_secondary_role` or `geo_postgresql` in the gitlab.rb config file.'.freeze
      UNHEALTHY_CONNECTION_MESSAGE = 'Check the tracking database configuration as the connection could not be established'.freeze
      NO_TABLES_MESSAGE = 'Run the tracking database migrations: gitlab-rake geo:db:migrate'.freeze

13 14 15 16
      def skip?
        !Gitlab::Geo.secondary?
      end

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
      def multi_check
        unless Gitlab::Geo.geo_database_configured?
          $stdout.puts 'no'.color(:red)

          try_fixing_it(WRONG_CONFIGURATION_MESSAGE)

          for_more_information('doc/gitlab-geo/database.md')

          return false
        end

        unless connection_healthy?
          $stdout.puts 'no'.color(:red)

          try_fixing_it(UNHEALTHY_CONNECTION_MESSAGE)

          for_more_information('doc/gitlab-geo/database.md')

          return false
        end

        unless tables_present?
          $stdout.puts 'no'.color(:red)

          try_fixing_it(NO_TABLES_MESSAGE)

          for_more_information('doc/gitlab-geo/database.md')

          return false
        end

        $stdout.puts 'yes'.color(:green)
        true
50 51
      end

52 53 54 55 56
      private

      def connection_healthy?
        ::Geo::TrackingBase.connection.active?
      end
57

58 59
      def tables_present?
        Gitlab::Geo::DatabaseTasks.with_geo_db { !ActiveRecord::Migrator.needs_migration? }
60 61 62 63
      end
    end
  end
end