Class: LolDba::MigrationFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/lol_dba/index_finding/migration_formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(indexes) ⇒ MigrationFormatter

Returns a new instance of MigrationFormatter.



3
4
5
# File 'lib/lol_dba/index_finding/migration_formatter.rb', line 3

def initialize(indexes)
  @indexes = indexes
end

Instance Method Details

#format_for_migration(missing_indexes) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/lol_dba/index_finding/migration_formatter.rb', line 16

def format_for_migration(missing_indexes)
  add = []
  missing_indexes.each do |table_name, keys_to_add|
    keys_to_add.each do |key|
      next if key.blank?
      add << format_index(table_name, key)
    end
  end
  add
end

#format_index(table_name, key) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/lol_dba/index_finding/migration_formatter.rb', line 27

def format_index(table_name, key)
  if key.is_a?(Array)
    keys = key.collect { |col| ":#{col}" }
    "add_index :#{table_name}, [#{keys.join(', ')}]"
  else
    "add_index :#{table_name}, :#{key}"
  end
end

#migration_instructions(formated_indexes) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/lol_dba/index_finding/migration_formatter.rb', line 36

def migration_instructions(formated_indexes)
  "* TIP: if you have a problem with the index name('index name too long'), you can\nsolve with the :name option. Something like :name => 'my_index'.\n* run `rails g migration AddMissingIndexes` and add the following content:\n\nclass AddMissingIndexes < ActiveRecord::Migration\n  def change\n    \#{formated_indexes.sort.uniq.join(\"\\n        \")}\n  end\nend\n"
end

#puts_migration_contentObject



7
8
9
10
11
12
13
14
# File 'lib/lol_dba/index_finding/migration_formatter.rb', line 7

def puts_migration_content
  formated_indexes = format_for_migration(@indexes)
  if formated_indexes.blank?
    puts 'Yey, no missing indexes found!'
  else
    puts migration_instructions(formated_indexes)
  end
end