Sha256: 5ede40b4fd2fe2c765036c39f4f9857f3067aef3d0a9cd278776dac8a317f553

Contents?: true

Size: 1.56 KB

Versions: 3

Compression:

Stored size: 1.56 KB

Contents

class <%= migration_name %> < ActiveRecord::Migration
<%
  def options_from_table_column col
    str = [":#{col.type}"] 
    str << ":default => #{col.default}" unless col.default.nil?
    str << ":null => false " unless col.null
    str << ":limit => #{col.limit}" unless col.limit.nil?
    str << ":precision => #{col.precision}" unless col.precision.nil?
    str << ":scale => #{col.scale}" unless col.scale.nil?
    str << ":primary => true" if col.primary
    str.join(', ')
  end

  def options_from_target_column hash
    return if hash.nil?
    " ," + hash.map{|el| el[0]=":#{el[0]}"; el * " => "} * ", " 
  end
%>
  def self.up
    <% removed_columns.each_pair do |col, col_info| %>
      remove_column :<%=table_name%>, :<%=col%>
    <% end %>

    <% added_columns.each_pair do |col, col_info| %>
      add_column :<%=table_name%>, :<%=col%>, :<%=col_info[1]%> <%= options_from_target_column col_info[2] %>
    <% end %>

    <% changed_columns.each_pair do |col, col_info| %>
      change_column :<%=table_name%>, :<%=col%>, :<%=col_info[0][1]%> <%= options_from_target_column col_info[0][2]%>
    <% end %>
  end
  
  def self.down
    <% removed_columns.each do |col, col_info| %>
      add_column :<%=table_name.to_sym%>, :<%= col%>, <%= options_from_table_column col_info%> 
    <% end %>

    <% added_columns.each_pair do |col, col_info| %>
      remove_column :<%=table_name%>, :<%=col%>
    <% end %>

    <% changed_columns.each_pair do |col, col_info| %>
      change_column :<%=table_name%>, :<%=col%>, <%= options_from_table_column col_info[1]%>
    <% end %>
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sga_fields_synchronizer-0.1.3 generators/fields_synchronizer/templates/migration.rb
sga_fields_synchronizer-0.1.2 generators/fields_synchronizer/templates/migration.rb
sga_fields_synchronizer-0.1.1 generators/fields_synchronizer/templates/migration.rb