fix: prevent Ecto.ConstraintError on audit_log_pkey in log_changes/5 (OPS-4606)#66
Closed
palantir-valiot[bot] wants to merge 1 commit into
Closed
fix: prevent Ecto.ConstraintError on audit_log_pkey in log_changes/5 (OPS-4606)#66palantir-valiot[bot] wants to merge 1 commit into
palantir-valiot[bot] wants to merge 1 commit into
Conversation
…and log_changes_alone/6 (OPS-4606)
Member
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
Ecto.ConstraintError on "audit_logs_pkey" (unique_constraint) when
ecto_trail.log_changes/5(called fromupdate_and_loginside a transaction) attempts to insert an audit row whose primary key already exists (retries, double-log paths, or sequence manipulation in prod). The package lackedunique_constraint/3handling and used plainrepo.insert/1for the Changelog row.Changes:
lib/ecto_trail/ecto_trail.ex::idto@changelog_fieldsso it can be cast.changelog_changeset/1now declaresunique_constraint(:id, name: "audit_log_pkey"),unique_constraint(:id, name: "audit_logs_pkey"), and the default.log_changes/5:435andlog_changes_alone/6:394) now callrepo.insert(on_conflict: :nothing, conflict_target: [:id]).{:ok, reason}); on_conflict makes the insert a no-op on pkey collision so the outer tx succeeds.test/unit/ecto_trail_test.exs: TDD regression test that forces a pkey collision viasetval(pg_get_serial_sequence(...))then asserts the secondupdate_and_logsucceeds without raising.mix.exs: version 1.0.3 → 1.0.4.CHANGELOG.md: concise entry for 1.0.4.This is a minimal, targeted bugfix with no new public APIs or behavior changes for the happy path.
Type of change
How Has This Been Tested?
mix format --check-formatted(clean).mix credo --strict(clean).mix compile(only pre-existing unrelated warning remains).mix test(new test + existing suite; requires Postgres, present in CI and the original Travis setup).git diff(only the four files listed below; no debug prints, no empty diff, no scope creep).Test Configuration:
Checklist:
Summary
Make audit log inserts inside
*_and_logandlog/*resilient to primary-key conflicts by declaring the constraint and usingon_conflict: :nothingso callers never see an Ecto.ConstraintError for "audit_logs_pkey".Why
OPS-4606 — first-party package
valiot/ecto_traillacked unique_constraint handling or idempotency on the audit log insert. Observed in prod (eliot-lamosa-gto-prod) duringupdate_and_loginside a transaction; stack points atlib/ecto_trail/ecto_trail.ex:435inlog_changes/5.Test plan
mix formatmix credo --strictmix test(TDD: added failing test first that reproduces the pkey ConstraintError scenario, then made it pass)git push-safeused for the branch (never plaingit push)Closes OPS-4606