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(postgres): update upsert regex to match the last RETURNING *
#11538
fix(postgres): update upsert regex to match the last RETURNING *
#11538
Conversation
72f80a1
to
6967bbb
Compare
Codecov Report
@@ Coverage Diff @@
## master #11538 +/- ##
==========================================
+ Coverage 96.27% 96.27% +<.01%
==========================================
Files 94 94
Lines 9185 9187 +2
==========================================
+ Hits 8843 8845 +2
Misses 342 342
Continue to review full report at Codecov.
|
6f75bc3
to
f58b66e
Compare
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.
Nice! LGTM
By the way, I have a question, was the SSCCE (papb/sequelize-sscce#9) useful to you? Or do you think you could have fixed the issue as easily without it? |
I used it yes, just changed the log to an error if the result was different from the expected. It helps to have the problem easily available, otherwise there is this great setup cost to start analyzing the issue 😄 |
@Americas Nice! I'm happy that my idea of an SSCCE repo was useful then :) |
I was thinking... Would you be willing to add an explicit test for the JSONB case as well? |
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.
Looks good.
insert.query = insert.query.replace('RETURNING *', `RETURNING ${primaryField} INTO primary_key`); | ||
update.query = update.query.replace('RETURNING *', `RETURNING ${primaryField} INTO primary_key`); | ||
if (options.returning) { | ||
insert.query = insert.query.replace(/RETURNING \*(?![\s\S]*RETURNING \*)/, `RETURNING ${primaryField} INTO primary_key`); |
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.
Nit: Regex should be moved to a constant.
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.
Done.
f58b66e
to
1655b1f
Compare
Added JSON example from SSCCE and a normal example. |
🎉 This PR is included in version 5.21.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Pull Request check-list
Please make sure to review and check all of these items:
npm run test
ornpm run test-DIALECT
pass with this change (including linting)?Description of change
Fixes #11523
Postgres' upsert query generator replaces the first
RETURNING *
it finds on the insert and update query, so if the data being added has that string, it replaces it (so it replaces the wrongRETURNING *
) changing the input data and messing with the the final query.This PR updates the replace method to use a regex with a negative lookahead so it only chooses the last
RETURNING *
in the query.