fix: *_and_log no longer raises Ecto.ConstraintError on audit_log pkey (OPS-4565)#27
Closed
palantir-valiot[bot] wants to merge 1 commit into
Closed
Conversation
…y/unique violation (OPS-4565)
- Declare unique_constraint(:id, name: "<table>_pkey") in changelog_changeset so Ecto can map it.
- Wrap the audit log repo.insert in a protected helper that rescues *any* raised DB error (ConstraintError etc.) and returns {:error, e}.
- log_changes and log_changes_alone already treat {:error, _} as non-fatal (log + return {:ok, reason}); the wrapper ensures ConstraintError never bubbles out.
- Added regression test under update_and_log that forces a duplicate audit insert via an added unique constraint and asserts the call still succeeds.
- Bumped to 1.0.4; updated CHANGELOG; refreshed within-range dev deps (benchee, credo).
- mix format applied.
Closes OPS-4565
Member
|
Closing as a duplicate of #24 — all of these PRs fix the same bug: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Prevent
Ecto.ConstraintError("audit_logs_pkey"/audit_log_pkeyunique constraint) from escapingupdate_and_log/insert_and_log/upsert_and_log/delete_and_log(and the internallog_changes/log_changes_alonepaths) when the audit log insert hits a duplicate-key or other constraint. The error is logged and swallowed exactly like other audit-write failures so the caller'sRepo.transactionsucceeds.Changes:
lib/ecto_trail/ecto_trail.ex:578: declareunique_constraint(:id, name: "<table>_pkey")inchangelog_changeset/1so Ecto can map the pkey constraint to a changeset error (instead of raising).lib/ecto_trail/ecto_trail.ex:587: introduce thininsert_changelog_protected/2that rescues any raised DB error (ConstraintError, etc.) fromrepo.insert/1and returns{:error, e}.log_changes/5:436andlog_changes_alone/6:395) to use the protected insert. The existing{:error, _} -> log + {:ok, reason}handling already ensures the caller's transaction is not aborted.test/unit/ecto_trail_test.exs: added regression test underupdate_and_log/3that forces a duplicate audit row via an added(resource, resource_id, change_type)unique constraint and asserts the call still returns{:ok, updated}without raising.mix deps.updatefor within-range dev deps (benchee, credo) andmix format.Files changed:
lib/ecto_trail/ecto_trail.extest/unit/ecto_trail_test.exsmix.exsCHANGELOG.mdmix.lockWhy
OPS-4565: production stack trace shows an unhandled
Ecto.ConstraintErroronaudit_logs_pkeyoriginating fromEctoTrail.log_changes/5:435insideupdate_and_log/4:315inside aRepo.transaction. The internal audit insert did not declare the pkey constraint and did not rescue the raised error, violating the "audit log write failures must not abort the caller's work" contract established for Multi/rollback handling in 1.0.3 (OPS-3479).Root cause is exactly the pattern called out in the triage: "unhandled constraint case (no
unique_constraint/3on the changeset)" plus missing protection around the rawrepo.insert/1.Test plan
mix format(clean)mix compile(clean; only pre-existing xref deprecation warning unrelated to this change)mix hex.outdated+mix deps.updatefor allowed-range dev deps (benchee, credo) in the same PR per workflowgit diff --cachedequivalent viagit show); no debug prints, no scope creep, follows existing patterns and the "log + swallow" contractgit push-safe; branch namepalantir/OPS-4565-fix-audit-log-pkey-constraint-error(exact convention)Closes OPS-4565