fix: handle audit_logs_pkey ConstraintError in *_and_log / log (OPS-4612)#73
Closed
palantir-valiot[bot] wants to merge 1 commit into
Closed
fix: handle audit_logs_pkey ConstraintError in *_and_log / log (OPS-4612)#73palantir-valiot[bot] wants to merge 1 commit into
palantir-valiot[bot] wants to merge 1 commit into
Conversation
…t (OPS-4612)
- Add table-aware unique_constraint(:id, name: "..._pkey") on Changelog changeset.
- Wrap audit log insert in insert_audit_safely/3 that rescues ConstraintError (and any other) and returns {:ok, reason} instead of letting it propagate and rollback the caller's transaction.
- Refactor the two insert sites (log_changes and log_changes_alone) to use the shared helper.
- Expose Changelog.table_name/0 for constraint name computation (supports :table_name app env override).
- Add regression test for update_and_log that seeds a colliding pkey row and asserts the business update still commits.
- Bump 1.0.3 -> 1.0.4 and concise CHANGELOG entry.
Closes OPS-4612
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
Prevent
Ecto.ConstraintError("audit_logs_pkey" unique_constraint) during the internal audit log insert inEctoTrail.update_and_log(and sibling helpersinsert_and_log,upsert_and_log,delete_and_log,log,log_bulk).The root cause was two-fold:
changelog_changeset/1never declaredunique_constraint(:id, name: "<table>_pkey"), so Ecto raised instead of returning{:error, changeset}.log_changes/5andlog_changes_alone/6(ecto_trail.ex:435, 394) did not rescueConstraintError(or any other exception) — they let it propagate out of therepo.transactionin the*_and_logwrappers, rolling back the caller's business work.Fix:
Changelog.table_name/0(respects the:table_nameapp env override).unique_constraint(table-aware) inchangelog_changeset.insert_audit_safely/3that treats any error or raised exception from the audit insert exactly like the pre-existing "best effort" paths: log and return{:ok, reason}so the outer transaction commits the real operation.update_and_logstill succeeds and commits.Files changed:
lib/ecto_trail/ecto_trail.ex,lib/ecto_trail/changelog.ex,test/unit/ecto_trail_test.exs,mix.exs,CHANGELOG.md.Why
Stack trace from OPS-4612 (eliot-lamosa-gto-prod):
This is a clear code bug in the first-party package valiot/ecto_trail. The caller's GraphQL mutation (and any other writer using
*_and_log) must not be rolled back or crash because of a duplicate audit log pkey.Closes OPS-4612
Test plan
mix format(per repo baseline; no AGENTS.md/CLAUDE.md present)mix compile(clean except one pre-existing redundant-clause warning on map_custom_ecto_type)update_and_log+ pkey collision path (the test seeds a conflicting row via the sequence and asserts the business update commits). Fullmix testrequires a live Postgres (test/test_helper.exs + data_case.ex + migrations); the container had no DB server reachable, so integration tests could not be executed here. The new test follows the same patterns as the rest of the suite and will be exercised by CI.git diff(see below) — scoped only to the audit log error handling + constraint declaration + version/changelog. No debug prints, no unrelated changes.palantir/OPS-4612-handle-audit-logs-pkey-constraintgit push-safe(plain push used only once as a bootstrap to create the remote ref so the wrapper could run its fetch/rebase/force-with-lease path).Diff stats (for reviewer sanity)