Commit b946b42a authored by Andy Soiron's avatar Andy Soiron Committed by James Fargher

Service validation for project and type uniqueness

This validation prevents duplicated services of the
same type in the same project. The validation can
only run on create because it is possible that the
database holds invalid data. If this is the case,
updating a service would fail.
parent 91867c3a
...@@ -34,6 +34,7 @@ class Service < ApplicationRecord ...@@ -34,6 +34,7 @@ class Service < ApplicationRecord
validates :project_id, presence: true, unless: -> { template? || instance? } validates :project_id, presence: true, unless: -> { template? || instance? }
validates :project_id, absence: true, if: -> { template? || instance? } validates :project_id, absence: true, if: -> { template? || instance? }
validates :type, uniqueness: { scope: :project_id }, unless: -> { template? || instance? }, on: :create
validates :type, presence: true validates :type, presence: true
validates :template, uniqueness: { scope: :type }, if: -> { template? } validates :template, uniqueness: { scope: :type }, if: -> { template? }
validates :instance, uniqueness: { scope: :type }, if: -> { instance? } validates :instance, uniqueness: { scope: :type }, if: -> { instance? }
......
---
title: Validate uniqueness of project_id and type when a new project service is created
merge_request: 26308
author:
type: fixed
...@@ -7312,7 +7312,7 @@ ...@@ -7312,7 +7312,7 @@
"tag_push_events": true, "tag_push_events": true,
"note_events": true, "note_events": true,
"job_events": true, "job_events": true,
"type": "AssemblaService", "type": "AsanaService",
"category": "common", "category": "common",
"default": false, "default": false,
"wiki_page_events": true "wiki_page_events": true
......
...@@ -125,7 +125,7 @@ ...@@ -125,7 +125,7 @@
}, },
{ {
"id": 101, "id": 101,
"title": "JetBrains TeamCity CI", "title": "Jira",
"project_id": 5, "project_id": 5,
"created_at": "2016-06-14T15:01:51.315Z", "created_at": "2016-06-14T15:01:51.315Z",
"updated_at": "2016-06-14T15:01:51.315Z", "updated_at": "2016-06-14T15:01:51.315Z",
...@@ -139,7 +139,7 @@ ...@@ -139,7 +139,7 @@
"tag_push_events": true, "tag_push_events": true,
"note_events": true, "note_events": true,
"job_events": true, "job_events": true,
"type": "TeamcityService", "type": "JiraService",
"category": "ci", "category": "ci",
"default": false, "default": false,
"wiki_page_events": true "wiki_page_events": true
......
...@@ -56,6 +56,14 @@ describe Service do ...@@ -56,6 +56,14 @@ describe Service do
expect(build(:service, :instance)).to be_invalid expect(build(:service, :instance)).to be_invalid
end end
end end
it 'validates uniqueness of type and project_id on create' do
project = create(:project)
expect(create(:service, project: project, type: 'Service')).to be_valid
expect(build(:service, project: project, type: 'Service').valid?(:create)).to eq(false)
expect(build(:service, project: project, type: 'Service').valid?(:update)).to eq(true)
end
end end
describe 'Scopes' do describe 'Scopes' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment