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 duplicate escaping of quotes for check constraint expressions in MySQL schema #42429

Merged
merged 1 commit into from Sep 26, 2023

Conversation

Flixt
Copy link
Contributor

@Flixt Flixt commented Jun 9, 2021

Summary

Mysql automatically escapes quotes in generated check constraints expressions.
When Rails dumps the schema (into schema.rb) the generated schema also contains
the quotes with duplicated escaping (\\').
To fix this, we remove the escaping that MySQL provides from the fetched expression.

Fixes #42424

Other Information

I decided to go with an MySQL specific test, because putting it in https://github.com/rails/rails/blob/6-1-stable/activerecord/test/cases/migration/check_constraint_test.rb leads to code like this:

if current_adapter?(:Mysql2Adapter)
  assert_equal "`name` <> 'forbidden_string'", constraint.expression
elsif current_adapter?(:PostgreSQLAdapter)
  assert_equal "(name)::text <> 'forbidden_string'::text", constraint.expression
else
  assert_equal "name != 'forbidden_string'", constraint.expression
end

As the code shows, even MariaDB handles the quoting differently than MySQL.

@Flixt Flixt force-pushed the fix-mysql-check-constraints-quoting branch 2 times, most recently from b89c7da to 4d57880 Compare June 9, 2021 08:28
@Flixt Flixt force-pushed the fix-mysql-check-constraints-quoting branch 2 times, most recently from f166b36 to 5ef4ca8 Compare June 10, 2021 17:18
@Flixt
Copy link
Contributor Author

Flixt commented Jun 12, 2021

Is this bugfix something we'd consider worth to be backported to 6.1 / 5.2 ?

@zzak zzak requested a review from kamipo June 12, 2021 23:37
@Flixt Flixt force-pushed the fix-mysql-check-constraints-quoting branch 2 times, most recently from cd3ff87 to bcef98b Compare June 18, 2021 08:49
@rails-bot
Copy link

rails-bot bot commented Sep 16, 2021

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Sep 16, 2021
@marcelolx
Copy link
Contributor

+1

@rails-bot rails-bot bot removed the stale label Sep 16, 2021
@Flixt Flixt force-pushed the fix-mysql-check-constraints-quoting branch from bcef98b to 0fb9302 Compare September 17, 2021 09:21
@Flixt
Copy link
Contributor Author

Flixt commented Sep 17, 2021

I rebased it onto main..

@rails-bot
Copy link

rails-bot bot commented Dec 16, 2021

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Dec 16, 2021
@rails-bot rails-bot bot closed this Dec 23, 2021
@HolyWalley
Copy link
Contributor

+1

@skipkayhil skipkayhil reopened this Jul 26, 2023
@rails-bot rails-bot bot removed the stale label Jul 26, 2023
@Flixt Flixt force-pushed the fix-mysql-check-constraints-quoting branch 2 times, most recently from c6c0304 to a2f5aee Compare July 26, 2023 14:03
@Flixt
Copy link
Contributor Author

Flixt commented Jul 26, 2023

I rebased it onto main.

…MySQL schema

Mysql automatically escapes quotes in generated check constraints expressions.
When Rails dumps the schema (into schema.rb) the generated schema also contains,
the quotes with duplicated escaping (\\\\').
To fix this, we remove the escaping, that MySQL provides from the fetched
expression.
@rafaelfranca rafaelfranca force-pushed the fix-mysql-check-constraints-quoting branch from 405075e to 468d3d0 Compare September 26, 2023 16:19
@rafaelfranca rafaelfranca merged commit dc56140 into rails:main Sep 26, 2023
4 checks passed
rafaelfranca added a commit that referenced this pull request Sep 26, 2023
Fix duplicate escaping of quotes for check constraint expressions in MySQL schema
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.

Duplicate escaping of quotes in check constraint expressions (MySQL)
6 participants