diff options
author | Yves Senn <yves.senn@gmail.com> | 2012-12-28 22:56:44 +0100 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2013-02-20 18:19:25 +0100 |
commit | 39eef1a565ef02e4dabc0811ef1bf4547ff9a60e (patch) | |
tree | cc528b7c8edb014e65bc9c4e1c5e8dec4d89993d /activerecord/test/cases/migration | |
parent | 20ed3e0f715741ca53c992bcdf87b5039e6692af (diff) | |
download | rails-39eef1a565ef02e4dabc0811ef1bf4547ff9a60e.tar.gz rails-39eef1a565ef02e4dabc0811ef1bf4547ff9a60e.tar.bz2 rails-39eef1a565ef02e4dabc0811ef1bf4547ff9a60e.zip |
also rename indexes when a table or column is renamed
When a table or a column is renamed related indexes kept their name. This will lead to confusing names. This patch renames related indexes when a column or a table is renamed. Only indexes with names generated by rails will be renamed. Indexes with custom names will not be renamed.
Diffstat (limited to 'activerecord/test/cases/migration')
-rw-r--r-- | activerecord/test/cases/migration/rename_column_test.rb | 25 | ||||
-rw-r--r-- | activerecord/test/cases/migration/rename_table_test.rb | 12 |
2 files changed, 34 insertions, 3 deletions
diff --git a/activerecord/test/cases/migration/rename_column_test.rb b/activerecord/test/cases/migration/rename_column_test.rb index b116753d04..88bea2211d 100644 --- a/activerecord/test/cases/migration/rename_column_test.rb +++ b/activerecord/test/cases/migration/rename_column_test.rb @@ -86,8 +86,29 @@ module ActiveRecord assert_equal 1, connection.indexes('test_models').size rename_column "test_models", "hat_name", "name" - # FIXME: should we rename the index if it's name was autogenerated by rails? - assert_equal ['index_test_models_on_hat_name'], connection.indexes('test_models').map(&:name) + + assert_equal ['index_test_models_on_name'], connection.indexes('test_models').map(&:name) + end + + def test_rename_column_with_multi_column_index + add_column "test_models", :hat_size, :integer + add_column "test_models", :hat_style, :string, limit: 100 + add_index "test_models", ["hat_style", "hat_size"], unique: true + + rename_column "test_models", "hat_size", 'size' + assert_equal ['index_test_models_on_hat_style_and_size'], connection.indexes('test_models').map(&:name) + + rename_column "test_models", "hat_style", 'style' + assert_equal ['index_test_models_on_style_and_size'], connection.indexes('test_models').map(&:name) + end + + def test_rename_column_does_not_rename_custom_named_index + add_column "test_models", :hat_name, :string + add_index :test_models, :hat_name, :name => 'idx_hat_name' + + assert_equal 1, connection.indexes('test_models').size + rename_column "test_models", "hat_name", "name" + assert_equal ['idx_hat_name'], connection.indexes('test_models').map(&:name) end def test_remove_column_with_index diff --git a/activerecord/test/cases/migration/rename_table_test.rb b/activerecord/test/cases/migration/rename_table_test.rb index 21901bec3c..22dbd7c38b 100644 --- a/activerecord/test/cases/migration/rename_table_test.rb +++ b/activerecord/test/cases/migration/rename_table_test.rb @@ -63,7 +63,17 @@ module ActiveRecord connection.enable_identity_insert("octopi", false) if current_adapter?(:SybaseAdapter) assert_equal 'http://www.foreverflying.com/octopus-black7.jpg', connection.select_value("SELECT url FROM octopi WHERE id=1") - assert connection.indexes(:octopi).first.columns.include?("url") + index = connection.indexes(:octopi).first + assert index.columns.include?("url") + assert_equal 'index_octopi_on_url', index.name + end + + def test_rename_table_does_not_rename_custom_named_index + add_index :test_models, :url, name: 'special_url_idx' + + rename_table :test_models, :octopi + + assert_equal ['special_url_idx'], connection.indexes(:octopi).map(&:name) end def test_rename_table_for_postgresql_should_also_rename_default_sequence |