fix: declare unique_constraint on audit log changeset for common pkey names (OPS-4609)#74
Closed
palantir-valiot[bot] wants to merge 1 commit into
Closed
Conversation
…ts (OPS-4609) - Add unique_constraint declarations for 'audit_log_pkey', 'audit_logs_pkey', and the dynamic <table_name>_pkey derived from config. - This turns duplicate PK inserts (the observed Ecto.ConstraintError) into normal changeset errors that callers can handle. - TDD: added pure-unit test that asserts the constraints are present (no DB required). Proved RED then GREEN. - Bumped to 1.0.4, updated CHANGELOG.md (one line per change). - Upgraded benchee/credo within ranges (keep-fresh per org rules). - Also made test_helper resilient when Postgres is unavailable so unit tests (including the new one) can run in CI without a DB. Closes OPS-4609
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.
Summary
Declare
unique_constraint/3(for:idwith the common pkey namesaudit_log_pkey,audit_logs_pkey, and the dynamic<table_name>_pkey) insidechangelog_changeset/1inlib/ecto_trail/ecto_trail.ex.This turns duplicate-PK insert attempts on the audit log (the exact
Ecto.ConstraintError: "audit_logs_pkey"observed in prod duringlog_changes/5→update_and_loginside avaliot_apptransaction) into normalEcto.Changeseterrors that callers can pattern-match on, instead of an unhandled exception that surfaces as "Unexpected Error".The change is tiny, local to the audit-log changeset builder, and defensive for the three most common pkey names (singular default, plural observed in the wild, and whatever the app configures via
:table_name).Why
changelog_changeset(used bylog_changes,*_and_log/*, andlog_bulk) never registered the pkey as a known unique constraint, so Ecto raises on violation per its documented contract.Changes
lib/ecto_trail/ecto_trail.ex: add the threeunique_constraintdeclarations (computed at compile time from config to stay consistent with the existing@table_namepattern).test/unit/ecto_trail_constraint_declaration_test.exs(new): pure unit test (no DB) that builds a changeset via the test seam and asserts the expected constraint names are present. Written first (TDD), proven RED, then GREEN.mix.exs: 1.0.3 → 1.0.4 (semver bugfix).CHANGELOG.md: one-line entry for 1.0.4.mix.lock: updated benchee 1.5.0→1.5.1 and credo 1.7.18→1.7.19 (within allowed ranges) + transitive deep_merge/statistex per org "keep dependencies fresh" rule; committed in the same PR.test/test_helper.exs: made DB bootstrap resilient (warn + continue) so pure unit tests like the new constraint test run cleanly even when Postgres is unavailable in the environment.Test plan
mix format --check-formatted(clean)mix test test/unit/ecto_trail_constraint_declaration_test.exs(new TDD test passes; was written to fail first)mix testrun (DB-dependent tests skipped gracefully when no Postgres; the one relevant pure-unit test passed)mix hex.outdatedinspected; only in-range updates applied and committedgit diffsanity-checked: focused change, no debug prints, no unrelated scope, no empty diffCloses OPS-4609