diff options
author | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-10-14 22:12:07 +0300 |
---|---|---|
committer | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-10-19 19:58:27 +0300 |
commit | 297892011330ecdd2fa7cbe47fbc6fd4f3b62171 (patch) | |
tree | 6cbbb21eae4f3fa3d28b33599a0ea87fbf0f34ec | |
parent | de9d3915d249a59e65226f6dd2ebe1f50a47306e (diff) |
Add LabelPriority model
-rw-r--r-- | app/models/label.rb | 7 | ||||
-rw-r--r-- | app/models/label_priority.rb | 8 | ||||
-rw-r--r-- | db/migrate/20161014173530_create_label_priorities.rb | 55 | ||||
-rw-r--r-- | db/schema.rb | 15 | ||||
-rw-r--r-- | spec/factories/label_priorities.rb | 7 | ||||
-rw-r--r-- | spec/models/label_priority_spec.rb | 21 | ||||
-rw-r--r-- | spec/models/label_spec.rb | 1 |
7 files changed, 106 insertions, 8 deletions
diff --git a/app/models/label.rb b/app/models/label.rb index 8b775e81db7..3ce4e253035 100644 --- a/app/models/label.rb +++ b/app/models/label.rb @@ -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 diff --git a/app/models/label_priority.rb b/app/models/label_priority.rb new file mode 100644 index 00000000000..5b85e0b6533 --- /dev/null +++ b/app/models/label_priority.rb @@ -0,0 +1,8 @@ +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 diff --git a/db/migrate/20161014173530_create_label_priorities.rb b/db/migrate/20161014173530_create_label_priorities.rb new file mode 100644 index 00000000000..f9d94ebdc70 --- /dev/null +++ b/db/migrate/20161014173530_create_label_priorities.rb @@ -0,0 +1,55 @@ +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 diff --git a/db/schema.rb b/db/schema.rb index 37f0be0e834..e17fdf75f88 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" diff --git a/spec/factories/label_priorities.rb b/spec/factories/label_priorities.rb new file mode 100644 index 00000000000..f25939d2d3e --- /dev/null +++ b/spec/factories/label_priorities.rb @@ -0,0 +1,7 @@ +FactoryGirl.define do + factory :label_priority do + project factory: :empty_project + label + sequence(:priority) + end +end diff --git a/spec/models/label_priority_spec.rb b/spec/models/label_priority_spec.rb new file mode 100644 index 00000000000..5f7fa3aa047 --- /dev/null +++ b/spec/models/label_priority_spec.rb @@ -0,0 +1,21 @@ +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 diff --git a/spec/models/label_spec.rb b/spec/models/label_spec.rb index c6e1ea19987..4af0fb6afa9 100644 --- a/spec/models/label_spec.rb +++ b/spec/models/label_spec.rb @@ -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 |