From a7756b81db2c15d55d5a15c2ff4dff26a296735d Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Tue, 17 Dec 2024 20:12:50 +0100 Subject: [PATCH 1/2] add unique constraint for life cycle step definition name --- app/models/project/life_cycle_step_definition.rb | 2 +- ...ness_index_to_project_life_cycle_step_definitions_name.rb | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20241217190533_add_uniqueness_index_to_project_life_cycle_step_definitions_name.rb diff --git a/app/models/project/life_cycle_step_definition.rb b/app/models/project/life_cycle_step_definition.rb index 0aadf22d0846..f80cc6116931 100644 --- a/app/models/project/life_cycle_step_definition.rb +++ b/app/models/project/life_cycle_step_definition.rb @@ -35,7 +35,7 @@ class Project::LifeCycleStepDefinition < ApplicationRecord has_many :projects, through: :life_cycle_steps belongs_to :color, optional: false - validates :name, presence: true + validates :name, presence: true, uniqueness: true validates :type, inclusion: { in: %w[Project::StageDefinition Project::GateDefinition], message: :must_be_a_stage_or_gate } validate :validate_type_and_class_name_are_identical diff --git a/db/migrate/20241217190533_add_uniqueness_index_to_project_life_cycle_step_definitions_name.rb b/db/migrate/20241217190533_add_uniqueness_index_to_project_life_cycle_step_definitions_name.rb new file mode 100644 index 000000000000..f6aba362e529 --- /dev/null +++ b/db/migrate/20241217190533_add_uniqueness_index_to_project_life_cycle_step_definitions_name.rb @@ -0,0 +1,5 @@ +class AddUniquenessIndexToProjectLifeCycleStepDefinitionsName < ActiveRecord::Migration[7.1] + def change + add_index :project_life_cycle_step_definitions, :name, unique: true + end +end From 6bd1b169329823213806e000d7dfea17d14f8aaf Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Wed, 18 Dec 2024 15:45:53 +0100 Subject: [PATCH 2/2] add specs for validations of life cycle step definition --- spec/models/project/life_cycle_step_definition_spec.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/spec/models/project/life_cycle_step_definition_spec.rb b/spec/models/project/life_cycle_step_definition_spec.rb index f5c69c5f7f93..8fee988c59ac 100644 --- a/spec/models/project/life_cycle_step_definition_spec.rb +++ b/spec/models/project/life_cycle_step_definition_spec.rb @@ -40,6 +40,15 @@ end describe "validations" do + it { is_expected.to validate_presence_of(:name) } + it { is_expected.to validate_uniqueness_of(:name) } + + it { + expect(subject).to validate_inclusion_of(:type) + .in_array(%w[Project::StageDefinition Project::GateDefinition]) + .with_message(:must_be_a_stage_or_gate) + } + it "is invalid if type and class name do not match" do subject.type = "Project::GateDefinition" expect(subject).not_to be_valid