Class: LegacyData::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/legacy_data/schema.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name) ⇒ Schema

Returns a new instance of Schema.



34
35
36
# File 'lib/legacy_data/schema.rb', line 34

def initialize(table_name)
  @table_name        = table_name
end

Instance Attribute Details

#table_nameObject (readonly)

Returns the value of attribute table_name.



3
4
5
# File 'lib/legacy_data/schema.rb', line 3

def table_name
  @table_name
end

Class Method Details

.analyze(options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/legacy_data/schema.rb', line 5

def self.analyze(options={})
  analyzed_schema = []

  @tables = {}
  if options[:table_name]
    @tables[options[:table_name]] = :pending
  else
    LegacyData::Schema.tables.each {|table| @tables[table] = :pending }
  end
  
  while table_name = next_table_to_process
    # puts "      Tables: #{@tables.inspect}"
    @tables[table_name] = analyze_table(table_name)

    [:has_some, :belongs_to].each do |relation_type| 
      associated_tables = @tables[table_name][:relations][relation_type].keys.map(&:to_s) 
      associated_tables.each {|associated_table| @tables[associated_table] = :pending if @tables[associated_table].nil? }
    end
  end
  @tables.values
end

.analyze_table(table_name) ⇒ Object



26
27
28
# File 'lib/legacy_data/schema.rb', line 26

def self.analyze_table table_name
  new(table_name).analyze_table
end

.next_table_to_processObject



30
31
32
# File 'lib/legacy_data/schema.rb', line 30

def self.next_table_to_process
  @tables.keys.detect {|table_name| @tables[table_name] == :pending }
end

.tables(name_pattern = /.*/) ⇒ Object



48
49
50
# File 'lib/legacy_data/schema.rb', line 48

def self.tables name_pattern=/.*/
  connection.tables.select {|table_name| table_name =~ name_pattern }.sort
end

Instance Method Details

#analyze_tableObject



38
39
40
41
42
43
44
45
46
# File 'lib/legacy_data/schema.rb', line 38

def analyze_table
  puts "analyzing #{table_name} => #{class_name}"
  { :table_name   => table_name,
    :class_name   => class_name,
    :primary_key  => primary_key,
    :relations    => relations,
    :constraints  => constraints
  }
end

#belongs_to_relationsObject



67
68
69
70
71
72
73
74
75
# File 'lib/legacy_data/schema.rb', line 67

def belongs_to_relations
  return [] unless connection.respond_to? :foreign_keys_for
  
  belongs_to = {}
  connection.foreign_keys_for(table_name).each do |relation|
    belongs_to[relation.first.downcase] = relation.second.downcase.to_sym
  end
  belongs_to
end

#class_nameObject



52
53
54
# File 'lib/legacy_data/schema.rb', line 52

def class_name
  TableClassNameMapper.class_name_for(self.table_name)
end

#constraintsObject



86
87
88
89
90
91
92
93
# File 'lib/legacy_data/schema.rb', line 86

def constraints
  unique, multi_column_unique = unique_constraints.partition {|columns| columns.size == 1}
  { :unique              => unique,
    :multi_column_unique => multi_column_unique,
    :non_nullable        => non_nullable_constraints,
    :custom              => custom_constraints
  }
end

#custom_constraintsObject



104
105
106
107
108
109
110
111
# File 'lib/legacy_data/schema.rb', line 104

def custom_constraints
  return [] unless connection.respond_to? :constraints
  user_constraints = {}
  connection.constraints(table_name).each do |constraint|
    user_constraints[constraint.first.underscore.to_sym] = constraint.second
  end
  user_constraints
end

#has_some_relationsObject



76
77
78
79
80
81
82
83
84
# File 'lib/legacy_data/schema.rb', line 76

def has_some_relations
  return [] unless connection.respond_to? :foreign_keys_of

  has_some = {}
  connection.foreign_keys_of(table_name).each do |relation|
    has_some[relation.first.downcase] = relation.second.downcase.to_sym
  end
  has_some
end

#non_nullable_constraintsObject



95
96
97
98
# File 'lib/legacy_data/schema.rb', line 95

def non_nullable_constraints
  non_nullable_constraints = connection.columns(table_name, "#{table_name} Columns").reject(&:null).map(&:name)
  non_nullable_constraints.reject {|col| col == primary_key}
end

#primary_keyObject



56
57
58
59
# File 'lib/legacy_data/schema.rb', line 56

def primary_key
  pk_and_sequence_for = connection.pk_and_sequence_for(table_name)
  pk_and_sequence_for.first if pk_and_sequence_for.respond_to? :first
end

#relationsObject



61
62
63
64
65
# File 'lib/legacy_data/schema.rb', line 61

def relations
  { :belongs_to   => belongs_to_relations,
    :has_some     => has_some_relations
  }
end

#unique_constraintsObject



100
101
102
# File 'lib/legacy_data/schema.rb', line 100

def unique_constraints
  connection.indexes(table_name).select(&:unique).map(&:columns)
end