BigW Consortium Gitlab

Add LabelPriority model

parent de9d3915
......@@ -16,6 +16,7 @@ class Label < ActiveRecord::Base
default_value_for :color, DEFAULT_COLOR
has_many :lists, dependent: :destroy
has_many :priorities, class_name: 'LabelPriority'
has_many :label_links, dependent: :destroy
has_many :issues, through: :label_links, source: :target, source_type: 'Issue'
has_many :merge_requests, through: :label_links, source: :target, source_type: 'MergeRequest'
......@@ -26,8 +27,6 @@ class Label < ActiveRecord::Base
validates :title, presence: true, format: { with: /\A[^,]+\z/ }
validates :title, uniqueness: { scope: [:group_id, :project_id] }
before_save :nullify_priority
default_scope { order(title: :asc) }
scope :templates, -> { where(template: true) }
......@@ -149,10 +148,6 @@ class Label < ActiveRecord::Base
end
end
def nullify_priority
self.priority = nil if priority.blank?
end
def sanitize_title(value)
CGI.unescapeHTML(Sanitize.clean(value.to_s))
end
......
class LabelPriority < ActiveRecord::Base
belongs_to :project
belongs_to :label
validates :project, :label, :priority, presence: true
validates :label_id, uniqueness: { scope: :project_id }
validates :priority, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
end
class CreateLabelPriorities < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = true
DOWNTIME_REASON = 'Prioritezed labels will not work as expected until this migration is complete.'
disable_ddl_transaction!
def up
create_table :label_priorities do |t|
t.references :project, foreign_key: { on_delete: :cascade }, null: false
t.references :label, foreign_key: { on_delete: :cascade }, null: false
t.integer :priority, null: false
t.timestamps null: false
end
execute <<-EOF.strip_heredoc
INSERT INTO label_priorities (project_id, label_id, priority, created_at, updated_at)
SELECT labels.project_id, labels.id, labels.priority, NOW(), NOW()
FROM labels
WHERE labels.project_id IS NOT NULL
AND labels.priority IS NOT NULL;
EOF
add_concurrent_index :label_priorities, [:project_id, :label_id], unique: true
add_concurrent_index :label_priorities, :priority
remove_column :labels, :priority
end
def down
add_column :labels, :priority, :integer
if Gitlab::Database.mysql?
execute <<-EOF.strip_heredoc
UPDATE labels
INNER JOIN label_priorities ON labels.id = label_priorities.label_id AND labels.project_id = label_priorities.project_id
SET labels.priority = label_priorities.priority;
EOF
else
execute <<-EOF.strip_heredoc
UPDATE labels
SET priority = label_priorities.priority
FROM label_priorities
WHERE labels.id = label_priorities.label_id
AND labels.project_id = label_priorities.project_id;
EOF
end
add_concurrent_index :labels, :priority
drop_table :label_priorities
end
end
......@@ -519,6 +519,17 @@ ActiveRecord::Schema.define(version: 20161017095000) do
add_index "label_links", ["label_id"], name: "index_label_links_on_label_id", using: :btree
add_index "label_links", ["target_id", "target_type"], name: "index_label_links_on_target_id_and_target_type", using: :btree
create_table "label_priorities", force: :cascade do |t|
t.integer "project_id", null: false
t.integer "label_id", null: false
t.integer "priority", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "label_priorities", ["priority"], name: "index_label_priorities_on_priority", using: :btree
add_index "label_priorities", ["project_id", "label_id"], name: "index_label_priorities_on_project_id_and_label_id", unique: true, using: :btree
create_table "labels", force: :cascade do |t|
t.string "title"
t.string "color"
......@@ -527,14 +538,12 @@ ActiveRecord::Schema.define(version: 20161017095000) do
t.datetime "updated_at"
t.boolean "template", default: false
t.string "description"
t.integer "priority"
t.text "description_html"
t.string "type"
t.integer "group_id"
end
add_index "labels", ["group_id"], name: "index_labels_on_group_id", using: :btree
add_index "labels", ["priority"], name: "index_labels_on_priority", using: :btree
add_index "labels", ["project_id"], name: "index_labels_on_project_id", using: :btree
add_index "labels", ["title"], name: "index_labels_on_title", using: :btree
......@@ -1216,6 +1225,8 @@ ActiveRecord::Schema.define(version: 20161017095000) do
add_foreign_key "boards", "projects"
add_foreign_key "issue_metrics", "issues", on_delete: :cascade
add_foreign_key "label_priorities", "labels", on_delete: :cascade
add_foreign_key "label_priorities", "projects", on_delete: :cascade
add_foreign_key "labels", "namespaces", column: "group_id", on_delete: :cascade
add_foreign_key "lists", "boards"
add_foreign_key "lists", "labels"
......
FactoryGirl.define do
factory :label_priority do
project factory: :empty_project
label
sequence(:priority)
end
end
require 'spec_helper'
describe LabelPriority, models: true do
describe 'relationships' do
it { is_expected.to belong_to(:project) }
it { is_expected.to belong_to(:label) }
end
describe 'validations' do
it { is_expected.to validate_presence_of(:project) }
it { is_expected.to validate_presence_of(:label) }
it { is_expected.to validate_presence_of(:priority) }
it { is_expected.to validate_numericality_of(:priority).only_integer.is_greater_than_or_equal_to(0) }
it 'validates uniqueness of label_id scoped to project_id' do
create(:label_priority)
expect(subject).to validate_uniqueness_of(:label_id).scoped_to(:project_id)
end
end
end
......@@ -10,6 +10,7 @@ describe Label, models: true do
it { is_expected.to have_many(:issues).through(:label_links).source(:target) }
it { is_expected.to have_many(:label_links).dependent(:destroy) }
it { is_expected.to have_many(:lists).dependent(:destroy) }
it { is_expected.to have_many(:priorities).class_name('LabelPriority') }
end
describe 'validation' 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