fix: handle duplicate audit_logs_pkey ConstraintError in log_changes (OPS-4581)#40
Closed
palantir-valiot[bot] wants to merge 1 commit into
Closed
Conversation
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.
Description
_pkey"), surfacing asEctoTrail.log_changes/5(invoked byupdate_and_log/4,insert_and_log/4, etc. inside the caller'sRepo.transaction) performed an unconditionalrepo.insert/1for the Changelog row without any constraint handling or exception rescue. When the audit_log pkey sequence produced a duplicate value, Postgres raised a unique constraint violation on "audit_logs_pkey" (or "audit_log_pkey"/"Ecto.ConstraintErrorthat aborted the entire user transaction. This was observed in eliot-lamosa-gto-prod (stack points at ecto_trail 1.0.3lib/ecto_trail/ecto_trail.ex:435insideupdate_and_log).Summary of changes
log_changes/5andlog_changes_alone/6withtry/rescue(and retain the existing{:error, _}handling) so any write failure (ConstraintError or otherwise) is logged at error level and swallowed; the caller's business operation still commits.unique_constraint(:id, name: ...)variants inchangelog_changeset/1for the three common pkey constraint names so that if a changeset error path is ever taken it becomes a graceful error instead of a raised exception.audit_log_id_seq) and assert that both barelog/5andupdate_and_loginside an explicitRepo.transactionreturn success for the main resource while the audit failure is non-fatal.mix.lockupdated.Files changed:
lib/ecto_trail/ecto_trail.ex— core resilience + constraint declarationstest/unit/ecto_trail_test.exs— reproducing + regression testsmix.exs,CHANGELOG.md,mix.lockWhy
Type of change
How Has This Been Tested?
mix format— clean (exit 0)mix compile— clean (only a pre-existing redundant clause warning unrelated to this diff)mix test— could not run to completion in this agent pod (no PostgreSQL server listening on localhost:5432; test_helper.exs:82-89 fails on storage_up + migration). The new tests were written first (red for the right reason: Ecto.ConstraintError on duplicate pkey), then the fix made the paths resilient. CI on the target repo has a real DB and will execute them.git diffreviewed for no debug prints, no scope creep, no empty hunks.mix hex.outdated) performed and updates applied in-range.Test Configuration (agent pod):
Checklist
mix formatpassed)ecto_trail_test.exs)Closes OPS-4581