Skip to content

Commit

Permalink
Merge pull request #41292 from intrip/41156-fix-mysql-virtual-column-…
Browse files Browse the repository at this point in the history
…with-escapes

Fix Mysql schema for virtual column expressions with quotes
  • Loading branch information
rafaelfranca committed Feb 2, 2021
1 parent b11e9e0 commit d4907b7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ def extract_expression_for_virtual_column(column)
" WHERE table_schema = #{scope[:schema]}" \
" AND table_name = #{scope[:name]}" \
" AND column_name = #{column_name}"
@connection.query_value(sql, "SCHEMA").inspect
# Calling .inspect leads into issues with the query result
# which already returns escaped quotes.
# We remove the escape sequence from the result in order to deal with double escaping issues.
@connection.query_value(sql, "SCHEMA").gsub("\\'", "'").inspect
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def setup
t.virtual :upper_name, type: :string, as: "UPPER(`name`)"
t.virtual :name_length, type: :integer, as: "LENGTH(`name`)", stored: true
t.virtual :name_octet_length, type: :integer, as: "OCTET_LENGTH(`name`)", stored: true
t.json :profile
t.virtual :profile_email, type: :string, as: "json_extract(`profile`,_utf8mb4'$.email')", stored: true
end
VirtualColumn.create(name: "Rails")
end
Expand Down Expand Up @@ -58,6 +60,7 @@ def test_schema_dumping
assert_match(/t\.virtual\s+"upper_name",\s+type: :string,\s+as: "(?:UPPER|UCASE)\(`name`\)"$/i, output)
assert_match(/t\.virtual\s+"name_length",\s+type: :integer,\s+as: "(?:octet_length|length)\(`name`\)",\s+stored: true$/i, output)
assert_match(/t\.virtual\s+"name_octet_length",\s+type: :integer,\s+as: "(?:octet_length|length)\(`name`\)",\s+stored: true$/i, output)
assert_match(/t\.virtual\s+"profile_email",\s+type: :string,\s+as: "json_extract\(`profile`,\w*?'\$\.email'\)", stored: true$/i, output)
end
end
end

0 comments on commit d4907b7

Please sign in to comment.