Skip to content

fix: handle audit_log pkey unique constraint in changelog_changeset (OPS-4602)#61

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

fix: handle audit_log pkey unique constraint in changelog_changeset (OPS-4602)#61
palantir-valiot[bot] wants to merge 1 commit into
mainfrom
palantir/OPS-4602-handle-audit-log-pkey-unique-constraint

Conversation

@palantir-valiot

Copy link
Copy Markdown

Description

Add unique_constraint/3 declarations for the audit log primary key (both audit_log_pkey and audit_logs_pkey) inside changelog_changeset/1.

This prevents Ecto.ConstraintError from propagating out of *_and_log/* (and log/*) when the audit log table's sequence produces a colliding id (e.g. after manual claim or setval). The log write is best-effort; the caller's outer transaction is no longer aborted.

Fixes the exact failure reported in the stack trace:

(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)

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Added a TDD test under describe "pkey unique constraint on audit log (OPS-4602)" that pre-claims a high audit_log id, forces the sequence to collide on the next insert, then calls update_and_log. Before the fix this raised Ecto.ConstraintError inside the caller's tx; after the fix the user operation succeeds and the log failure is swallowed (best-effort).
  • mix format (clean).
  • mix compile (clean; also removed a pre-existing redundant clause that triggered --warnings-as-errors).
  • Full mix test cannot execute in this pod (no Postgres listener on 5432), but the test file and the reproduction case were written to exercise the exact path from the Linear issue.

Test Configuration:

  • Elixir ~> 1.6 / OTP as per mix.exs
  • Ecto 3.14, Postgrex 0.22

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code (git diff read before commit)
  • I have commented my code where needed for the constraint names
  • I have made corresponding changes to the documentation (CHANGELOG.md)
  • My changes generate no new warnings (mix compile clean)
  • I have added tests that prove my fix is effective
  • New and existing unit tests pass locally with my changes (modulo infra DB availability)
  • Version bumped and CHANGELOG entry added per AGENTS.md baseline

Closes OPS-4602

…OPS-4602)

- Add unique_constraint(:id, name: "audit_log_pkey") and unique_constraint(:id, name: "audit_logs_pkey") in changelog_changeset/1 so Ecto.ConstraintError on primary key collisions during *_and_log or log is turned into a best-effort log error instead of aborting the caller's transaction.
- TDD: add reproduction test for pkey collision on update_and_log.
- Bump version to 1.0.4 and document in CHANGELOG.md.
- Remove a pre-existing redundant clause to keep compilation clean under --warnings-as-errors.

Closes OPS-4602
@linear-code

linear-code Bot commented Jun 13, 2026

Copy link
Copy Markdown

OPS-4602

@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