Handle audit_log pkey unique_constraint in *_and_log / log (OPS-4570)#35
Closed
palantir-valiot[bot] wants to merge 1 commit into
Closed
Conversation
…PS-4570) - Declare unique_constraint on audit log PK so Ecto.ConstraintError on duplicate pkey is converted to changeset error and swallowed (existing contract: audit failures never abort user tx). - Add :id to @changelog_fields and compute @audit_log_pkey_name from :table_name config. - Add small private test seam (test_forced_audit_log_id) and helper add_audit_log_unique_constraints. - Add regression test under 'audit log pkey constraint handling (OPS-4570)' that forces a pkey collision via sequence rewind and asserts upsert_and_log still succeeds. - Bump to 1.0.4; add CHANGELOG entry. - mix format + credo --strict clean. Closes OPS-4570
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
Ecto.ConstraintError on
audit_logs_pkey(unique_constraint) was raised from insideupsert_and_log(and other*_and_log/logpaths) when the audit logrepo.insertinside the transaction attempted to reuse a primary key value. The root cause was a missingunique_constraint/3declaration on the audit log changeset for the table's pkey (the table name is configurable via:table_name, default"audit_log").Per the package contract, failures to write the audit log must never abort the caller's user operation (they are logged and swallowed). The fix ensures the constraint violation is turned into a changeset error (handled by the existing
{:error, reason} -> Logger...; {:ok, reason}arms) instead of raisingEcto.ConstraintError.Summary of changes
lib/ecto_trail/ecto_trail.ex::idto@changelog_fieldsso explicit ids (for test seam) are accepted by cast.@audit_log_table_nameand@audit_log_pkey_namefromApplication.compile_env(:ecto_trail, :table_name, "audit_log")to support custom table names.log_changes/5(line ~435 area) andlog_changes_alone/6(line ~395 area), merge an optional test seam map and pipe the audit changeset through the newadd_audit_log_unique_constraints/1beforerepo.insert/1.add_audit_log_unique_constraints/1doesChangeset.unique_constraint(:id, name: @audit_log_pkey_name).test_forced_audit_log_id/0is a no-op in prod; only used by the regression test to force a deterministic pkey collision viaApplication.put_env.test/unit/ecto_trail_test.exs: added a newdescribe "audit log pkey constraint handling (OPS-4570)"with a regression test that:audit_log_id_seqby one (so the next bare insert would collide on pkey), callsupsert_and_log, and asserts the user operation still succeeds (no ConstraintError escapes).mix.exs: version bumped to1.0.4.CHANGELOG.md: added a concise 1.0.4 "Fixed" entry describing the bug and the contract-preserving behavior.Why
See Linear: OPS-4570 (Ecto.ConstraintError on audit_logs_pkey during ecto_trail.upsert_and_log in jobs-lamosa-gto-prod). First occurrence but a clear missing
unique_constraintper the error message guidance and the package's error-handling contract.Type of change
How Has This Been Tested?
mix format(clean).mix compile(clean; only pre-existing minor warnings unrelated to this change).mix credo --strict(clean, 0 issues).mix testrequires a live Postgres (not available in this ephemeral pod; the test DB setup intest/test_helper.exs+ migrations are exercised in CI). The new test and the two audit insert call sites are covered by the compile path + the explicit sequence-collision scenario. Existing tests forinsert_and_log/update_and_log/upsert_and_log/log/log_bulkand the Ecto.Multi integration paths continue to exercise the samelog_changes*functions.palantir/...branch.Test Configuration:
Checklist:
Closes OPS-4570