Skip to content

Commit 2c03e17

Browse files
authored
Merge pull request rails#38995 from islam-taha/preserve_column_comment_on_renaming_column
Preserve column comment on renaming column
2 parents d5a3b2e + c22cad9 commit 2c03e17

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

activerecord/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Preserve column comment value on changing column name on MySQL.
2+
3+
*Islam Taha*
4+
15
* Add support for `if_exists` option for removing an index.
26

37
The `remove_index` method can take an `if_exists` option. If this is set to true an error won't be raised if the index doesn't exist.

activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,8 @@ def rename_column_for_alter(table_name, column_name, new_column_name)
665665
options = {
666666
default: column.default,
667667
null: column.null,
668-
auto_increment: column.auto_increment?
668+
auto_increment: column.auto_increment?,
669+
comment: column.comment
669670
}
670671

671672
current_type = exec_query("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE #{quote(column_name)}", "SCHEMA").first["Type"]

activerecord/test/cases/comment_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,17 @@ def test_remove_comment_from_column
114114
assert_nil column.comment
115115
end
116116

117+
def test_rename_column_preserves_comment
118+
@connection.add_column :commenteds, :rating, :string, comment: "I am running out of imagination"
119+
@connection.rename_column :commenteds, :rating, :new_rating
120+
121+
Commented.reset_column_information
122+
column = Commented.columns_hash["new_rating"]
123+
124+
assert_equal :string, column.type
125+
assert_equal column.comment, "I am running out of imagination"
126+
end
127+
117128
def test_schema_dump_with_comments
118129
# Do all the stuff from other tests
119130
@connection.add_column :commenteds, :rating, :integer, comment: "I am running out of imagination"

0 commit comments

Comments
 (0)