fix: handle audit_logs_pkey unique constraint in log_changes (OPS-4574)#32
Closed
palantir-valiot[bot] wants to merge 1 commit into
Closed
fix: handle audit_logs_pkey unique constraint in log_changes (OPS-4574)#32palantir-valiot[bot] wants to merge 1 commit into
palantir-valiot[bot] wants to merge 1 commit into
Conversation
…tx no longer raise Ecto.ConstraintError on collisions
- TDD: added test that seeds a Changelog row then rewinds the id sequence so the next update_and_log hits pkey unique violation; asserts the outer write still succeeds (no crash).
- changelog_changeset now pipes through unique_constraint(:id, name: "#{table}_pkey").
- log_changes already treats {:error, reason} as soft-fail (logs + returns {:ok, reason}); the constraint declaration converts the DB exception into that path.
- Bumped 1.0.3 -> 1.0.4 and updated CHANGELOG.md per release rules.
- Upgraded benchee 1.5.0->1.5.1 and credo 1.7.18->1.7.19 (plus transitive) within allowed ranges; ran mix hex.outdated.
- Ran mix format.
Closes OPS-4574
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.
Summary
Root cause:
update_and_log/insert_and_log(and friends) run inside an explicitrepo.transaction, then calllog_changeswhich does a rawrepo.insert(changelog_changeset(...)). TheChangelogschema had nounique_constraintdeclared on its pkey, so a DB unique violation onaudit_log_pkey(e.g. sequence rewind in prod) produced an unhandledEcto.ConstraintErrorinstead of{:error, changeset}. The surrounding code already treats{:error, reason}as a soft failure (it logs and returns{:ok, reason}), allowing the caller's write to succeed. The error surfaced invaliot_appduring a GraphQLupdateDevicemutation.Fix: pipe the cast through
Changeset.unique_constraint(:id, name: pkey_constraint_name())insidechangelog_changeset/1. The pkey name is computed dynamically from the (configurable)Changelogtable source so it works for customtable_nameconfigs. Added a TDD test that seeds aChangelogrow, rewinds the sequence, then callsupdate_and_logand asserts the outer operation still succeeds.Changed files:
lib/ecto_trail/ecto_trail.ex:574(the constraint declaration + helper)test/unit/ecto_trail_test.exs(new collision test in theupdate_and_logdescribe)mix.exs,CHANGELOG.md(version + release note)mix.lock(freshened dev/test deps per baseline rules)Why
This is the exact failure described in OPS-4574 (Ecto.ConstraintError on audit_logs_pkey during
ecto_trail.log_changescalled fromupdate_and_loginside a transaction). See stack trace in the issue forlib/ecto_trail/ecto_trail.ex:435.Closes OPS-4574
Test plan
mix format(clean)mix compile(clean; only pre-existing redundant-clause warning in map_custom_ecto_type)mix hex.outdated+ upgraded benchee 1.5.0→1.5.1 and credo 1.7.18→1.7.19 (and transitive) within allowed ranges in the same commitsetvalon the sequence) duringupdate_and_logand asserts success instead of ConstraintErrormix test— could not execute in the agent pod (Postgres is not available on localhost:5432;test/test_helper.exsrequires a real DB and runs migrations). The new test follows the exact style and helpers of the surrounding tests and will be exercised by CI.git diff; no debug prints, no unrelated changes, no empty diffpalantir/OPS-4574-handle-audit-log-pkey-constraintScreenshots
N/A — no UI change.