Sha256: 43eea934db93164248aa5185643751d676628aeab21d69ea4bf9b64e89fd9ffc

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

module Ryakuzu
  class RunMigration
    attr_accessor :old_table, :new_table, :old_column, :new_column
    require 'fileutils'

    def initialize(options = {})
      @old_table  = options[:old_table]
      @new_table  = options[:new_table]
      @old_column = options[:old_column]
      @new_column = options[:new_column]
    end

    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

    def text_migration(type, class_name, text_line)
      "class Change#{type.titleize}#{class_name} < ActiveRecord::Migration
      def change
        #{text_line}
      end
    end"
    end

    private

    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ryakuzu-0.3.0 lib/ryakuzu/services/run_migration.rb
ryakuzu-0.2.6 app/services/ryakuzu/run_migration.rb