Skip to content

Commit 9b138de

Browse files
authored
Merge pull request rails#45792 from ahoglund/ahoglund/binary-collation-fix
Ensure collation is set to database default when changing column to binary
2 parents 95fa021 + be0a58b commit 9b138de

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)