fix(queue/mysql): make Insert idempotent on (topic, partition_key, id)#163
Merged
Merged
Conversation
The queue_messages schema documents its UNIQUE KEY as supporting "INSERT ... ON DUPLICATE KEY to enforce idempotent publishes", but message_store.Insert used a plain INSERT that surfaced 1062 duplicate-key errors to the caller on a repeated publish. That made publishers responsible for deduplication the schema was already designed to absorb, and turned legitimate retries (network blip, caller-side retry loop, user clicking a button twice) into hard errors. Add `ON DUPLICATE KEY UPDATE topic = topic` so: - duplicate publishes for the same (topic, partition_key, id) succeed silently, and - the existing row is preserved unchanged — the no-op write does not overwrite payload, metadata, or created_at, so the first publish remains authoritative. The integration test TestIdempotentPublish previously asserted that the second publish errored — i.e. it locked in the schema/code mismatch. Realigned to assert the documented contract: the second publish must succeed silently. Added a sqlmock-based unit test for the same behavior. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
behinddwalls
approved these changes
May 28, 2026
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
queue_messagesschema documents itsUNIQUE KEY (topic, partition_key, id)as supporting "INSERT ... ON DUPLICATE KEY to enforce idempotent publishes", butmessage_store.Insertissued a plain INSERT that surfaced 1062 duplicate-key errors on repeated publishes — pushing dedup responsibility onto every caller that the schema already promised to absorb.ON DUPLICATE KEY UPDATE topic = topicso retried publishes succeed silently and the first message remains authoritative (the no-op write does not mutate payload, metadata, orcreated_at).TestIdempotentPublish, which previously locked in the schema/code mismatch by asserting the second publish errored; add a sqlmock unit test covering the same behavior.Test plan
go test -count=1 ./extension/queue/mysql/...passes (unit + sqlmock).make integration-test-extensions(coversTestIdempotentPublishagainst real MySQL).🤖 Generated with Claude Code