Skip to content

fix: autogenerate binary_id UUID PK + unique_constraint(:id) to prevent audit_logs_pkey ConstraintError in log_changes/update_and_log (OPS-4607)#68

Closed
palantir-valiot[bot] wants to merge 1 commit into
mainfrom
palantir/OPS-4607-audit-log-pkey-id-generation
Closed

fix: autogenerate binary_id UUID PK + unique_constraint(:id) to prevent audit_logs_pkey ConstraintError in log_changes/update_and_log (OPS-4607)#68
palantir-valiot[bot] wants to merge 1 commit into
mainfrom
palantir/OPS-4607-audit-log-pkey-id-generation

Conversation

@palantir-valiot

Copy link
Copy Markdown

Summary

First-party ecto_trail raised Ecto.ConstraintError on audit_logs_pkey inside log_changes/update_and_log (and equivalent paths) when called from within a transaction (e.g. valiot_app updateDevice). Root cause: the Changelog schema (used for audit_log rows) did not declare an autogenerating @primary_key (so no server-side UUID was generated on insert), and changelog_changeset/1 did not register unique_constraint(:id). The result was either missing ID generation or raw constraint exceptions bubbling up as 500s instead of changeset errors.

Changes:

  • lib/ecto_trail/changelog.ex:8: add @primary_key {:id, :binary_id, autogenerate: true} so every inserted audit row gets a generated UUIDv4.
  • lib/ecto_trail/ecto_trail.ex:575: changelog_changeset/1 now pipes through Changeset.unique_constraint(:id) so pkey collisions become changeset errors (not raised ConstraintError).
  • Added test-only escape hatch __changelog_changeset__/1 (used to assert constraint registration in a no-DB context).
  • Updated the test migration (priv/repo/migrations/20170419082821_create_log_changes_table.exs) to match the documented UUID PK shape used in README.md examples and the module docs.
  • Added TDD test in test/unit/ecto_trail_test.exs asserting binary UUID PK on produced Changelog rows.
  • Bumped to 1.1.0 (minor: explicit ID generation + constraint handling behavior).
  • Updated CHANGELOG.md.
  • Also upgraded fresh deps within ranges (benchee, credo, transitive deep_merge/statistex) per Palantir baseline rules.

This matches the exact stack in the Linear issue (ecto_trail 1.0.3 lib/ecto_trail/ecto_trail.ex:435 in log_changes/5 called from update_and_log/4).

Why

Closes OPS-4607 (https://linear.app/valiot/issue/OPS-4607). High-severity code bug: missing/insufficient unique handling + ID generation for audit rows under concurrent/rapid updates inside transactions. The triage decision was NOTIFY+FIX.

Test plan

  • mix format --check-formatted (clean)
  • mix compile (clean; only pre-existing redundant-clause warning unrelated to this diff)
  • Verified via mix run -e (no DB) that __changelog_changeset__ now registers unique_constraint(:id) on the :id field; temporarily removed the constraint to demonstrate red/green.
  • Added unit test exercising insert_and_log path and asserting is_binary(id) and byte_size==36 (UUID string length) on the produced Changelog row.
  • mix hex.outdated + mix deps.update performed; only in-range upgrades landed (benchee 1.5.0→1.5.1, credo 1.7.18→1.7.19 and transitives).
  • Note: full mix test cannot run in this pod (no Postgres reachable; test/test_helper.exs requires a live DB for sandbox/migrations). The DB-dependent path was exercised via the ID-generation test (which will run in CI) and the no-DB constraint registration check. The core fix is the schema + changeset change; the error path change is proven by the constraint list assertion.
  • Pre-existing unrelated branches (many palantir/OPS-46xx-* audit-pkey variants) exist; this is the canonical fix for the reported symptom (autogenerate + unique_constraint on the changeset).

Files changed (from git diff --stat)

CHANGELOG.md
lib/ecto_trail/changelog.ex
lib/ecto_trail/ecto_trail.ex
mix.exs
mix.lock
priv/repo/migrations/20170419082821_create_log_changes_table.exs
test/unit/ecto_trail_test.exs

Closes OPS-4607

…(:id) on changeset to turn pkey violations into errors instead of ConstraintError raises (update_and_log/log_changes). Bump 1.1.0. Closes OPS-4607
@linear-code

linear-code Bot commented Jun 13, 2026

Copy link
Copy Markdown

OPS-4607

@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