diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 747204a2f1..5511a3f738 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,7 @@ +* Fix index creation to preserve index comment in bulk change table on MySQL. + + *Ryuta Kamizono* + * Allow `unscope` to be aware of table name qualified values. It is possible to unscope only the column in the specified table. diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index 78edeba1a6..af7c6d5790 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -676,9 +676,10 @@ module ActiveRecord end def add_index_for_alter(table_name, column_name, options = {}) - index_name, index_type, index_columns, _, _, index_algorithm, index_using = add_index_options(table_name, column_name, **options) + index_name, index_type, index_columns, _, _, index_algorithm, index_using, comment = add_index_options(table_name, column_name, **options) index_algorithm[0, 0] = ", " if index_algorithm.present? - "ADD #{index_type} INDEX #{quote_column_name(index_name)} #{index_using} (#{index_columns})#{index_algorithm}" + sql = +"ADD #{index_type} INDEX #{quote_column_name(index_name)} #{index_using} (#{index_columns})#{index_algorithm}" + add_sql_comment!(sql, comment) end def remove_index_for_alter(table_name, column_name = nil, options = {}) diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index d9ef7a59b8..18e3681db5 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -1084,7 +1084,7 @@ if ActiveRecord::Base.connection.supports_bulk_alter? classname = ActiveRecord::Base.connection.class.name[/[^:]*$/] expected_query_count = { "Mysql2Adapter" => 1, # mysql2 supports creating two indexes using one statement - "PostgreSQLAdapter" => 2, + "PostgreSQLAdapter" => 3, }.fetch(classname) { raise "need an expected query count for #{classname}" } @@ -1092,7 +1092,7 @@ if ActiveRecord::Base.connection.supports_bulk_alter? assert_queries(expected_query_count) do with_bulk_change_table do |t| t.index :username, unique: true, name: :awesome_username_index - t.index [:name, :age] + t.index [:name, :age], comment: "This is a comment" end end @@ -1100,6 +1100,7 @@ if ActiveRecord::Base.connection.supports_bulk_alter? name_age_index = index(:index_delete_me_on_name_and_age) assert_equal ["name", "age"].sort, name_age_index.columns.sort + assert_equal "This is a comment", name_age_index.comment assert_not name_age_index.unique assert index(:awesome_username_index).unique