diff options
author | José Valim <jose.valim@gmail.com> | 2012-03-21 00:25:02 -0700 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2012-03-21 00:25:02 -0700 |
commit | 2884567e55c6a00ae6b744599f6c57fb60e6051b (patch) | |
tree | b07814dd9aab2618ad07395c542bcad37a48a2e6 | |
parent | b49a7ddce18a35a39fd5b3f6003d4a02cbd09b0e (diff) | |
parent | b2a59388b2ad281ccce1f72dd5fda09ca746dc32 (diff) | |
download | rails-2884567e55c6a00ae6b744599f6c57fb60e6051b.tar.gz rails-2884567e55c6a00ae6b744599f6c57fb60e6051b.tar.bz2 rails-2884567e55c6a00ae6b744599f6c57fb60e6051b.zip |
Merge pull request #5522 from travisjeffery/fix_migration_generator_adding_removing_index
Fix adding/removing field's index when generating migration
-rw-r--r-- | activerecord/lib/rails/generators/active_record/migration/templates/migration.rb | 3 | ||||
-rw-r--r-- | railties/test/generators/migration_generator_test.rb | 18 |
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 d084a00ed7..43fa956c85 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 4e08e5dae1..fd84164340 100644 --- a/railties/test/generators/migration_generator_test.rb +++ b/railties/test/generators/migration_generator_test.rb @@ -41,6 +41,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"] |