fix: register unique_constraint on audit_log pkey to prevent Ecto.ConstraintError in upsert_and_log#30
Closed
palantir-valiot[bot] wants to merge 1 commit into
Closed
Conversation
Prevents Ecto.ConstraintError on audit_logs_pkey (or configured table pkey) when concurrent/default-task-queue upserts call upsert_and_log (and siblings) from inside a transaction. - changelog_changeset now chains unique_constraint(:id, name: "<table>_pkey") using the same :table_name compile_env as the schema. - Added TDD regression test that forces a pkey collision via sequence rewind and asserts upsert_and_log still succeeds (audit logging is best-effort). - Bump to 1.0.4, update CHANGELOG. Closes OPS-4571
Member
|
Closing as a duplicate of #24 — all of these PRs fix the same bug: |
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
Prevent
Ecto.ConstraintErroronaudit_logs_pkey(or the configured table's pkey) whenupsert_and_log(and sibling*_and_loghelpers) are called concurrently, e.g. default-task-queue upserts from inside a transaction in valiot_app.Root cause:
log_changes/5(andlog_changes_alone) performed a barerepo.insert()of aChangelogrow viachangelog_changeset/1. Under high concurrency the autogenerated pkey collided on the audit table and Ecto raised because nounique_constraint/3had been declared on the changeset.Fix:
changelog_changesetnow chainsChangeset.unique_constraint(:id, name: "<table>_pkey"), using the same:table_namecompile-time config the schema uses. Ecto turns the violation into{:error, changeset}which the*_and_logpaths already swallow (audit logging has always been best-effort).Changes:
upsert_and_logstill succeeds)Fixes # (issue)
Type of change
How Has This Been Tested?
Ecto.ConstraintErrorreported in the stacktrace.mix format --check-formattedpasses (clean).mix testcould not execute in this agent pod (no local Postgres); the test and the fix were validated by inspecting the produced error path and confirming the constraint registration matches the pkey name from the error ("audit_logs_pkey").upsert_and_log,insert_and_log,update_and_log,delete_and_log, and Multi usage cover the happy paths.Test Configuration:
Checklist:
Closes OPS-4571