aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPiotr Sarnacki <drogus@gmail.com>2012-03-21 18:23:19 -0700
committerPiotr Sarnacki <drogus@gmail.com>2012-03-21 18:23:19 -0700
commit89f88669bfbdc7987032c2716c87faefb8fe4a8e (patch)
tree91a74303ee8182d8d4a290ce3fb9f279d9cd6d37
parentf829515ad9ca386bf1ae7f2dc342ee3d883ab3be (diff)
parent35bf748cd253610b0ee539b3f1f192779405886b (diff)
downloadrails-89f88669bfbdc7987032c2716c87faefb8fe4a8e.tar.gz
rails-89f88669bfbdc7987032c2716c87faefb8fe4a8e.tar.bz2
rails-89f88669bfbdc7987032c2716c87faefb8fe4a8e.zip
Merge pull request #5542 from mhfs/port_5522_to_32stable
Port of #5522 'Fix adding/removing field's index when generating migration'
-rw-r--r--activerecord/lib/rails/generators/active_record/migration/templates/migration.rb3
-rw-r--r--railties/test/generators/migration_generator_test.rb18
2 files changed, 21 insertions, 0 deletions
diff --git a/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb b/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb
index aec5c429fe..9ca63a209e 100644
--- a/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb
+++ b/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb
@@ -24,6 +24,9 @@ class <%= migration_class_name %> < ActiveRecord::Migration
<% 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
diff --git a/railties/test/generators/migration_generator_test.rb b/railties/test/generators/migration_generator_test.rb
index 8d49faacf1..16e4ac0881 100644
--- a/railties/test/generators/migration_generator_test.rb
+++ b/railties/test/generators/migration_generator_test.rb
@@ -42,6 +42,24 @@ class MigrationGeneratorTest < Rails::Generators::TestCase
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"]