Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Port of #5522 'Fix adding/removing field's index when generating migration' #5542

Merged
merged 1 commit into from

2 participants

Marcelo Silveira Piotr Sarnacki
Marcelo Silveira

This is just a port of #5522 to 3-2-stable with the minor fix from #5540 already applied.

Cheers!

/cc @josevalim @travisjeffery

Piotr Sarnacki drogus merged commit 89f8866 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 21, 2012
  1. Marcelo Silveira
This page is out of date. Refresh to see the latest.
3  activerecord/lib/rails/generators/active_record/migration/templates/migration.rb
View
@@ -24,6 +24,9 @@ def down
<% attributes.reverse.each do |attribute| -%>
<%- if migration_action -%>
<%= migration_action == 'add' ? 'remove' : 'add' %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'remove' %>, :<%= attribute.type %><%= attribute.inject_options %><% end %>
+ <%- if attribute.has_index? && migration_action == 'remove' -%>
+ add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
+ <%- end -%>
<%- end -%>
<%- end -%>
end
18 railties/test/generators/migration_generator_test.rb
View
@@ -42,6 +42,24 @@ def test_add_migration_with_attributes
end
end
+ def test_remove_migration_with_indexed_attribute
+ migration = "remove_title_body_from_posts"
+ run_generator [migration, "title:string:index", "body:text"]
+
+ assert_migration "db/migrate/#{migration}.rb" do |content|
+ assert_method :up, content do |up|
+ assert_match(/remove_column :posts, :title/, up)
+ assert_match(/remove_column :posts, :body/, up)
+ end
+
+ assert_method :down, content do |down|
+ assert_match(/add_column :posts, :title, :string/, down)
+ assert_match(/add_column :posts, :body, :text/, down)
+ assert_match(/add_index :posts, :title/, down)
+ end
+ end
+ end
+
def test_remove_migration_with_attributes
migration = "remove_title_body_from_posts"
run_generator [migration, "title:string", "body:text"]
Something went wrong with that request. Please try again.