Ensure that we have only one list per label per board

parent c08a14c3
......@@ -6,6 +6,7 @@ class List < ActiveRecord::Base
validates :board, :list_type, presence: true
validates :label, :position, presence: true, if: :label?
validates :label_id, uniqueness: { scope: :board_id }, if: :label?
validates :position, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, if: :label?
delegate :name, to: :label, allow_nil: true, prefix: true
......
class AddUniqueIndexToListsLabelId < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
add_concurrent_index :lists, [:board_id, :label_id], unique: true
end
def down
remove_index :lists, column: [:board_id, :label_id] if index_exists?(:lists, [:board_id, :label_id], unique: true)
end
end
......@@ -550,6 +550,7 @@ ActiveRecord::Schema.define(version: 20160810142633) do
t.datetime "updated_at", null: false
end
add_index "lists", ["board_id", "label_id"], name: "index_lists_on_board_id_and_label_id", unique: true, using: :btree
add_index "lists", ["board_id"], name: "index_lists_on_board_id", using: :btree
add_index "lists", ["label_id"], name: "index_lists_on_label_id", using: :btree
......
......@@ -17,6 +17,12 @@ describe List do
it { is_expected.to validate_presence_of(:position) }
it { is_expected.to validate_numericality_of(:position).only_integer.is_greater_than_or_equal_to(0) }
it 'validates uniqueness of label scoped to board_id' do
create(:list)
expect(subject).to validate_uniqueness_of(:label_id).scoped_to(:board_id)
end
context 'when list_type is set to backlog' do
subject { described_class.new(list_type: :backlog) }
......
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