Skip to content
This repository
Browse code

[3-2-stable] Port of #5522 'Fix adding/removing field's index when ge…

…nerating migration'
  • Loading branch information...
commit 35bf748cd253610b0ee539b3f1f192779405886b 1 parent f829515
Marcelo Silveira authored March 21, 2012
3  activerecord/lib/rails/generators/active_record/migration/templates/migration.rb
@@ -24,6 +24,9 @@ def down
24 24
 <% attributes.reverse.each do |attribute| -%>
25 25
   <%- if migration_action -%>
26 26
     <%= migration_action == 'add' ? 'remove' : 'add' %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'remove' %>, :<%= attribute.type %><%= attribute.inject_options %><% end %>
  27
+    <%- if attribute.has_index? && migration_action == 'remove' -%>
  28
+    add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
  29
+    <%- end -%>
27 30
   <%- end -%>
28 31
 <%- end -%>
29 32
   end
18  railties/test/generators/migration_generator_test.rb
@@ -42,6 +42,24 @@ def test_add_migration_with_attributes
42 42
     end
43 43
   end
44 44
 
  45
+  def test_remove_migration_with_indexed_attribute
  46
+    migration = "remove_title_body_from_posts"
  47
+    run_generator [migration, "title:string:index", "body:text"]
  48
+
  49
+    assert_migration "db/migrate/#{migration}.rb" do |content|
  50
+      assert_method :up, content do |up|
  51
+        assert_match(/remove_column :posts, :title/, up)
  52
+        assert_match(/remove_column :posts, :body/, up)
  53
+      end
  54
+
  55
+      assert_method :down, content do |down|
  56
+        assert_match(/add_column :posts, :title, :string/, down)
  57
+        assert_match(/add_column :posts, :body, :text/, down)
  58
+        assert_match(/add_index :posts, :title/, down)
  59
+      end
  60
+    end
  61
+  end
  62
+
45 63
   def test_remove_migration_with_attributes
46 64
     migration = "remove_title_body_from_posts"
47 65
     run_generator [migration, "title:string", "body:text"]

0 notes on commit 35bf748

Please sign in to comment.
Something went wrong with that request. Please try again.