Skip to content

Commit 25b7f64

Browse files
authored
Merge pull request rails#50733 from yash/yk/fix-mysql-escapes-on-default-generated
Fix single quote escapes on default generated MySQL columns
2 parents a9562e2 + 0702c24 commit 25b7f64

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

activerecord/CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
* Fix single quote escapes on default generated MySQL columns
2+
3+
MySQL 5.7.5+ supports generated columns, which can be used to create a column that is computed from an expression.
4+
5+
Previously, the schema dump would output a string with double escapes for generated columns with single quotes in the default expression.
6+
7+
This would result in issues when importing the schema on a fresh instance of a MySQL database.
8+
9+
Now, the string will not be escaped and will be valid Ruby upon importing of the schema.
10+
11+
*Yash Kapadia*
12+
113
* Fix Migrations with versions older than 7.1 validating options given to
214
`add_reference` and `t.references`.
315

activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ def new_column_from_field(table_name, field, _definitions)
185185
default, default_function = nil, default
186186
elsif type_metadata.extra == "DEFAULT_GENERATED"
187187
default = +"(#{default})" unless default.start_with?("(")
188+
default = default.gsub("\\'", "'")
188189
default, default_function = nil, default
189190
elsif type_metadata.type == :text && default&.start_with?("'")
190191
# strip and unescape quotes

activerecord/test/cases/defaults_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,13 @@ class MysqlDefaultExpressionTest < ActiveRecord::TestCase
175175
output = dump_table_schema("defaults")
176176
assert_match %r/t\.binary\s+"uuid",\s+limit: 36,\s+default: -> { "\(?uuid\(\)\)?" }/i, output
177177
end
178+
179+
if current_adapter?(:Mysql2Adapter)
180+
test "schema dump includes default expression with single quotes reflected correctly" do
181+
output = dump_table_schema("defaults")
182+
assert_match %r/t\.string\s+"char2_concatenated",\s+default: -> { "\(?concat\(`char2`,(_utf8mb4)?'-'\)\)?" }/i, output
183+
end
184+
end
178185
end
179186

180187
if supports_datetime_with_precision?

activerecord/test/schema/mysql2_specific_schema.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
t.string :char2, limit: 50, default: "a varchar field"
2424
if ActiveRecord::TestCase.supports_default_expression?
2525
t.binary :uuid, limit: 36, default: -> { "(uuid())" }
26+
t.string :char2_concatenated, default: -> { "(concat(`char2`, '-'))" }
2627
end
2728
end
2829

0 commit comments

Comments
 (0)