Skip to content

Commit 82d6ebc

Browse files
committed
Make sure we check that the column we want to rename is present before trying to rename it. And thus throw the correct AR exception type rather than letting the DB fail.
1 parent d183184 commit 82d6ebc

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,8 +522,12 @@ def add_column(table_name, column_name, type, options = {})
522522
execute(add_column_sql)
523523
end
524524

525-
def rename_column(table, column, new_column_name)
526-
execute "EXEC sp_rename '#{table}.#{column}', '#{new_column_name}'"
525+
def rename_column(table_name, column_name, new_column_name)
526+
if columns(table_name).find{|c| c.name.to_s == column_name.to_s}
527+
execute "EXEC sp_rename '#{table_name}.#{column_name}', '#{new_column_name}'"
528+
else
529+
raise ActiveRecordError, "No such column: #{table_name}.#{column_name}"
530+
end
527531
end
528532

529533
def change_column(table_name, column_name, type, options = {}) #:nodoc:

0 commit comments

Comments
 (0)