Skip to content

Commit

Permalink
Merge pull request rails#45744 from fatkodima/mysql-change-column-col…
Browse files Browse the repository at this point in the history
…lation

Preserve collation when changing column in MySQL
  • Loading branch information
byroot committed Aug 3, 2022
1 parent 6441787 commit d60d058
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,10 @@ def change_column_for_alter(table_name, column_name, type, **options)
options[:comment] = column.comment
end

unless options.key?(:collation)
options[:collation] = column.collation
end

unless options.key?(:auto_increment)
options[:auto_increment] = column.auto_increment?
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ class Mysql2CharsetCollationTest < ActiveRecord::Mysql2TestCase
assert_equal "utf8mb4_general_ci", column.collation
end

test "change column preserves collation" do
@connection.add_column :charset_collations, :description, :string, charset: "utf8mb4", collation: "utf8mb4_unicode_ci"
@connection.change_column :charset_collations, :description, :text

column = @connection.columns(:charset_collations).find { |c| c.name == "description" }
assert_equal :text, column.type
assert_equal "utf8mb4_unicode_ci", column.collation
end

test "schema dump includes collation" do
output = dump_table_schema("charset_collations")
assert_match %r/create_table "charset_collations", id: { type: :string, collation: "utf8mb4_bin" }/, output
Expand Down

0 comments on commit d60d058

Please sign in to comment.