Skip to content

Remove redundant validates_unique where DB unique index already exists#4930

Open
serdarozerr wants to merge 13 commits intocloudfoundry:mainfrom
sap-contributions:feature/remove-extra-validates-unique-check
Open

Remove redundant validates_unique where DB unique index already exists#4930
serdarozerr wants to merge 13 commits intocloudfoundry:mainfrom
sap-contributions:feature/remove-extra-validates-unique-check

Conversation

@serdarozerr
Copy link
Contributor

Models with a DB-level unique index were performing an extra SELECT on every save via Sequel's validates_unique. This PR removes that redundant check and relies on around_save to catch Sequel::UniqueConstraintViolation instead, letting the database enforce what it already enforces.

  • 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 requested a review from philippthun March 18, 2026 07:59

def validate
validates_presence :name
validates_unique :name, dataset: Domain.dataset
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with removing this validation is that now another one is triggering (name_overlaps?) which actually checks something else (e.g. "The domain name "foo.com" cannot be created because "bar.foo.com" is already reserved by another domain") and thus results in a wrong error message. There doesn't seem to be a test that verifies that the index-based validation is failing for domains.

other_domain.name = subject.name
expect(other_domain).not_to be_valid
expect(other_domain.errors[:name]).to include(:unique)
expect do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment in app/models/runtime/domain.rb - this now tests something else...


describe 'Validations' do
it { is_expected.to validate_presence :name }
it { is_expected.to validate_uniqueness :name }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we should add a test for the index-based validation.

org_create.create(message)
end.not_to raise_error

# Mock the validation for the second request to simulate the race condition and trigger a unique constraint violation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the complete context could be removed, we don't have to test concurrency if the database enforces uniqueness.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

implemented in 3e6166d

SpaceCreate.new(user_audit_info:).create(org, message)
end.not_to raise_error

# Mock the validation for the second request to simulate the race condition and trigger a unique constraint violation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the complete context could be removed, we don't have to test concurrency if the database enforces uniqueness.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

implemented in 3e6166d


describe 'Validations' do
it { is_expected.to validate_presence :guid }
it { is_expected.to validate_uniqueness :guid }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is uniqueness tested somewhere else?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of same reason that, in action tests, this concurrency test suit already check uniqueness implicitly, i thought adding uniqueness test would be redundant in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

implemented in 3e6166d

it { is_expected.to validate_presence :broker_url }
it { is_expected.to validate_presence :auth_username }
it { is_expected.to validate_presence :auth_password }
it { is_expected.to validate_uniqueness :name, message: Sequel.lit('Name must be unique') }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is uniqueness tested somewhere else?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of same reason that in action tests, this concurrency test suit already check uniqueness implicitly, i thought adding uniqueness test would be redundant in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

implemented in 3e6166d

raise e
end
self
rescue Sequel::ValidationFailed
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a comment that "errors have been added"...

post '/v2/service_brokers', public_body
expect(last_response).to have_status_code(201)

stub_catalog
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you understand why this needs to be added? Because I don't...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This stub added because of the validation in here i assume this validation called just before http call to prevent extra calls if broker is not valid etc.(kind of guard flag) Since we removed uniqueness_validation check, http call hits , so we stub to prevent error throwing.

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.

3 participants