Skip to content

Commit

Permalink
Merge pull request #8522 from senny/3489_index_names_on_copy
Browse files Browse the repository at this point in the history
Leep index names when using `alter_table` with sqlite3. Closes #3489
  • Loading branch information
carlosantoniodasilva committed Dec 19, 2012
2 parents b204830 + d01f913 commit e36f9ef
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
5 changes: 5 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Rails 4.0.0 (unreleased) ##

* Keep index names when using `alter_table` with sqlite3.
Fix #3489

*Yves Senn*

* Add ability for postgresql adapter to disable user triggers in disable_referential_integrity.
Fix #5523

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,6 @@ def copy_table(from, to, options = {}) #:nodoc:
end
yield @definition if block_given?
end

copy_table_indexes(from, to, options[:rename] || {})
copy_table_contents(from, to,
@definition.columns.map {|column| column.name},
Expand All @@ -560,7 +559,7 @@ def copy_table_indexes(from, to, rename = {}) #:nodoc:

unless columns.empty?
# index name can't be the same
opts = { :name => name.gsub(/_(#{from})_/, "_#{to}_") }
opts = { name: name.gsub(/(^|_)(#{from})_/, "\\1#{to}_") }
opts[:unique] = true if index.unique
add_index(to, columns, opts)
end
Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/migration/rename_column_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@ def test_change_column_with_new_default
refute TestModel.new.administrator?
end

def test_change_column_with_custom_index_name
add_column "test_models", "category", :string
add_index :test_models, :category, name: 'test_models_categories_idx'

assert_equal ['test_models_categories_idx'], connection.indexes('test_models').map(&:name)
change_column "test_models", "category", :string, null: false, default: 'article'

assert_equal ['test_models_categories_idx'], connection.indexes('test_models').map(&:name)
end

def test_change_column_default
add_column "test_models", "first_name", :string
connection.change_column_default "test_models", "first_name", "Tester"
Expand Down

0 comments on commit e36f9ef

Please sign in to comment.