Skip to content
Browse files

SQLite: fix rename_ and remove_column for columns with unique indexes…

…. Closes #10576.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8453 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent f16cd72 commit b72763a96f0c936699d0f211be553f8f75accd07 @jeremy jeremy committed Dec 20, 2007
View
2 activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* SQLite: fix rename_ and remove_column for columns with unique indexes. #10576 [Brandon Keepers]
+
* Ruby 1.9 compatibility. [Jeremy Kemper]
View
19 activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
@@ -304,13 +304,13 @@ def copy_table(from, to, options = {}) #:nodoc:
yield @definition if block_given?
end
- copy_table_indexes(from, to)
+ copy_table_indexes(from, to, options[:rename] || {})
copy_table_contents(from, to,
@definition.columns.map {|column| column.name},
options[:rename] || {})
end
- def copy_table_indexes(from, to) #:nodoc:
+ def copy_table_indexes(from, to, rename = {}) #:nodoc:
indexes(from).each do |index|
name = index.name
if to == "altered_#{from}"
@@ -319,10 +319,17 @@ def copy_table_indexes(from, to) #:nodoc:
name = name[5..-1]
end
- # index name can't be the same
- opts = { :name => name.gsub(/_(#{from})_/, "_#{to}_") }
- opts[:unique] = true if index.unique
- add_index(to, index.columns, opts)
+ to_column_names = columns(to).map(&:name)
+ columns = index.columns.map {|c| rename[c] || c }.select do |column|
+ to_column_names.include?(column)
+ end
+
+ unless columns.empty?
+ # index name can't be the same
+ opts = { :name => name.gsub(/_(#{from})_/, "_#{to}_") }
+ opts[:unique] = true if index.unique
+ add_index(to, columns, opts)
+ end
end
end
View
38 activerecord/test/migration_test.rb
@@ -431,6 +431,44 @@ def test_rename_column_with_sql_reserved_word
end
end
+ def test_rename_column_with_an_index
+ ActiveRecord::Base.connection.create_table(:hats) do |table|
+ table.column :hat_name, :string, :limit => 100
+ table.column :hat_size, :integer
+ end
+ Person.connection.add_index :people, :first_name
+ assert_nothing_raised do
+ Person.connection.rename_column "hats", "hat_name", "name"
+ end
+ ensure
+ ActiveRecord::Base.connection.drop_table(:hats)
+ end
+
+ def test_remove_column_with_index
+ ActiveRecord::Base.connection.create_table(:hats) do |table|
+ table.column :hat_name, :string, :limit => 100
+ table.column :hat_size, :integer
+ end
+ ActiveRecord::Base.connection.add_index "hats", "hat_size"
+
+ assert_nothing_raised { Person.connection.remove_column("hats", "hat_size") }
+ ensure
+ ActiveRecord::Base.connection.drop_table(:hats)
+ end
+
+ def test_remove_column_with_multi_column_index
+ ActiveRecord::Base.connection.create_table(:hats) do |table|
+ table.column :hat_name, :string, :limit => 100
+ table.column :hat_size, :integer
+ table.column :hat_style, :string, :limit => 100
+ end
+ ActiveRecord::Base.connection.add_index "hats", ["hat_style", "hat_size"], :unique => true
+
+ assert_nothing_raised { Person.connection.remove_column("hats", "hat_size") }
+ ensure
+ ActiveRecord::Base.connection.drop_table(:hats)
+ end
+
def test_change_type_of_not_null_column
assert_nothing_raised do
Topic.connection.change_column "topics", "written_on", :datetime, :null => false

0 comments on commit b72763a

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