From b72763a96f0c936699d0f211be553f8f75accd07 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Thu, 20 Dec 2007 22:28:12 +0000 Subject: 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 --- .../connection_adapters/sqlite_adapter.rb | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb index 586594e0ef..cd619143e6 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb @@ -304,13 +304,13 @@ module ActiveRecord 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 @@ module ActiveRecord 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 -- cgit v1.2.3