Skip to content

Commit

Permalink
Merge pull request rails#49336 from fatkodima/fix-mysql-expression-in…
Browse files Browse the repository at this point in the history
…dex-dumping

Fix MySQL expression index dumping with escaped quotes
  • Loading branch information
eileencodes authored Sep 25, 2023
2 parents d64e44b + 69b32bd commit f57d54c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def indexes(table_name)
end

if row[:Expression]
expression = row[:Expression]
expression = row[:Expression].gsub("\\'", "'")
expression = +"(#{expression})" unless expression.start_with?("(")
indexes.last[-2] << expression
indexes.last[-1][:expressions] ||= {}
Expand Down
9 changes: 9 additions & 0 deletions activerecord/test/cases/schema_dumper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,15 @@ def test_schema_dump_expression_indices
assert false
end
end

if current_adapter?(:Mysql2Adapter, :TrilogyAdapter)
def test_schema_dump_expression_indices_escaping
index_definition = dump_table_schema("companies").split(/\n/).grep(/t\.index.*full_name_index/).first.strip
index_definition.sub!(/, name: "full_name_index"\z/, "")

assert_match %r{concat_ws\(`firm_name`,`name`,_utf8mb4' '\)\)"\z}i, index_definition
end
end
end

if current_adapter?(:Mysql2Adapter, :TrilogyAdapter)
Expand Down
7 changes: 6 additions & 1 deletion activerecord/test/schema/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,12 @@
t.index [:firm_id, :type], name: "company_partial_index", where: "(rating > 10)"
t.index [:firm_id], name: "company_nulls_not_distinct", nulls_not_distinct: true
t.index :name, name: "company_name_index", using: :btree
t.index "(CASE WHEN rating > 0 THEN lower(name) END) DESC", name: "company_expression_index" if supports_expression_index?
if supports_expression_index?
t.index "(CASE WHEN rating > 0 THEN lower(name) END) DESC", name: "company_expression_index"
if ActiveRecord::TestCase.current_adapter?(:Mysql2Adapter, :TrilogyAdapter)
t.index "(CONCAT_WS(`firm_name`, `name`, _utf8mb4' '))", name: "full_name_index"
end
end
end

create_table :content, force: true do |t|
Expand Down

0 comments on commit f57d54c

Please sign in to comment.