Class: RailsLens::Analyzers::DatabaseConstraints

Inherits:
Base
  • Object
show all
Defined in:
lib/rails_lens/analyzers/database_constraints.rb

Instance Attribute Summary

Attributes inherited from Base

#model_class

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from ErrorHandling

#safe_analyze

Constructor Details

This class inherits a constructor from RailsLens::Analyzers::Base

Instance Method Details

#analyzeObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rails_lens/analyzers/database_constraints.rb', line 6

def analyze
  return nil unless connection.respond_to?(:check_constraints)

  constraints = []

  # Get check constraints
  check_constraints = connection.check_constraints(table_name)
  return nil if check_constraints.empty?

  constraints << '[check_constraints]'
  formatted = check_constraints.map do |constraint|
    name = constraint.options[:name] || constraint.name
    expression = constraint.expression || constraint.options[:validate]
    "{ name = \"#{name}\", expr = \"#{expression.to_s.gsub('"', '\\"')}\" }"
  end
  constraints << "constraints = [#{formatted.join(', ')}]"

  constraints.empty? ? nil : constraints.join("\n")
rescue ActiveRecord::StatementInvalid => e
  RailsLens.logger.debug { "Failed to fetch check constraints for #{table_name}: #{e.message}" }
  nil
rescue NoMethodError => e
  RailsLens.logger.debug { "Check constraints not supported by adapter: #{e.message}" }
  nil
end