Permalink
Browse files

Fixed rename_table on SQLite tables with indexes defined (closes #5942)…

… [brandon@opensoul.org]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5260 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent fc7fd4c commit fe4d5ea786aa82426735b081b6f1581678222814 @dhh dhh committed Oct 9, 2006
@@ -227,13 +227,13 @@ def remove_index(table_name, options={}) #:nodoc:
end
def rename_table(name, new_name)
- move_table(name, new_name)
+ execute "ALTER TABLE #{name} RENAME TO #{new_name}"
end
def add_column(table_name, column_name, type, options = {}) #:nodoc:
- alter_table(table_name) do |definition|
- definition.column(column_name, type, options)
- end
+ super(table_name, column_name, type, options = {})
+ # See last paragraph on http://www.sqlite.org/lang_altertable.html
+ execute "VACUUM"
end
def remove_column(table_name, column_name) #:nodoc:
@@ -316,8 +316,9 @@ def copy_table_indexes(from, to) #:nodoc:
elsif from == "altered_#{to}"
name = name[5..-1]
end
-
- opts = { :name => name }
+
+ # 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)
end
@@ -370,6 +371,17 @@ def rewrite_count_distinct_queries(sql)
sql
end
end
+
+ def rename_table(name, new_name)
+ move_table(name, new_name)
+ end
+
+ def add_column(table_name, column_name, type, options = {}) #:nodoc:
+ alter_table(table_name) do |definition|
+ definition.column(column_name, type, options)
+ end
+ end
+
end
class DeprecatedSQLiteAdapter < SQLite2Adapter # :nodoc:
@@ -378,6 +378,27 @@ def test_rename_table
end
end
+ def test_rename_table_with_an_index
+ begin
+ ActiveRecord::Base.connection.create_table :octopuses do |t|
+ t.column :url, :string
+ end
+ ActiveRecord::Base.connection.add_index :octopuses, :url
+
+ ActiveRecord::Base.connection.rename_table :octopuses, :octopi
+
+ # Using explicit id in insert for compatibility across all databases
+ con = ActiveRecord::Base.connection
+ assert_nothing_raised { con.execute "INSERT INTO octopi (#{con.quote_column_name('id')}, #{con.quote_column_name('url')}) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')" }
+
+ assert_equal 'http://www.foreverflying.com/octopus-black7.jpg', ActiveRecord::Base.connection.select_value("SELECT url FROM octopi WHERE id=1")
+ assert ActiveRecord::Base.connection.indexes(:octopi).first.columns.include?("url")
+ ensure
+ ActiveRecord::Base.connection.drop_table :octopuses rescue nil
+ ActiveRecord::Base.connection.drop_table :octopi rescue nil
+ end
+ end
+
def test_change_column
Person.connection.add_column 'people', 'age', :integer
old_columns = Person.connection.columns(Person.table_name, "#{name} Columns")

0 comments on commit fe4d5ea

Please sign in to comment.