Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 8453

Show
Ignore:
Timestamp:
12/20/07 22:28:12 (1 year ago)
Author:
bitsweat
Message:

SQLite: fix rename_ and remove_column for columns with unique indexes. Closes #10576.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r8439 r8453  
    11*SVN* 
     2 
     3* SQLite: fix rename_ and remove_column for columns with unique indexes.  #10576 [Brandon Keepers] 
    24 
    35* Ruby 1.9 compatibility.  [Jeremy Kemper] 
  • trunk/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb

    r8113 r8453  
    305305          end 
    306306 
    307           copy_table_indexes(from, to
     307          copy_table_indexes(from, to, options[:rename] || {}
    308308          copy_table_contents(from, to, 
    309309            @definition.columns.map {|column| column.name}, 
     
    311311        end 
    312312 
    313         def copy_table_indexes(from, to) #:nodoc: 
     313        def copy_table_indexes(from, to, rename = {}) #:nodoc: 
    314314          indexes(from).each do |index| 
    315315            name = index.name 
     
    320320            end 
    321321 
    322             # index name can't be the same 
    323             opts = { :name => name.gsub(/_(#{from})_/, "_#{to}_") } 
    324             opts[:unique] = true if index.unique 
    325             add_index(to, index.columns, opts) 
     322            to_column_names = columns(to).map(&:name) 
     323            columns = index.columns.map {|c| rename[c] || c }.select do |column| 
     324              to_column_names.include?(column) 
     325            end 
     326 
     327            unless columns.empty? 
     328              # index name can't be the same 
     329              opts = { :name => name.gsub(/_(#{from})_/, "_#{to}_") } 
     330              opts[:unique] = true if index.unique 
     331              add_index(to, columns, opts) 
     332            end 
    326333          end 
    327334        end 
  • trunk/activerecord/test/migration_test.rb

    r8199 r8453  
    432432    end 
    433433     
     434    def test_rename_column_with_an_index 
     435      ActiveRecord::Base.connection.create_table(:hats) do |table| 
     436        table.column :hat_name, :string, :limit => 100 
     437        table.column :hat_size, :integer 
     438      end 
     439      Person.connection.add_index :people, :first_name 
     440      assert_nothing_raised do 
     441        Person.connection.rename_column "hats", "hat_name", "name" 
     442      end 
     443    ensure 
     444      ActiveRecord::Base.connection.drop_table(:hats) 
     445    end 
     446 
     447    def test_remove_column_with_index 
     448      ActiveRecord::Base.connection.create_table(:hats) do |table| 
     449        table.column :hat_name, :string, :limit => 100 
     450        table.column :hat_size, :integer 
     451      end 
     452      ActiveRecord::Base.connection.add_index "hats", "hat_size" 
     453 
     454      assert_nothing_raised { Person.connection.remove_column("hats", "hat_size") } 
     455    ensure 
     456      ActiveRecord::Base.connection.drop_table(:hats) 
     457    end 
     458 
     459    def test_remove_column_with_multi_column_index 
     460      ActiveRecord::Base.connection.create_table(:hats) do |table| 
     461        table.column :hat_name, :string, :limit => 100 
     462        table.column :hat_size, :integer 
     463        table.column :hat_style, :string, :limit => 100 
     464      end 
     465      ActiveRecord::Base.connection.add_index "hats", ["hat_style", "hat_size"], :unique => true 
     466 
     467      assert_nothing_raised { Person.connection.remove_column("hats", "hat_size") } 
     468    ensure 
     469      ActiveRecord::Base.connection.drop_table(:hats) 
     470    end 
     471 
    434472    def test_change_type_of_not_null_column 
    435473      assert_nothing_raised do