1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #39323 from kamipo/fix_rename_column_for_postgresql

Fix rename column in bulk alter table for PostgreSQL
This commit is contained in:
Ryuta Kamizono 2020-05-18 11:43:20 +09:00 committed by GitHub
commit 12250a6ce9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 3 deletions

View file

@ -1493,7 +1493,7 @@ module ActiveRecord
schema_creation.accept(AddColumnDefinition.new(cd))
end
def rename_column_for_alter(table_name, column_name, new_column_name)
def rename_column_sql(table_name, column_name, new_column_name)
"RENAME COLUMN #{quote_column_name(column_name)} TO #{quote_column_name(new_column_name)}"
end

View file

@ -667,7 +667,7 @@ module ActiveRecord
end
def rename_column_for_alter(table_name, column_name, new_column_name)
return super if supports_rename_column?
return rename_column_sql(table_name, column_name, new_column_name) if supports_rename_column?
column = column_for(table_name, column_name)
options = {

View file

@ -437,7 +437,7 @@ module ActiveRecord
# Renames a column in a table.
def rename_column(table_name, column_name, new_column_name) #:nodoc:
clear_cache!
execute("ALTER TABLE #{quote_table_name(table_name)} #{rename_column_for_alter(table_name, column_name, new_column_name)}")
execute("ALTER TABLE #{quote_table_name(table_name)} #{rename_column_sql(table_name, column_name, new_column_name)}")
rename_column_indexes(table_name, column_name, new_column_name)
end

View file

@ -1056,6 +1056,23 @@ if ActiveRecord::Base.connection.supports_bulk_alter?
assert_equal "This is a comment", column(:birthdate).comment
end
def test_rename_columns
with_bulk_change_table do |t|
t.string :qualification
end
assert column(:qualification)
with_bulk_change_table do |t|
t.rename :qualification, :experience
t.string :qualification_experience
end
assert_not column(:qualification)
assert column(:experience)
assert column(:qualification_experience)
end
def test_removing_columns
with_bulk_change_table do |t|
t.string :qualification, :experience