class Ryakuzu::RunMigration

Attributes

new_column[RW]
new_table[RW]
old_column[RW]
old_table[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/ryakuzu/services/run_migration.rb, line 6
def initialize(options = {})
  @old_table  = options[:old_table]
  @new_table  = options[:new_table]
  @old_column = options[:old_column]
  @new_column = options[:new_column]
end

Public Instance Methods

call(migration, text_line, type) click to toggle source
# File lib/ryakuzu/services/run_migration.rb, line 13
def call(migration, text_line, type)
  date = DateTime.now.to_s(:number)
  class_name   = migration.classify
  migrate_name = "#{date}_change_#{type}_#{class_name.underscore}.rb"
  text = text_migration(type, class_name, text_line)

  migration(migrate_name, text, class_name)
end
text_migration(type, class_name, text_line) click to toggle source
# File lib/ryakuzu/services/run_migration.rb, line 22
def text_migration(type, class_name, text_line)
  "class Change#{type.titleize}#{class_name} < ActiveRecord::Migration
  def change
    #{text_line}
  end
end"
end

Private Instance Methods

migration(migrate, text_migration, klass) click to toggle source
# File lib/ryakuzu/services/run_migration.rb, line 32
def migration(migrate, text_migration, klass)
  output = File.new("./db/migrate/#{migrate}", 'a+')
  output << text_migration
  output.close
  result = system 'rake db:migrate'
  File.delete(output)
  fail StandardError if result == false
rescue StandardError
  "Cannot drop table, maybe it has reference to other tables? Find #{klass.singularize.downcase}_id in other tables and remove it."
end