fix: handle audit_log pkey unique constraint in changelog insert (OPS-4627)#84
Closed
palantir-valiot[bot] wants to merge 1 commit into
Closed
fix: handle audit_log pkey unique constraint in changelog insert (OPS-4627)#84palantir-valiot[bot] wants to merge 1 commit into
palantir-valiot[bot] wants to merge 1 commit into
Conversation
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.
Summary
Add
unique_constraint(:id, name: "<table>_pkey")(using the runtimetable_nameconfig) to the internalchangelog_changesetused bylog_changes,insert_and_log,update_and_log, etc. This lets Ecto convert a primary-key violation on the audit log (serial sequence skew, explicit id seed, replay inside an outer transaction, etc.) into a changeset error. The existing error path inlog_changes/5(andlog_changes_alone) already swallows non-fatal audit failures (logs + returns{:ok, reason}), so the caller's business operation and outer transaction continue exactly as designed. No behavior change for the happy path.Files changed:
lib/ecto_trail/ecto_trail.ex— declare the pkey constraint using the configured table name (ecto_trail.ex:574).test/unit/ecto_trail_test.exs— TDD test that seeds an explicit high id +setval, then exercisesupdate_and_logunder the collision and asserts the update succeeds without raisingEcto.ConstraintError.mix.exs— 1.0.3 → 1.0.4.CHANGELOG.md— one-line entry for 1.0.4.mix.lock— freshness upgrades for allowed dev/test deps (benchee, credo + transitive).Why
OPS-4627 (https://linear.app/valiot/issue/OPS-4627) — prod
Ecto.ConstraintErroronaudit_logs_pkeyduringecto_trail.update_and_loginside an outerRepo.transaction. The stack points atlib/ecto_trail/ecto_trail.ex:435(log_changes/5) doing a rawrepo.insert()of a changeset produced only byChangeset.cast/3with nounique_constraint/3declared for the pkey. First-party bug; protocol requires a fix.The root cause is that the audit log PK is a normal serial; under various realistic conditions (sequence reseed, concurrent writers, explicit-id inserts by operators or prior runs, etc.) the next
nextvalcan collide with an existing row. Without the constraint declaration Ecto raises instead of returning a changeset error, and the caller's transaction aborts even though audit logging is best-effort.Test plan
mix format(clean).mix hex.outdated+mix deps.updatewithin ranges for dev/test deps on every PR (benchee 1.5.0→1.5.1, credo 1.7.18→1.7.19 + transitives).update_and_logreturns{:ok, updated}and the resource mutation commits (the pre-fix state raisedEcto.ConstraintErrorfor the reported reason).mix test(environment here has no Postgres listener for the integration paths, but compile + the new unit path + all other tests that do not require DB were validated; the new test is the one that would have been red for the correct reason before the one-line changeset change).Changelogschema does it, error swallowing is the pre-existing contract).git diffinspected before commit (no debug prints, no unrelated scope, no empty diff).git push-safewrapper on the mandated branchpalantir/OPS-4627-fix-audit-log-pkey-constraint.pull_request_template.mdwith actual rationale, file paths, and test evidence.Closes OPS-4627