Class: RailsLens::Analyzers::CompositeKeys

Inherits:
Base
  • Object
show all
Defined in:
lib/rails_lens/analyzers/composite_keys.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
# File 'lib/rails_lens/analyzers/composite_keys.rb', line 6

def analyze
  # First try Rails native support
  if model_class.respond_to?(:primary_keys) && model_class.primary_keys.is_a?(Array)
    keys = model_class.primary_keys
    return format_composite_keys(keys) if keys.length > 1
  end

  # For PostgreSQL, check the actual database constraints
  if adapter_name == 'PostgreSQL'
    keys = detect_composite_primary_key_from_db
    return format_composite_keys(keys) if keys && keys.length > 1
  end

  nil
rescue NoMethodError => e
  RailsLens.logger.debug { "Failed to analyze composite keys for #{model_class.name}: #{e.message}" }
  nil
rescue ActiveRecord::ConnectionNotEstablished => e
  RailsLens.logger.debug { "No database connection for #{model_class.name}: #{e.message}" }
  nil
end