Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix single quote escapes on default generated MySQL columns #50733

Merged
merged 1 commit into from
Jan 17, 2024

Conversation

yash
Copy link
Contributor

@yash yash commented Jan 12, 2024

Closes #50732

Motivation / Background

MySQL 5.7.5+ supports generated columns, which can be used to create a column that is computed from an expression. If single quotes are used in the definition of a default generated column, the schema dump will escape the quotes twice.

In a scenario such as the following:

create_table :people, force: true do |t|
  t.string :first_name
  t.string :last_name
  t.string :full_name, default: -> { "(concat(`first_name`,_utf8mb4'-',`last_name`))" }
end

We want the schema dump to look like this:

t.string "full_name", default: -> { "(concat(`first_name`,_utf8mb4'-',`last_name`))" }

And not this, which is how it now it is now:

t.string "full_name", default: -> { "(concat(`first_name`,_utf8mb4\\'-\\',`last_name`))" }

See the related issue for more.

Detail

This pull request changes the schema statement generator under the elsif block for a column with DEFAULT_GENERATED in the metadata. In the changes, we simply gsub the escapes as we do in other parts of the class.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message.
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@yash yash force-pushed the yk/fix-mysql-escapes-on-default-generated branch 5 times, most recently from bf906a3 to e5d51ff Compare January 12, 2024 21:55
@yash yash marked this pull request as ready for review January 12, 2024 22:07
@yash yash force-pushed the yk/fix-mysql-escapes-on-default-generated branch 3 times, most recently from db76162 to 330fb6a Compare January 17, 2024 11:41
@yash yash changed the title Fix MySQL escapes on default generated columns Fix single quote escapes on default generated MySQL columns Jan 17, 2024
@yash yash force-pushed the yk/fix-mysql-escapes-on-default-generated branch 2 times, most recently from 99572cd to 3e7779f Compare January 17, 2024 12:11
MySQL 5.7.5+ supports generated columns, which can be used to create a column that is computed from an expression. This commit fixes the escaping of the default value for such expressions if a single quote is included.

See the following for more: https://dev.mysql.com/blog-archive/generated-columns-in-mysql-5-7-5/
@yash yash force-pushed the yk/fix-mysql-escapes-on-default-generated branch from 3e7779f to 0702c24 Compare January 17, 2024 12:12
@byroot byroot merged commit 25b7f64 into rails:main Jan 17, 2024
4 checks passed
byroot added a commit that referenced this pull request Jan 17, 2024
…enerated

Fix single quote escapes on default generated MySQL columns
byroot added a commit that referenced this pull request Jan 17, 2024
…enerated

Fix single quote escapes on default generated MySQL columns
@byroot
Copy link
Member

byroot commented Jan 17, 2024

Backported to 7.1 and 7.0

@yash yash deleted the yk/fix-mysql-escapes-on-default-generated branch January 17, 2024 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect schema dump of default generated columns with single quotes
2 participants