-
Notifications
You must be signed in to change notification settings - Fork 368
Remove redundant validates_unique where DB unique index already exists #4930
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
c41a3df
66dc1f5
156e48a
41ca880
bca2128
84cd8d4
bb30c98
c496a36
be178e2
0b73ef9
240dd21
8c7d5ed
3e6166d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,8 @@ def create | |
| raise e | ||
| end | ||
| self | ||
| rescue Sequel::ValidationFailed | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add a comment that "errors have been added"... |
||
| nil | ||
| end | ||
|
|
||
| def update | ||
|
|
@@ -50,6 +52,8 @@ def update | |
| synchronize_services_and_plans! | ||
| end | ||
| self | ||
| rescue Sequel::ValidationFailed | ||
| nil | ||
| end | ||
|
|
||
| delegate :errors, to: :broker | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,10 +13,9 @@ module VCAP::CloudController | |
|
|
||
| context "when there's another domain with the same name" do | ||
| it 'fails to validate' do | ||
| other_domain = described_class.make | ||
| other_domain.name = subject.name | ||
| expect(other_domain).not_to be_valid | ||
| expect(other_domain.errors[:name]).to include(:unique) | ||
| expect do | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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... |
||
| described_class.make(name: subject.name) | ||
| end.to raise_error(Sequel::ValidationFailed, /already reserved by another domain|unique/) | ||
| end | ||
| end | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -110,26 +110,6 @@ module VCAP::CloudController | |
| end.to raise_error(OrganizationCreate::Error, "Organization '#{name}' already exists.") | ||
| end | ||
| end | ||
|
|
||
| context 'when creating organizations concurrently' do | ||
| let(:name) { 'Alfredo' } | ||
|
|
||
| it 'ensures one creation is successful and the other fails due to name conflict' do | ||
| # First request, should succeed | ||
| message = VCAP::CloudController::OrganizationUpdateMessage.new(name:) | ||
| expect do | ||
| 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. implemented in 3e6166d |
||
| allow_any_instance_of(Organization).to receive(:validate).and_return(true) | ||
|
|
||
| # Second request, should fail with correct error | ||
| expect do | ||
| org_create.create(message) | ||
| end.to raise_error(OrganizationCreate::Error, "Organization 'Alfredo' already exists.") | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -132,23 +132,6 @@ module CloudController | |
| end | ||
| end | ||
| end | ||
|
|
||
| describe 'when creating a service broker with the same name concurrently' do | ||
| it 'ensures one creation is successful and the other fails due to name conflict' do | ||
| # First request, should succeed | ||
| expect do | ||
| action.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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. But in this file I don't see a check for the uniqueness error, so this one should be added.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. implemented in 3e6166d |
||
| allow_any_instance_of(ServiceBroker).to receive(:validate).and_return(true) | ||
|
|
||
| # Second request, should fail with correct error | ||
| expect do | ||
| action.create(message) | ||
| end.to raise_error(V3::ServiceBrokerCreate::InvalidServiceBroker) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,26 +79,6 @@ module VCAP::CloudController | |
| end.to raise_error(SpaceCreate::Error, 'Name must be unique per organization') | ||
| end | ||
| end | ||
|
|
||
| context 'when creating spaces concurrently' do | ||
| let(:name) { 'Rose' } | ||
|
|
||
| it 'ensures one creation is successful and the other fails due to name conflict' do | ||
| # First request, should succeed | ||
| message = VCAP::CloudController::SpaceCreateMessage.new(name:) | ||
| expect do | ||
| 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. implemented in 3e6166d |
||
| allow_any_instance_of(Space).to receive(:validate).and_return(true) | ||
|
|
||
| # Second request, should fail with correct error | ||
| expect do | ||
| SpaceCreate.new(user_audit_info:).create(org, message) | ||
| end.to raise_error(SpaceCreate::Error, 'Name must be unique per organization') | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -202,23 +202,6 @@ module VCAP::CloudController | |
| end | ||
| end | ||
| end | ||
|
|
||
| context 'when creating space quota with the same name concurrently' do | ||
| it 'ensures one creation is successful and the other fails due to name conflict' do | ||
| # First request, should succeed | ||
| expect do | ||
| space_quotas_create.create(message, organization: org) | ||
| end.not_to raise_error | ||
|
|
||
| # Mock the validation for the second request to simulate the race condition and trigger a unique constraint violation | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. implemented in 3e6166d |
||
| allow_any_instance_of(SpaceQuotaDefinition).to receive(:validate).and_return(true) | ||
|
|
||
| # Second request, should fail with correct error | ||
| expect do | ||
| space_quotas_create.create(message, organization: org) | ||
| end.to raise_error(SpaceQuotasCreate::Error, "Space Quota 'my-name' already exists.") | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -119,26 +119,6 @@ module VCAP::CloudController | |
| end.to raise_error(StackCreate::Error, 'Name must be unique') | ||
| end | ||
| end | ||
|
|
||
| context 'when creating stack with the same name concurrently' do | ||
| let(:name) { 'Gaby' } | ||
|
|
||
| it 'ensures one creation is successful and the other fails due to name conflict' do | ||
| message = VCAP::CloudController::StackCreateMessage.new(name:) | ||
| # First request, should succeed | ||
| expect do | ||
| stack_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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. implemented in 3e6166d |
||
| allow_any_instance_of(Stack).to receive(:validate).and_return(true) | ||
|
|
||
| # Second request, should fail with correct error | ||
| expect do | ||
| stack_create.create(message) | ||
| end.to raise_error(StackCreate::Error, 'Name must be unique') | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
There was a problem hiding this comment.
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.