Skip to content

Make many-to-many additions idempotent on unique constraint violation - #4611

Merged
johha merged 1 commit into
mainfrom
ignore-unique-errors-for-many-to-many
Oct 23, 2025
Merged

Make many-to-many additions idempotent on unique constraint violation#4611
johha merged 1 commit into
mainfrom
ignore-unique-errors-for-many-to-many

Conversation

@johha

@johha johha commented Oct 16, 2025

Copy link
Copy Markdown
Contributor

Adds an ignored_unique_constraint_violation_errors option to the many_to_many association in the VcapRelations Sequel plugin. When this option is provided with a list of index name patterns, any Sequel::UniqueConstraintViolation error that occurs during an add_ operation on an association and matches one of the patterns will be caught. The operation is wrapped in a transaction with a savepoint, which is rolled back upon catching the specific error, effectively making the addition idempotent. This prevents race conditions where concurrent requests attempt to add the same association. This issue can be observed when a user makes two POST requests at the same time to create a new resource instance.

This new option is applied to the staging_spaces relationship in the SecurityGroup model to handle potential concurrent updates. Further models will updated in separate commits.

  • I have reviewed the contributing guide

  • I have viewed, signed, and submitted the Contributor License Agreement

  • I have made this pull request to the main branch

  • I have run all the unit tests using bundle exec rake

  • I have run CF Acceptance Tests

@johha
johha force-pushed the ignore-unique-errors-for-many-to-many branch from 19a9d0e to 1d21619 Compare October 22, 2025 09:27
@johha
johha marked this pull request as ready for review October 22, 2025 10:01
@johha
johha requested review from a team October 22, 2025 10:01
Comment thread spec/unit/lib/sequel_plugins/vcap_relations_spec.rb Outdated
Comment thread lib/sequel_plugins/vcap_relations.rb
@jochenehret
jochenehret self-requested a review October 22, 2025 13:24
jochenehret
jochenehret previously approved these changes Oct 22, 2025
Adds an `ignored_unique_constraint_violation_errors` option to the many_to_many association in the `VcapRelations` Sequel plugin.
When this option is provided with a list of index name patterns, any `Sequel::UniqueConstraintViolation` error that occurs during an `add_` operation on an association and matches one of the patterns will be caught.
The operation is wrapped in a transaction with a savepoint, which is rolled back upon catching the specific error, effectively making the addition idempotent. This prevents race conditions where concurrent requests attempt to add the same association.
This issue can be observed when a user makes two POST requests at the same time to create a new resource instance.

This new option is applied to the `staging_spaces relationship in the `SecurityGroup` model to handle potential concurrent updates. Further models will updated in separate commits.
@johha
johha force-pushed the ignore-unique-errors-for-many-to-many branch from 1d21619 to cfe5b65 Compare October 22, 2025 13:25
@johha
johha requested a review from jochenehret October 22, 2025 13:27
@johha
johha merged commit 8b932f9 into main Oct 23, 2025
11 checks passed
ari-wg-gitbot added a commit to cloudfoundry/capi-release that referenced this pull request Oct 23, 2025
Changes in cloud_controller_ng:

- Make many-to-many additions idempotent on unique constraint violation
    PR: cloudfoundry/cloud_controller_ng#4611
    Author: Johannes Haass <johannes.haass@sap.com>
@moleske
moleske deleted the ignore-unique-errors-for-many-to-many branch October 23, 2025 15:42
johha added a commit that referenced this pull request Nov 3, 2025
Commit `8b932f9f9498d2d222b981aa7dc5db7a6d16dcf7` (PR #4611) introduced
the  option to ignore `UniqueConstraintViolation`` errors for specified indexes/ constraints/ keys.
This change does the same for the domain, route and service instance model.

The ignore logic is already tested in the vcap_relations plugin (see #4611).
johha added a commit that referenced this pull request Nov 5, 2025
Commit `8b932f9f9498d2d222b981aa7dc5db7a6d16dcf7` (PR #4611) introduced
the  option to ignore `UniqueConstraintViolation`` errors for specified indexes/ constraints/ keys.
This change does the same for the domain, route and service instance model.

The ignore logic is already tested in the vcap_relations plugin (see #4611).
sethboyles pushed a commit to atanas-attodorov-wq/cloud_controller_ng that referenced this pull request Dec 11, 2025
…dry#4641)

Commit `8b932f9f9498d2d222b981aa7dc5db7a6d16dcf7` (PR cloudfoundry#4611) introduced
the  option to ignore `UniqueConstraintViolation`` errors for specified indexes/ constraints/ keys.
This change does the same for the domain, route and service instance model.

The ignore logic is already tested in the vcap_relations plugin (see cloudfoundry#4611).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants