Skip to content
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

Add unique constraint for life cycle step definition name #17494

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/project/life_cycle_step_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUniquenessIndexToProjectLifeCycleStepDefinitionsName < ActiveRecord::Migration[7.1]
def change
add_index :project_life_cycle_step_definitions, :name, unique: true
end
end
9 changes: 9 additions & 0 deletions spec/models/project/life_cycle_step_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading