Skip to content

Commit

Permalink
Merge pull request #45744 from fatkodima/mysql-change-column-collation
Browse files Browse the repository at this point in the history
Preserve collation when changing column in MySQL
  • Loading branch information
byroot committed Aug 3, 2022
2 parents 9ac8e0c + 3a0d0f4 commit a5862af
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 @@ -391,6 +391,10 @@ def build_change_column_definition(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 a5862af

Please sign in to comment.