1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix index creation to preserve comment in bulk change table on MySQL

I've found the bug when I'm refactoring index creation code in #39203.
This commit is contained in:
Ryuta Kamizono 2020-05-09 16:03:45 +09:00
parent 1c84b1ab50
commit fc7eab048b
3 changed files with 10 additions and 4 deletions

View file

@ -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.

View file

@ -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 = {})

View file

@ -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