Skip to content

Commit be0a58b

Browse files
authored
Ensure collation is set to database default when changing column
A recent change made it so that when you perform a change column it will preserve collation on the column rather than nil it out· which would select the database default. This works fine except in the case of a binary type. In this case the collation should be set to nil in the options so that the database default collation type can be used on the column.
1 parent 22a0692 commit be0a58b

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ def build_change_column_definition(table_name, column_name, type, **options) # :
398398
options[:comment] = column.comment
399399
end
400400

401-
unless options.key?(:collation)
401+
unless options.key?(:collation) || type == :binary
402402
options[:collation] = column.collation
403403
end
404404

activerecord/test/cases/adapters/mysql2/charset_collation_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ class Mysql2CharsetCollationTest < ActiveRecord::Mysql2TestCase
4848
assert_equal "utf8mb4_general_ci", column.collation
4949
end
5050

51+
test "change column ensures binary column type are set to nil" do
52+
@connection.add_column :charset_collations, :description, :string, charset: "utf8mb4", collation: "utf8mb4_unicode_ci"
53+
@connection.change_column :charset_collations, :description, :binary
54+
55+
column = @connection.columns(:charset_collations).find { |c| c.name == "description" }
56+
57+
assert_equal :binary, column.type
58+
assert_nil column.collation
59+
end
60+
5161
test "change column preserves collation" do
5262
@connection.add_column :charset_collations, :description, :string, charset: "utf8mb4", collation: "utf8mb4_unicode_ci"
5363
@connection.change_column :charset_collations, :description, :text

0 commit comments

Comments
 (0)