Skip to content

Commit

Permalink
added index and existing records update to migration
Browse files Browse the repository at this point in the history
  • Loading branch information
7even committed Sep 24, 2011
1 parent 141a4fb commit 553fccf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/generators/zodiac/migration/migration_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def migration_classname
"Add#{sign_attribute.camelcase}To#{model_name.gsub('::', '').camelcase.pluralize}"
end

def model_classname
model_name.camelcase.singularize
end

def table_name
model_name.underscore.gsub('/', '_').pluralize
end
Expand Down
8 changes: 8 additions & 0 deletions lib/generators/zodiac/migration/templates/migration30.rb.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
<%# migration for ActiveRecord 3.0 -%>
class <%= migration_classname %> < ActiveRecord::Migration
def self.up
add_column :<%= table_name %>, :<%= sign_attribute.underscore %>, :integer
add_index :<%= table_name %>, :<%= sign_attribute.underscore %>

<%= model_classname %>.all.each do |<%= model_name.underscore.singularize %>|
<%= model_name.underscore.singularize %>.send(:update_sign_id)
<%= model_name.underscore.singularize %>.save
end
end

def self.down
remove_index :<%= table_name %>, :<%= sign_attribute.underscore %>
remove_column :<%= table_name %>, :<%= sign_attribute.underscore %>
end
end
7 changes: 7 additions & 0 deletions lib/generators/zodiac/migration/templates/migration31.rb.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<%# migration for ActiveRecord 3.1 and higher -%>
class <%= migration_classname %> < ActiveRecord::Migration
def change
add_column :<%= table_name %>, :<%= sign_attribute.underscore %>, :integer
add_index :<%= table_name %>, :<%= sign_attribute.underscore %>

<%= model_classname %>.all.each do |<%= model_name.underscore.singularize %>|
<%= model_name.underscore.singularize %>.send(:update_sign_id)
<%= model_name.underscore.singularize %>.save
end
end
end

0 comments on commit 553fccf

Please sign in to comment.