Sha256: 74bd4cc47a4f78472caf7ca2c8fd6791efe96d1c3d17b087f54b0b6b3552420e

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 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
    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

1 entries across 1 versions & 1 rubygems

Version Path
sga_fields_synchronizer-0.1.0 generators/fields_synchronizer/templates/migration.rb