Class: ActiveRecordDoctor::AddIndexesGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/active_record_doctor/add_indexes/add_indexes_generator.rb

Overview

Generate migrations that add missing indexes to the database.

Instance Method Summary collapse

Instance Method Details

#create_migrationsObject



9
10
11
12
13
14
15
16
17
18
# File 'lib/generators/active_record_doctor/add_indexes/add_indexes_generator.rb', line 9

def create_migrations
  migration_descriptions = read_migration_descriptions(path)
  now = Time.now

  migration_descriptions.each_with_index do |(table, columns), index|
    timestamp = (now + index).strftime("%Y%m%d%H%M%S")
    file_name = "db/migrate/#{timestamp}_index_foreign_keys_in_#{table}.rb"
    create_file(file_name, content(table, columns).tap { |x| puts x })
  end
end