fix: declare unique_constraint(:id) on audit_log pkey in changelog_changeset#50
Closed
palantir-valiot[bot] wants to merge 1 commit into
Closed
Conversation
…angeset (OPS-4589) Prevents Ecto.ConstraintError on audit_logs_pkey (or audit_log_pkey) during *_and_log and log paths when a pkey collision occurs on insert of the Changelog entry. - Add :id to @changelog_fields - Compute pkey constraint name (overridable via :audit_log_pkey_name) - Call unique_constraint/3 in changelog_changeset - Expose table_name/0 and test helper - Add pure unit test for the constraint declaration - Upgrade benchee/credo (within ranges) per baseline - Bump to 1.0.4, update CHANGELOG TDD: wrote constraint test first (red until fix), then green. Closes OPS-4589
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.
Description
Prevents
Ecto.ConstraintErroronaudit_logs_pkey(oraudit_log_pkey) during*_and_log/3andlog/5when theChangelog(audit) insert hits a primary-key unique violation.Root cause
changelog_changeset/1(the single place that builds the audit row for all public logging functions) only didChangeset.cast/3. It never declared the pkeyunique_constraint/3. Per Ecto, a missingunique_constraint/3turns a DB constraint violation into a hardEcto.ConstraintErrorinstead of a validation error on the changeset. This matches the exact stack in OPS-4589:Fix (identical to the pattern already shipped for OPS-4583 / OPS-4582)
:idto@changelog_fieldsChangelog.table_name/0config :ecto_trail, :audit_log_pkey_name)Changeset.unique_constraint(:id, name: ...)insidechangelog_changeset/1__changelog_changeset_for_test__/1test seamtest/unit/ecto_trail_constraint_test.exs) + a@tag :dbintegration testtest_helper.exsso the pure unit test runs in envs without Postgres (ECTO_TRAIL_SKIP_DB=true)mix deps.update benchee credo(within allowed ranges) per Palantir baseline1.0.4, concise CHANGELOG entryFiles changed:
lib/ecto_trail/ecto_trail.exlib/ecto_trail/changelog.extest/unit/ecto_trail_constraint_test.exs(new)test/unit/ecto_trail_test.exstest/test_helper.exsmix.exs,CHANGELOG.md,mix.lockType of change
How Has This Been Tested?
TDD: wrote the failing pure-unit test first (saw
UndefinedFunctionErrorfor the test seam + missing constraint), implemented the declaration, observed green.Ran in this agent pod (no live Postgres for the
:dbtagged test):mix format --check-formattedECTO_TRAIL_SKIP_DB=true mix test test/unit/ecto_trail_constraint_test.exs→ 1/1 greenecto_trail_test.exsThe exact same diff (modulo issue number) was previously reviewed + merged for OPS-4583 on the same repo; the constraint declaration + test seam is now the org-standard pattern for this class of failure.
Test Configuration:
Checklist:
mix format)Closes OPS-4589