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

feat: commit_comment option for prepending a SQL comment to a COMMIT statement #669

Conversation

bceskavich
Copy link
Contributor

We're using this option at @knocklabs to inject comments for all transactions executed via the Ecto.Repo.transaction/2 callback, which is gives us more granular insight into transaction performance in our RDS performance insights dashboard. We'd like to upstream it!

I wasn't sure where to add unit test coverage for this, if anywhere. Nor was I sure how I should try to sanitize the input, if at all.

More context in the ecto-elixir Google Group discussion here: https://groups.google.com/g/elixir-ecto/c/vUImb5ZX_CI

@josevalim
Copy link
Member

Thank you. I have some questions:

  1. Should we use -- or /* ... */? Do they work the same?
  2. Regardless of the form that we choose, we should probably validate it against SQL injection.
  3. We'd also need docs. :)

@bceskavich
Copy link
Contributor Author

Should we use -- or /* ... */? Do they work the same?

We opted for -- (single-line comment) plus a newline char before the COMMIT because we found it helps to provide visual distinction between the comment and the actual SQL command. Functionally I believe they are identical besides the full-line/multi-line behavior, and I don't have a strong opinion on which should be used.

Here's a real example from our RDS insights dashboard:

Inline query preview Full query highlight view
image image

Regardless of the form that we choose, we should probably validate it against SQL injection.

Are there any shared tools or preexisting patterns for doing this in the Postgrex codebase that I should reference?

We'd also need docs. :)

Yup of course, will add those in! 😄

@josevalim
Copy link
Member

I think we should go with /* then because it will allow multi-line comments. Then, to validate it doesn't inject SQL, we just need to validate it doesn't have "*/" in it.

@bceskavich
Copy link
Contributor Author

@josevalim I just pushed updates to add docs + validate against SQL injection. For testing the validation flow, should that live in TransactionTest?

@josevalim
Copy link
Member

SOunds good yeah

@bceskavich
Copy link
Contributor Author

Alright @josevalim just pushed up a unit test. I think this is ready for another review.

lib/postgrex/protocol.ex Outdated Show resolved Hide resolved
@dkuku
Copy link
Contributor

dkuku commented Mar 29, 2024

Should this be named differently? transaction_comment maybe.

@@ -537,16 +540,19 @@ defmodule Postgrex.Protocol do
{:ok, Postgrex.Result.t(), state}
| {DBConnection.status(), state}
| {:disconnect, %RuntimeError{}, state}
| {:disconnect, %DBConnection.ConnectionError{} | Postgrex.Error.t(), state}
| {:disconnect,
%DBConnection.ConnectionError{} | Postgrex.Error.t() | Postgrex.QueryError.t(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's return Postgrex.Error. It is not worth introducing a new exception type only for this case. WDYT? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine with me!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also return a RuntimeError if you prefer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went with Postgrex.Error in af319f9

bceskavich and others added 2 commits March 30, 2024 14:06
Co-authored-by: José Valim <jose.valim@gmail.com>
@josevalim josevalim merged commit ff417bd into elixir-ecto:master Mar 30, 2024
8 of 9 checks passed
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants