Skip to content

Commit

Permalink
Ensure table names are quoted while renaming for sqlite3 adapter [#2272
Browse files Browse the repository at this point in the history
… state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
napcs authored and lifo committed Jun 21, 2009
1 parent 21cd4c0 commit b26c2c1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Expand Up @@ -245,7 +245,7 @@ def remove_index(table_name, options={}) #:nodoc:
end

def rename_table(name, new_name)
execute "ALTER TABLE #{name} RENAME TO #{new_name}"
execute "ALTER TABLE #{quote_table_name(name)} RENAME TO #{quote_table_name(new_name)}"
end

# See: http://www.sqlite.org/lang_altertable.html
Expand Down
26 changes: 26 additions & 0 deletions activerecord/test/cases/migration_test.rb
Expand Up @@ -635,6 +635,32 @@ def test_change_type_of_not_null_column
end
end

if current_adapter?(:SQLiteAdapter)
def test_rename_table_for_sqlite_should_work_with_reserved_words
begin
assert_nothing_raised do
ActiveRecord::Base.connection.rename_table :references, :old_references
ActiveRecord::Base.connection.create_table :octopuses do |t|
t.column :url, :string
end
end

assert_nothing_raised { ActiveRecord::Base.connection.rename_table :octopuses, :references }

# Using explicit id in insert for compatibility across all databases
con = ActiveRecord::Base.connection
assert_nothing_raised do
con.execute "INSERT INTO 'references' (#{con.quote_column_name('id')}, #{con.quote_column_name('url')}) VALUES (1, 'http://rubyonrails.com')"
end
assert_equal 'http://rubyonrails.com', ActiveRecord::Base.connection.select_value("SELECT url FROM 'references' WHERE id=1")

ensure
ActiveRecord::Base.connection.drop_table :references
ActiveRecord::Base.connection.rename_table :old_references, :references
end
end
end

def test_rename_table
begin
ActiveRecord::Base.connection.create_table :octopuses do |t|
Expand Down

0 comments on commit b26c2c1

Please sign in to comment.