Class: RailsForge::Analyzers::DatabaseAnalyzer

Inherits:
BaseAnalyzer
  • Object
show all
Defined in:
lib/railsforge/analyzers/database_analyzer.rb

Overview

DatabaseAnalyzer scans database/schema for issues

Defined Under Namespace

Classes: DatabaseError

Class Method Summary collapse

Methods inherited from BaseAnalyzer

#analyze, #find_rails_app_path, find_rails_app_path

Class Method Details

.analyze(base_path = nil) ⇒ Object

Analyze database

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/railsforge/analyzers/database_analyzer.rb', line 13

def self.analyze(base_path = nil)
  base_path ||= find_rails_app_path
  raise DatabaseError, "Not in a Rails application" unless base_path

  results = []
  schema_file = File.join(base_path, "db", "schema.rb")

  return results unless File.exist?(schema_file)

  content = File.read(schema_file)
  tables = content.scan(/create_table\s+"(\w+)"/).flatten

  tables.each do |table|
    table_section = content[/create_table\s+"#{table}".*?(?=create_table|\z)/m]
    if table_section && table_section.include?("t.datetime")
      results << {
        type: :index,
        table: table,
        issue: "Consider adding index on datetime columns",
        suggestion: "add_index :#{table}, :created_at"
      }
    end
  end

  results
end


40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/railsforge/analyzers/database_analyzer.rb', line 40

def self.print_report(results)
  puts "\nDatabase Analysis"
  puts "-" * 40

  if results.empty?
    puts "✓ No issues found"
    return
  end

  results.each do |result|
    puts "#{result[:table]}: #{result[:issue]}"
  end
end