Fix Ecto.ConstraintError on audit_logs_pkey during *_and_log (OPS-4577)#37
Closed
palantir-valiot[bot] wants to merge 1 commit into
Closed
Fix Ecto.ConstraintError on audit_logs_pkey during *_and_log (OPS-4577)#37palantir-valiot[bot] wants to merge 1 commit into
palantir-valiot[bot] wants to merge 1 commit into
Conversation
…nd_log (OPS-4577)
- Add @table_name / @pkey_constraint_name module attrs derived from config (matching migration and Changelog schema).
- Pipe unique_constraint(:id, name: @pkey_constraint_name) into the internal changelog_changeset/1.
- This converts the DB constraint violation into a changeset error; the existing log_changes rescue path logs and returns {:ok, reason} instead of letting Ecto.ConstraintError bubble out of the wrapping transaction.
- Added regression test under "audit log pkey constraint handling (OPS-4577 regression)" that seeds a future pkey and asserts the outer update_and_log succeeds.
- Also upgraded benchee/credo (and transitive deep_merge/statistex) within ranges per "keep deps fresh" rule.
- Version bumped to 1.0.4; CHANGELOG entry added.
Closes OPS-4577
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
Fix
Ecto.ConstraintErroronaudit_logs_pkey(unique_constraint) raised from insideEctoTrail.update_and_log/4(and sibling*_and_log/logpaths) when the audit log insert collides on the primary key inside the wrapping transaction.Root cause:
changelog_changeset/1did not declare the pkey unique constraint. When the DB raises a pkey violation (e.g. sequence reset, concurrent writers, or explicit high-id seed in some environments), Ecto surfaces it as an unhandledEcto.ConstraintErrorinstead of a changeset error. The existing error-handling path inlog_changes/5(andlog_changes_alone/6) only rescues{:error, reason}fromrepo.insert/1.Changes
lib/ecto_trail/ecto_trail.ex:@table_nameand@pkey_constraint_name(matching the existing migration andChangelogschema).Changeset.unique_constraint(:id, name: @pkey_constraint_name)intochangelog_changeset/1.log_changescode already logs the failure and returns{:ok, reason}(preserving the outer operation's success) — exactly the desired behavior for audit-log side effects.test/unit/ecto_trail_test.exs("audit log pkey constraint handling (OPS-4577 regression)") that forces a pkey collision viasetvaland assertsupdate_and_logstill succeeds for the primary resource.1.0.4inmix.exs.CHANGELOG.md.benchee,credo, and transitives) and refreshedmix.lockper "keep dependencies fresh" rule on every PR.Files changed:
Fixes the exact stack from the incident:
(ecto_trail 1.0.3) lib/ecto_trail/ecto_trail.ex:435: EctoTrail.log_changes/5
...
(valiot_app ...) ... in Repo.transaction
Type of change
How Has This Been Tested?
mix format --check-formatted(passes)mix credo --strict(passes, 0 issues)mix hex.outdated+ upgrades of benchee/credo within ranges (now clean)mix compile(clean)mix testrequires a live Postgres; it will be exercised by CI on this PR (and matches the existing test matrix in .travis.yml).Test Configuration:
TestRepo+Changelogschema (no new test frameworks)Checklist:
mix format, credo clean)Closes OPS-4577