-
Notifications
You must be signed in to change notification settings - Fork 1
fix: catch Ecto.ConstraintError (duplicate audit_log pkey) from log_changes/update_and_log so caller tx commits (OPS-4572) #33
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 all commits
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 |
|---|---|---|
|
|
@@ -391,19 +391,7 @@ defmodule EctoTrail do | |
| change_type: operation_type | ||
| } | ||
| |> changelog_changeset() | ||
| |> repo.insert() | ||
| |> case do | ||
| {:ok, changelog} -> | ||
| {:ok, changelog} | ||
|
|
||
| {:error, reason} -> | ||
| Logger.error( | ||
| "Failed to store changes in audit log: #{inspect(operation)} " <> | ||
| "by actor #{inspect(actor_id)}. Reason: #{inspect(reason)}" | ||
| ) | ||
|
|
||
| {:ok, reason} | ||
| end | ||
| |> insert_changelog(repo) | ||
| end | ||
|
|
||
| defp log_changes(repo, %{operation: operation} = _multi_acc, struct_or_changeset, actor_id, operation_type) do | ||
|
|
@@ -432,19 +420,7 @@ defmodule EctoTrail do | |
| change_type: operation_type | ||
| } | ||
| |> changelog_changeset() | ||
| |> repo.insert() | ||
| |> case do | ||
| {:ok, changelog} -> | ||
| {:ok, changelog} | ||
|
|
||
| {:error, reason} -> | ||
| Logger.error( | ||
| "Failed to store changes in audit log: #{inspect(struct_or_changeset)} " <> | ||
| "by actor #{inspect(actor_id)}. Reason: #{inspect(reason)}" | ||
| ) | ||
|
|
||
| {:ok, reason} | ||
| end | ||
| |> insert_changelog(repo) | ||
| end | ||
|
|
||
| defp prepare_struct_or_changeset(%Changeset{data: data} = _changeset, :delete), do: data | ||
|
|
@@ -574,4 +550,23 @@ defmodule EctoTrail do | |
| defp changelog_changeset(attrs) do | ||
| Changeset.cast(%Changelog{}, attrs, @changelog_fields) | ||
| end | ||
|
|
||
| defp insert_changelog(changeset, repo) do | ||
| changeset | ||
| |> repo.insert() | ||
| |> case do | ||
| {:ok, changelog} -> | ||
| {:ok, changelog} | ||
|
|
||
| {:error, reason} -> | ||
| Logger.error("Failed to store changes in audit log. Reason: #{inspect(reason)}") | ||
|
|
||
| {:ok, reason} | ||
| end | ||
| rescue | ||
| error -> | ||
| Logger.error("Failed to store changes in audit log (raised). Reason: #{inspect(error)}") | ||
|
|
||
| {:ok, error} | ||
|
Author
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. The rescue block in |
||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| defmodule EctoTrail.Mixfile do | ||
| use Mix.Project | ||
|
|
||
| @version "1.0.3" | ||
| @version "1.0.4" | ||
|
|
||
| def project do | ||
| [ | ||
|
|
||
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.
log_changes_alone/6(used by the publiclog/5) now passes a plain map directly toinsert_changelog/2, which doesrepo.insert(changeset).Ecto.Repo.insert/1requires a changeset or struct, not a bare map — this will raiseArgumentErrorat runtime for any call toRepo.log/4. The new helper was applied inconsistently:log_changes/5correctly wraps viachangelog_changeset/1first, butlog_changes_alone/6skips that step.