diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2006-10-09 02:14:36 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2006-10-09 02:14:36 +0000 |
commit | fe4d5ea786aa82426735b081b6f1581678222814 (patch) | |
tree | 200a1cb5c2836ca5917c0fa5556f5b6c82c2d0ae /activerecord | |
parent | fc7fd4c5cee277d569fd406c55fc9d147ca000c1 (diff) | |
download | rails-fe4d5ea786aa82426735b081b6f1581678222814.tar.gz rails-fe4d5ea786aa82426735b081b6f1581678222814.tar.bz2 rails-fe4d5ea786aa82426735b081b6f1581678222814.zip |
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
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb | 24 | ||||
-rw-r--r-- | activerecord/test/migration_test.rb | 21 |
2 files changed, 39 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb index db9aa2e717..eb57df3d2f 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb @@ -227,13 +227,13 @@ module ActiveRecord 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 @@ module ActiveRecord 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 @@ module ActiveRecord 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: diff --git a/activerecord/test/migration_test.rb b/activerecord/test/migration_test.rb index fdd21df300..a28532eed7 100644 --- a/activerecord/test/migration_test.rb +++ b/activerecord/test/migration_test.rb @@ -378,6 +378,27 @@ if ActiveRecord::Base.connection.supports_migrations? 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") |