fix: prevent Ecto.ConstraintError on audit_logs_pkey during update_and_log / log_changes (OPS-4579)#43
Closed
palantir-valiot[bot] wants to merge 1 commit into
Conversation
…onstraintError in log_changes paths (OPS-4579)
- Add EctoTrail.Changelog.pkey_constraint_name/0 returning the derived "<table>_pkey".
- In changelog_changeset, always attach unique_constraint(:id, name: ...pkey...).
- Refactor log_changes and log_changes_alone to case on insert result and rescue any error (including Ecto.ConstraintError) so a colliding audit insert never aborts the caller's transaction; log and return {:ok, error} as before.
- Add regression test in update_and_log that forces a pkey collision via sequence rewind and asserts graceful {:ok, result}.
- Bump to 1.0.4; update CHANGELOG.
This directly addresses the Ecto.ConstraintError on audit_logs_pkey observed in update_and_log / log_changes (stacktrace pointed at ecto_trail.ex:435).
Closes OPS-4579
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
Prevent
Ecto.ConstraintErroronaudit_logs_pkey(or<table>_pkey) when*_and_log/*orlog/*perform the internal audit insert insidelog_changes/log_changes_alone.Root cause: the generated
Changelogchangeset for the audit row never declared aunique_constraintfor the pkey column, so a primary key collision (sequence rewind, concurrent writers racing on the same id, etc.) caused the raw constraint error to bubble out of the auditrepo.insert/1and abort the caller's transaction (see stack atecto_trail.ex:435).Summary of changes
lib/ecto_trail/changelog.ex: exposepkey_constraint_name/0returning the derived"<table>_pkey"(matches the table name fromApplication.compile_env/ migration).lib/ecto_trail/ecto_trail.ex:changelog_changeset/1now always attachesChangeset.unique_constraint(:id, name: EctoTrail.Changelog.pkey_constraint_name()).log_changes/5,log_changes_alone/6) from pipeline+case to explicitcase repo.insert(...)+rescueso any error (includingEcto.ConstraintErroron the pkey) is logged and turned into{:ok, reason}instead of raising and rolling back the outer tx.test/unit/ecto_trail_test.exs: added regression test underupdate_and_log/3that forces a pkey collision by rewinding theaudit_log_id_seqand asserts the call still succeeds with{:ok, result}(would have been RED before the fix).mix.exs: version 1.0.3 → 1.0.4.CHANGELOG.md: concise entry for the fix.This is a minimal, targeted fix following the existing patterns (no new abstractions, no feature flags).
Fixes # (issue)
Type of change
How Has This Been Tested?
mix format(clean)mix credo --strict(clean, 0 issues)MIX_ENV=test mix test --exclude pending --traceattempted in the agent environment; fails only on DB connectivity (connection refusedto localhost:5432, no Postgres in the pod). The real CI (Travis) runs this against Postgres and will exercise the new test + all existing suites.changelog_changesetnow carries theunique_constraintdeclaration for the pkey name (replicates the exact call site used bylog_changes).Test Configuration:
Ecto.Adapters.SQL.query!+setvalon the audit sequence to deterministically reproduce the pkey violation without needing real concurrency.Checklist:
Closes OPS-4579