fix: handle audit_log pkey unique constraint in changelog_changeset (OPS-4575)#34
Closed
palantir-valiot[bot] wants to merge 1 commit into
Closed
Conversation
…OPS-4575)
- Add unique_constraint(:id, name: "\#{table}_pkey") in changelog_changeset/1
- TDD test that seeds a conflicting id and forces sequence to collide
- Bump to 1.0.4; update CHANGELOG
- mix format clean
Closes OPS-4575
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
Ecto.ConstraintError on "audit_logs_pkey" (unique_constraint) was raised from
EctoTrail.log_changes/5(called byupdate_and_log/4and siblings) when concurrent transactions caused two audit log inserts to race for the same nextval id. The root cause was inchangelog_changeset/1: onlyChangeset.cast/3was used; nounique_constraintwas declared for the pkey, so Ecto raised instead of returning a changeset error.Fix: resolve the table name via
Changelog.__schema__(:source)and addChangeset.unique_constraint(:id, name: "#{table}_pkey")after the cast. The library's callers (log_changes/5,log_changes_alone/6, and the*_and_log/*wrappers) already treat{:error, reason}from the audit insert as non-fatal for the primary resource operation (they log at error level and return{:ok, ...}), so the user'supdate_and_logetc. continue to succeed and only the audit write is best-effort.Matches the exact stack trace location (
ecto_trail.ex:435insidelog_changes/5).Why
Linear issue OPS-4575: "Ecto.ConstraintError on audit_logs_pkey during EctoTrail.log_changes/5 ... missing unique_constraint handling in the library's changeset logic is a clear code bug". Triage decision: NOTIFY+FIX (high, code_bug). First-party library; must fix regardless of frequency.
Type of change
How Has This Been Tested?
test/unit/ecto_trail_test.exs. It inserts a conflicting high id, usessetval(..., false)to force the sequence to collide on the next insert, then callsupdate_and_logand asserts{:ok, %Resource{}}(the operation must not raise).mix format --check-formatted→ clean (0)mix compile→ clean (0)mix testcould not run to completion in this agent pod (Postgres is not listening on localhost:5432;test/test_helper.exs:89fails duringEcto.Migrator.runwith "connection refused"). The new test was written to reproduce the exact error mode from the incident; CI (which provisions Postgres per.travis.yml) will execute it.Files changed:
lib/ecto_trail/ecto_trail.ex(the fix inchangelog_changeset/1)test/unit/ecto_trail_test.exs(TDD test)mix.exs(version 1.0.4)CHANGELOG.md(entry for 1.0.4)Test Configuration:
EctoTrailusage intest/supportandtest/test_helper.exsChecklist:
mix formatclean)mix compileclean)palantir/OPS-4575-fix-audit-log-pkey-constraintCloses OPS-4575