-
-
Notifications
You must be signed in to change notification settings - Fork 11
Use bind params regex from sqlalchemy while escaping #122
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
Use bind params regex from sqlalchemy while escaping #122
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #122 +/- ##
=======================================
Coverage 89.81% 89.81%
=======================================
Files 86 86
Lines 4054 4056 +2
Branches 841 841
=======================================
+ Hits 3641 3643 +2
Misses 336 336
Partials 77 77 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Do you mind adding a simple View test with a cast, asserting that it outputs your expected syntax? Mostly just so this doesn't get inadvertently reverted |
|
Makes perfect sense, I'll do that. |
|
@DanCardin I just extended the |
|
Umm, I really don't understand what happened with the last |
fedea97 to
0d8bf83
Compare
0d8bf83 to
8d990b6
Compare
|
Thank you! |
This PR makes the
sqlalchemy.escape_paramsfunction more specific about which:characters it escapes. I believe this is a safe change, because we're using the same regex that the sqlalchemy library uses to find the parameters.I believe this change makes the output more pleasant to look at in more cases, but my main motivation for making this change is how the old behavior of
escape_paramsinteracted withpostgresql.Views: Whensqlalchemy-declarative-extensionsencounters a changed Postgres view, it creates a temporaryVIEWin Postgres with the newViewexpression and reads the body of theVIEWit back from Postgres to construct the Python literal that's used to assemble theop.execute(...)statement for the alembic migration. The trouble is thatPostgresaugments every literal expression in theViewdefinition with explicit:: TYPEcasts. The old behavior ofescape_paramsthen replaced all the::occurrences with\:\:s unnecessarily.The proliferation of these
\:\:occurrences makes the resulting migration harder to read, but aside from that aesthetic concern, it also interacts poorly with how theop.execute()statements are rendered:Which causes the escaped
\:fragments to appear directly in the generated migration file and then Pyright complains that\:is not a valid escape sequence. Ideally we'd emit theop.executestatements via an expression likef'op.execute({repr(command)})'and let the repr machinery do all the escaping, but I'm not suggesting that change sincerepr(<string>)produces very ugly one-line strings that escape newlines as well.TLDR;
\:\:fragments we're avoiding with this PR are unnecessary and bad for readability, but they also interact badly with the way we're emitting alembic migration statements.