Sha256: a03ec3d8e2f3b58543e2d5e91b385779e7611773fba40bf52404c3b6ea3dd05e

Contents?: true

Size: 1.99 KB

Versions: 11

Compression:

Stored size: 1.99 KB

Contents

class <%= @activity.camelize.gsub(/\s/, '') %> < ActiveRecord::Migration
  def self.up
  <% @columns[:added].each do |field, options| %>
    add_column :<%= @table_name %>, :<%= field %>, :<%= options.delete(:type) %><%= (options.blank?)? '': ", "+options.inspect[1..-2] %>
  <% end -%>
  <% @columns[:changed].each do |field, options, old_options| %>
    change_column :<%= @table_name %>, :<%= field %>, :<%= options.delete(:type) %><%= (options.blank?)? '': ", "+options.inspect[1..-2] %>
  <% end -%>
  <% @columns[:transferred].each do |source, target| %>
    puts "-- copy data from :<%= source %> to :<%= target %>"
    <%= @table_name.classify %>.update_all("<%= target %> = <%= source %>")
  <% end -%>  
  <% @columns[:renamed].each do |old_name, new_name| %>
    rename_column :<%= @table_name %>, :<%= old_name %>, :<%= new_name %>
  <% end -%>
  <% @columns[:deleted].each do |field, options| %>
    remove_column :<%= @table_name %>, :<%= field %>
  <% end -%>  
  <% @indexes.each do |index, options| %>
    add_index :<%= @table_name %>, <%= index.inspect %>
  <% end -%>
  end
  
  def self.down
  <% @columns[:deleted].each do |field, options| %>
    add_column :<%= @table_name %>, :<%= field %>, :<%= options.delete(:type) %><%= (options.blank?)? '': ", "+options.inspect[1..-2] %>
  <% end -%>  
  <% @columns[:renamed].each do |old_name, new_name| %>
    rename_column :<%= @table_name %>, :<%= new_name %>, :<%= old_name %>
  <% end -%>    
  <% @columns[:transferred].each do |source, target| %>
    puts "-- copy data from :<%= target %> to :<%= source %>"    
    <%= @table_name.classify %>.update_all("<%= source %> = <%= target %>")
  <% end -%>  
  <% @columns[:changed].each do |field, options, old_options| %>
    change_column :<%= @table_name %>, :<%= field %>, :<%= old_options.delete(:type) %><%= (old_options.blank?)? '': ", "+old_options.inspect[1..-2] %>
  <% end -%>
  <% @columns[:added].each do |field, options| %>
    remove_column :<%= @table_name %>, :<%= field %>
  <% end -%>
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
migrant-1.5.0 lib/generators/templates/change_migration.erb
migrant-1.4.3 lib/generators/templates/change_migration.erb
migrant-1.4.1 lib/generators/templates/change_migration.erb
migrant-1.4.0 lib/generators/templates/change_migration.erb
migrant-1.3.2 lib/generators/templates/change_migration.erb
migrant-1.3.1 lib/generators/templates/change_migration.erb
migrant-1.3.0 lib/generators/templates/change_migration.erb
migrant-1.2.8 lib/generators/templates/change_migration.erb
migrant-1.2.7 lib/generators/templates/change_migration.erb
migrant-1.2.5 lib/generators/templates/change_migration.erb
migrant-1.2.2 lib/generators/templates/change_migration.erb