-
Notifications
You must be signed in to change notification settings - Fork 13.9k
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(migration): replace unquote with double percentages #30532
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #30532 +/- ##
===========================================
+ Coverage 60.48% 83.93% +23.44%
===========================================
Files 1931 533 -1398
Lines 76236 38540 -37696
Branches 8568 0 -8568
===========================================
- Hits 46114 32347 -13767
+ Misses 28017 6193 -21824
+ Partials 2105 0 -2105
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's important to note that the original problem here is the string being passed to a python-style interpolator accepting tokens like %s
, and not anything to do with URLs. The original fix (mostly) worked by eliminating %
s in the URL by decoding URL escape sequences like %20
, which prevented the interpolator complaining about invalid interpolation sequences. However that leaves the URL improperly encoded, resulting in other issues popping up, and does not fix the problem where %
s still appear in the string, e.g. when the password contains a literal %
.
See the documentation link from @villebro 's linked stack overflow thread: https://alembic.sqlalchemy.org/en/latest/api/config.html#alembic.config.Config.set_main_option
Simply doubling the %
s is the correct solution here so LGTM.
decoded_uri = urllib.parse.unquote(DATABASE_URI) | ||
config.set_main_option("sqlalchemy.url", decoded_uri) | ||
# Escape % chars in the database URI to avoid interpolation errors in ConfigParser | ||
escaped_uri = DATABASE_URI.replace("%", "%%") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we not need to urllib.parse.unquote
anymore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my comment above; this was never needed, it was actually a bit of a dodgy hack.
(cherry picked from commit 163b71e)
SUMMARY
#23421 introduced a regression, where passwords containing
@
characters would break migrations. To avoid having to decode the URL, we can simply escape the connection string for variable interpolation by doubling%
chars, after which the original encoded connection string works as expected.Note, that the change is functionally identical to the change proposed in the linked issue.
Here's one of many similar StackOverflow threads: https://stackoverflow.com/questions/39849641/in-flask-migrate-valueerror-invalid-interpolation-syntax-in-connection-string-a
BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
TESTING INSTRUCTIONS
This has been tested with a Postgres connection with a huge amount of special characters in the password, including
@
that previously broke the migration workflow.ADDITIONAL INFORMATION