Skip to content

Commit b8b796c

Browse files
authored
Merge pull request rails#46127 from fatkodima/fix-change_column-collation
Do not preserve original column collation in `change_column` for older migrations
2 parents 32ea85f + 5fecf23 commit b8b796c

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,10 @@ def build_change_column_definition(table_name, column_name, type, **options) # :
393393
options[:comment] = column.comment
394394
end
395395

396-
unless options.key?(:collation)
397-
options[:collation] = column.collation if text_type?(type)
396+
if options[:collation] == :no_collation
397+
options.delete(:collation)
398+
else
399+
options[:collation] ||= column.collation if text_type?(type)
398400
end
399401

400402
unless options.key?(:auto_increment)

activerecord/lib/active_record/migration/compatibility.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ def rename_table(table_name, new_name, **options)
6161
super
6262
end
6363

64+
def change_column(table_name, column_name, type, **options)
65+
if connection.adapter_name == "Mysql2"
66+
options[:collation] = :no_collation
67+
end
68+
super
69+
end
70+
6471
def change_column_null(table_name, column_name, null, default = nil)
6572
super(table_name, column_name, !!null, default)
6673
end

activerecord/test/cases/migration/compatibility_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,20 @@ def up
658658
end
659659
end
660660

661+
if current_adapter?(:Mysql2Adapter)
662+
def test_change_column_on_7_0
663+
migration = Class.new(ActiveRecord::Migration[7.0]) do
664+
def up
665+
execute("ALTER TABLE testings CONVERT TO CHARACTER SET utf32")
666+
change_column(:testings, :foo, "varchar(255) CHARACTER SET ascii")
667+
end
668+
end
669+
assert_nothing_raised do
670+
ActiveRecord::Migrator.new(:up, [migration], @schema_migration, @internal_metadata).migrate
671+
end
672+
end
673+
end
674+
661675
private
662676
def precision_implicit_default
663677
if current_adapter?(:Mysql2Adapter)

0 commit comments

Comments
 (0)