Class: Idxfence::Checker

Inherits:
Object
  • Object
show all
Defined in:
lib/idxfence/checker.rb

Overview

Joins the two passes: every uniqueness validation ModelParser found in app/models/*.rb, resolved to a table name (via Inflector.table_name_for, unless the model set self.table_name explicitly), against every unique index SchemaParser found in db/schema.rb for that same table. A validation with no matching unique index -- one covering exactly its column set (the validated column plus any scope: column(s), unordered) -- is a real gap: the AR validation can be raced, and only a DB-level unique index closes it. See DETAILS.md for the full algorithm and its documented limitations (case-insensitive uniqueness, irregular pluralization, etc).

Instance Method Summary collapse

Constructor Details

#initialize(model_paths:, schema_path:) ⇒ Checker



19
20
21
22
# File 'lib/idxfence/checker.rb', line 19

def initialize(model_paths:, schema_path:)
  @model_paths = model_paths
  @schema_path = schema_path
end

Instance Method Details

#call ⇒ Array<Finding>, String



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/idxfence/checker.rb', line 28

def call
  begin
    schema_tables = SchemaParser.parse(schema_path)
  rescue SchemaParser::MissingSchemaError => e
    return [[], "idxfence: warning: #{e.message} -- skipping uniqueness cross-check for #{model_paths.size} model file(s)"]
  end

  findings = model_paths.flat_map do |model_path|
    ModelParser.new(path: model_path).call.flat_map do |parsed_model|
      check_model(parsed_model, schema_tables)
    end
  end

  [findings, nil]
end