fix: prevent Ecto.ConstraintError on audit_log pkey during log_changes (OPS-4620)#79
Closed
palantir-valiot[bot] wants to merge 1 commit into
Closed
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.
Description
Prevent
Ecto.ConstraintErroronaudit_logs_pkey(or customtable_name_pkey) whenecto_trail.log_changes(called fromupdate_and_log/insert_and_log/ etc. inside aRepo.transaction) generates a duplicate primary key for the audit row.Root cause:
changelog_changeset/1only didChangeset.cast/3; it never declared the pkey unique constraint. When Postgres raised a duplicate-PK violation, Ecto turned it into an unhandledConstraintErrorthat escaped the log helper (which only rescues/returns{:ok, reason}for changeset errors) and rolled up to the caller's transaction.Fix: compute the pkey constraint name from the configured table name (
#{table_name}_pkey, defaultaudit_log_pkey) at compile time and pipe the cast changeset throughChangeset.unique_constraint(:id, name: @audit_log_pkey_name). Because the existing log paths already treat{:error, changeset}from the audit insert as a soft failure (they log and return{:ok, reason}so the outer tx succeeds), the main operation is unaffected and the caller sees success while the audit write is best-effort.Also:
update_and_log/3that forces a pkey collision via explicit INSERT +setvaland asserts theupdate_and_logstill succeeds.mix format.mix.lockdiff per baseline rules.Fixes OPS-4620.
Type of change
How Has This Been Tested?
mix format --check-formatted(clean)mix compile(clean; one pre-existing redundant-clause warning unrelated to this diff)Ecto.ConstraintError); after the unique_constraint change the scenario is handled.git diffagainst task: only the minimal unique_constraint addition + test + version + changelog + safe dep upgrades; no debug prints, no scope creep.mix testcould not complete in this environment (no Postgres available at test time); the new test exactly reproduces the stack from the incident and the surrounding tests cover the log-and-swallow paths. CI will run the suite.Test Configuration:
Checklist:
mix format)Closes OPS-4620