Class: SchemaDoctor::Analyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/schema_doctor/analyzer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAnalyzer

Returns a new instance of Analyzer.



9
10
11
# File 'lib/schema_doctor/analyzer.rb', line 9

def initialize
  @schema_file = SchemaFile.new
end

Instance Attribute Details

#schema_fileObject (readonly)

Returns the value of attribute schema_file.



7
8
9
# File 'lib/schema_doctor/analyzer.rb', line 7

def schema_file
  @schema_file
end

Instance Method Details

#analyze_schemaObject



13
14
15
16
17
18
19
20
21
# File 'lib/schema_doctor/analyzer.rb', line 13

def analyze_schema
  existing_schema = schema_file.load

  puts "== Analyzing model schema..."
  new_schema = model_schema(existing_schema || {})

  puts "== Exporting Specification files..."
  schema_file.dump(new_schema)
end

#model_schema(schema = {}) ⇒ Object



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
# File 'lib/schema_doctor/analyzer.rb', line 23

def model_schema(schema = {})
  models.each do |model|
    next if model.table_name.blank?

    puts "Processing #{model.name}..."
    schema[model.name.to_sym] =
      {
        name: model.name,
        table_name: model.table_name,
        table_comment: connection.table_comment(model.table_name),
        extra_comment: schema.dig(model.name, :extra_comment),
        columns: columns(model, schema.dig(model.name, :columns) || {}),
        indexes: indexes(model),
        associations: associations(model)
      }
  rescue ActiveRecord::TableNotSpecified
    nil
  rescue => e
    # Skip analyzing if an error occurs
    puts "Failed to process #{model.name}: #{e.inspect}"
    puts "\e[31mWe're sorry, Failed to process \e[33m#{model.name}\e[31m:\n #{e.inspect}\e[0m"
  end

  schema
end