Sha256: 0a39a3824ee335ba286dbecac21772c6f8e4a2962bf093030943a8616a5be700

Contents?: true

Size: 854 Bytes

Versions: 6

Compression:

Stored size: 854 Bytes

Contents

# frozen_string_literal: true

class HeyDoctor::CheckDatabaseHealthService
  class << self
    SUCCESS = {
      message: 'Database is connected',
      success: true
    }.freeze

    MIGRATION_PENDING = {
      message: 'Pending migrations detected',
      success: true
    }.freeze

    ERROR = {
      message: 'Error connecting to database',
      success: false
    }.freeze

    def call
      return ERROR unless connected?
      return MIGRATION_PENDING if needs_migration?

      SUCCESS
    end

    private

    def connected?
      ActiveRecord::Base.connection.execute('select 1')

      true
    rescue ActiveRecord::StatementInvalid, ActiveRecord::NoDatabaseError
      false
    end

    def needs_migration?
      return false unless connected?

      ActiveRecord::Base.connection.migration_context.needs_migration?
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
hey_doctor-1.2.0 app/services/hey_doctor/check_database_health_service.rb
hey_doctor-1.1.3.pre.rc2 app/services/hey_doctor/check_database_health_service.rb
hey_doctor-1.1.2 app/services/hey_doctor/check_database_health_service.rb
hey_doctor-1.1.1 app/services/hey_doctor/check_database_health_service.rb
hey_doctor-1.1.0 app/services/hey_doctor/check_database_health_service.rb
hey_doctor-1.0.0 app/services/hey_doctor/check_database_health_service.rb