Class: Crystalball::Rails::Predictor::ModifiedSchema

Inherits:
Object
  • Object
show all
Includes:
Predictor::Helpers::AffectedExampleGroupsDetector
Defined in:
lib/crystalball/rails/predictor/modified_schema.rb

Overview

Used with ‘predictor.use Crystalball::Rails::Predictor::ModifiedSchema.new(tables_map_path:)`. When used will check db/schema.rb for changes and add specs which depend on files affected by changed tables

Constant Summary collapse

SCHEMA_PATH =
'db/schema.rb'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Predictor::Helpers::AffectedExampleGroupsDetector

#detect_examples

Constructor Details

#initialize(tables_map_path:) ⇒ ModifiedSchema

Returns a new instance of ModifiedSchema.

Parameters:

  • tables_map_path (String)
    • path to generated TablesMap



19
20
21
# File 'lib/crystalball/rails/predictor/modified_schema.rb', line 19

def initialize(tables_map_path:)
  @tables_map_path = tables_map_path
end

Instance Attribute Details

#tables_map_pathObject (readonly)

Returns the value of attribute tables_map_path.



16
17
18
# File 'lib/crystalball/rails/predictor/modified_schema.rb', line 16

def tables_map_path
  @tables_map_path
end

Instance Method Details

#call(diff, map) ⇒ Array<String>

Returns the spec paths associated with the changes.

Parameters:

Returns:

  • (Array<String>)

    the spec paths associated with the changes



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

def call(diff, map)
  return [] if schema_diff(diff).nil?

  old_schema = old_schema(diff)
  new_schema = new_schema(diff)

  changed_tables = changed_tables(old_schema, new_schema)

  files = changed_tables.flat_map do |table_name|
    files = tables_map[table_name]
    Crystalball.log :warn, "There are no model files for changed table `#{table_name}`. Check https://github.com/toptal/crystalball#warning for detailed description" unless files&.any?
    files
  end.compact
  detect_examples(files, map)
end

#tables_mapCrystalball::Rails::TablesMap



45
46
47
# File 'lib/crystalball/rails/predictor/modified_schema.rb', line 45

def tables_map
  @tables_map ||= MapStorage::YAMLStorage.load(Pathname(tables_map_path))
end