-
Notifications
You must be signed in to change notification settings - Fork 287
feat: commit_comment
option for prepending a SQL comment to a COMMIT
statement
#669
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
Changes from 4 commits
3e81f34
f2f0607
95f9809
ce786c9
bf2c21d
af319f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,9 @@ defmodule Postgrex.Protocol do | |
@nonposix_errors [:closed, :timeout] | ||
@max_rows 500 | ||
@text_type_oid 25 | ||
@commit_comment_validation_error Postgrex.QueryError.exception( | ||
"`:comment_comment` option cannot contain sequence \"*/\"" | ||
) | ||
|
||
defstruct sock: nil, | ||
connection_id: nil, | ||
|
@@ -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(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fine with me! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can also return a RuntimeError if you prefer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Went with Postgrex.Error in af319f9 |
||
state} | ||
def handle_commit(_, %{postgres: {_, _}} = s) do | ||
lock_error(s, :commit) | ||
end | ||
|
||
def handle_commit(opts, %{postgres: postgres} = s) do | ||
case Keyword.get(opts, :mode, :transaction) do | ||
:transaction when postgres == :transaction -> | ||
statement = "COMMIT" | ||
handle_transaction(statement, opts, s) | ||
with {:ok, statement} <- build_commit_statement(opts, s) do | ||
handle_transaction(statement, opts, s) | ||
end | ||
|
||
:savepoint when postgres == :transaction -> | ||
statement = "RELEASE SAVEPOINT postgrex_savepoint" | ||
|
@@ -2887,6 +2893,22 @@ defmodule Postgrex.Protocol do | |
|
||
## transaction | ||
|
||
defp build_commit_statement(opts, %{buffer: buffer} = s) do | ||
case Keyword.get(opts, :commit_comment) do | ||
comment when is_binary(comment) -> | ||
if String.contains?(comment, "*/") do | ||
disconnect(s, @commit_comment_validation_error, buffer) | ||
else | ||
statement = "/* #{comment} */\nCOMMIT" | ||
{:ok, statement} | ||
end | ||
|
||
_ -> | ||
statement = "COMMIT" | ||
{:ok, statement} | ||
end | ||
end | ||
|
||
defp handle_transaction(statement, opts, %{buffer: buffer} = s) do | ||
status = new_status(opts, mode: :transaction) | ||
msgs = [msg_query(statement: statement)] | ||
|
@@ -3368,6 +3390,10 @@ defmodule Postgrex.Protocol do | |
{:disconnect, %{err | connection_id: connection_id}, %{s | buffer: buffer}} | ||
end | ||
|
||
defp disconnect(s, %Postgrex.QueryError{} = err, buffer) do | ||
{:disconnect, err, %{s | buffer: buffer}} | ||
end | ||
|
||
defp disconnect(s, %RuntimeError{} = err, buffer) do | ||
{:disconnect, err, %{s | buffer: buffer}} | ||
end | ||
|
Uh oh!
There was an error while loading. Please reload this page.