class Indexter::Validator

Constants

DEFAULT_EXCLUSIONS
DEFAULT_FORMATTER
DEFAULT_SUFFIXES

Attributes

exclusions[R]
formatter[R]
results[R]
suffixes[R]

Public Class Methods

new(config: nil) click to toggle source

——————– Instance Methods ——————–

# File lib/indexter/validator.rb, line 13
def initialize(config: nil)
  configure(config)
end

Public Instance Methods

validate() click to toggle source
# File lib/indexter/validator.rb, line 17
def validate
  missing = missing_indexes(tables)
  output  = build_results(missing)

  results = formatter.new.format(output)
end

Private Instance Methods

build_results(missing) click to toggle source
# File lib/indexter/validator.rb, line 77
def build_results(missing)
  {
    suffixes:   @suffixes,
    exclusions: @exclusions,
    missing:    missing
  }
end
configure(config) click to toggle source
# File lib/indexter/validator.rb, line 26
def configure(config)
  config = config || NullConfig.new

  format      = config.format     || DEFAULT_FORMATTER
  @formatter  = find_formatter(format: format)

  @exclusions = config.exclusions || DEFAULT_EXCLUSIONS
  @suffixes   = config.suffixes   || DEFAULT_SUFFIXES
end
find_formatter(format: nil) click to toggle source
# File lib/indexter/validator.rb, line 36
def find_formatter(format: nil)
  format     = format || DEFAULT_FORMATTER
  klass_name = "Indexter::Formatters::#{format.to_s.camelize}"
  klass      = klass_name.constantize
rescue NameError
  # If an un-known formatter is passed here, fall back to the hash
  Indexter::Formatters::Hash
end
id_columns(table) click to toggle source

These are the columns we expect to have an index on that end in COL_SUFFIX

# File lib/indexter/validator.rb, line 64
def id_columns(table)
  ActiveRecord::Base.connection.columns(table).select do |column|
    column.name.end_with? *@suffixes
  end.map(&:name)
end
indexes(table) click to toggle source

These are the columns we have indexes on that also end in COL_SUFFIX

# File lib/indexter/validator.rb, line 71
def indexes(table)
  ActiveRecord::Base.connection.indexes(table).map do |idx_def|
    idx_def.columns.select { |col| col.end_with? *@suffixes }
  end.flatten
end
missing_indexes(tbls) click to toggle source
# File lib/indexter/validator.rb, line 45
def missing_indexes(tbls)
  # Check the intersection between what we expect to have indexes on and what we actually have
  # indexes on. If the set is not empty, we might be missing an index
  result = tbls.inject({}) do |acc, table|
    acc[table] = (id_columns(table) - indexes(table))
    acc
  end

  # Reject any tables that have empty results
  result.delete_if { |_, missing| missing.empty? }
end
tables() click to toggle source

Returns a list of all the tables in the database that are analysable

# File lib/indexter/validator.rb, line 58
def tables
  func = ActiveRecord::Base.connection.respond_to?(:data_sources) ? :data_sources : :tables
  ActiveRecord::Base.connection.send(func) - @exclusions.keys
end