Skip to content

fix: *_and_log and log no longer raise Ecto.ConstraintError on audit_logs_pkey (on_conflict: :nothing)#63

Closed
palantir-valiot[bot] wants to merge 1 commit into
mainfrom
palantir/OPS-4605-audit-log-pkey-unique-constraint
Closed

fix: *_and_log and log no longer raise Ecto.ConstraintError on audit_logs_pkey (on_conflict: :nothing)#63
palantir-valiot[bot] wants to merge 1 commit into
mainfrom
palantir/OPS-4605-audit-log-pkey-unique-constraint

Conversation

@palantir-valiot

Copy link
Copy Markdown

Summary

Prevent Ecto.ConstraintError on audit_logs_pkey (unique) during best-effort audit logging in update_and_log / insert_and_log / upsert_and_log / delete_and_log / log by changing the internal repo.insert for the Changelog row to use on_conflict: :nothing, conflict_target: :id.

The audit log is intentionally best-effort (errors are logged and swallowed, the outer operation still succeeds). Duplicate log attempts (from retries, Multi re-entrancy, or pkey sequence skew under load) now silently no-op instead of raising inside the transaction and rolling back user work.

Also: version bump to 1.0.4, CHANGELOG entry, and refresh of allowed-range outdated deps (benchee 1.5.0→1.5.1, credo 1.7.18→1.7.19 + transitives).

Why

Stacktrace from OPS-4605 (eliot-lamosa-gto-prod):

(ecto_trail 1.0.3) lib/ecto_trail/ecto_trail.ex:435: EctoTrail.log_changes/5
(ecto_trail 1.0.3) lib/ecto_trail/ecto_trail.ex:315: anonymous fn/4 in EctoTrail.update_and_log/4
...
** (Ecto.ConstraintError) constraint error when attempting to insert struct:
    * "audit_logs_pkey" (unique_constraint)

Triage: NOTIFY+FIX, category code_bug. The root cause is a missing duplicate-log guard on the pkey insert of the audit row (table uses a plain serial/identity id pkey with no natural unique on (resource,resource_id,change_type,inserted_at) etc.).

Fix is minimal and localized to the two log_changes* helpers that perform the audit insert. No behavior change for happy path; only changes the error path from "raise and rollback" to "best-effort log + continue".

Test plan

  • mix deps.get
  • mix compile (clean; only pre-existing redundant-clause warning on map_custom_ecto_type)
  • mix format --check-formatted (exit 0)
  • mix hex.outdated + mix deps.update for allowed updates (benchee, credo) — now all up-to-date within constraints
  • Read git diff (own diff) before commit — matches scope, no debug prints, no scope creep
  • Added regression test exercising the pkey collision path (seed row + rewind sequence so internal insert hits unique violation; update_and_log still succeeds)
  • mix test attempted (fails only on "connection refused" to Postgres in this agent pod; the new test and all prior *_and_log / Multi cases are present and would exercise the on_conflict path; CI with real DB will run it)
  • Version bumped in mix.exs (1.0.3 → 1.0.4)
  • CHANGELOG.md updated (concise one-line-per-change style)
  • Branch name exactly palantir/OPS-4605-audit-log-pkey-unique-constraint
  • One PR per task

Closes OPS-4605

@linear-code

linear-code Bot commented Jun 13, 2026

Copy link
Copy Markdown

OPS-4605

@acrogenesis

Copy link
Copy Markdown
Member

Closing as a duplicate of #24 — same bug: Ecto.ConstraintError on audit_logs_pkey in EctoTrail.log_changes/5. These were generated from a backlog of duplicate Linear issues created by a log-agent dedup gap (now fixed in palantir 38438d6; no new duplicates are being filed). Consolidating on #24.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant