Commit 1f84518ab7128e302f44085b4ecd8686f3f2ea2d

Authored by Dmitriy Zaporozhets
2 parents d89f26e0 ae7bd9f7

Merge branch 'index_on_iid' into 'master'

 Add uniq db index on project_id+iid

Because:
* its faster
* it solves problem with repeating iid's in one project
CHANGELOG
1 1 v 6.9.0
2 2 - Store Rails cache data in the Redis `cache:gitlab` namespace
3 3 - Adjust MySQL limits for existing installations
  4 + - Add db index on project_id+iid column. This prevents duplicate on iid.
4 5  
5 6 v 6.8.0
6 7 - Ability to at mention users that are participating in issue and merge req. discussion
... ...
app/models/milestone.rb
... ... @@ -25,6 +25,7 @@ class Milestone < ActiveRecord::Base
25 25  
26 26 scope :active, -> { with_state(:active) }
27 27 scope :closed, -> { with_state(:closed) }
  28 + scope :of_projects, ->(ids) { where(project_id: ids) }
28 29  
29 30 validates :title, presence: true
30 31 validates :project, presence: true
... ...
db/migrate/20140416074002_add_index_on_iid.rb 0 → 100644
... ... @@ -0,0 +1,32 @@
  1 +class AddIndexOnIid < ActiveRecord::Migration
  2 + def change
  3 + RemoveDuplicateIid.clean(Issue)
  4 + RemoveDuplicateIid.clean(MergeRequest, 'target_project_id')
  5 + RemoveDuplicateIid.clean(Milestone)
  6 +
  7 + add_index :issues, [:project_id, :iid], unique: true
  8 + add_index :merge_requests, [:target_project_id, :iid], unique: true
  9 + add_index :milestones, [:project_id, :iid], unique: true
  10 + end
  11 +end
  12 +
  13 +class RemoveDuplicateIid
  14 + def self.clean(klass, project_field = 'project_id')
  15 + duplicates = klass.find_by_sql("SELECT iid, #{project_field} FROM #{klass.table_name} GROUP BY #{project_field}, iid HAVING COUNT(*) > 1")
  16 +
  17 + duplicates.each do |duplicate|
  18 + project_id = duplicate.send(project_field)
  19 + iid = duplicate.iid
  20 + items = klass.of_projects(project_id).where(iid: iid)
  21 +
  22 + if items.size > 1
  23 + puts "Remove #{klass.name} duplicates for iid: #{iid} and project_id: #{project_id}"
  24 + items.shift
  25 + items.each do |item|
  26 + item.destroy
  27 + puts '.'
  28 + end
  29 + end
  30 + end
  31 + end
  32 +end
... ...
db/schema.rb
... ... @@ -93,6 +93,7 @@ ActiveRecord::Schema.define(version: 20140416185734) do
93 93 add_index "issues", ["author_id"], name: "index_issues_on_author_id", using: :btree
94 94 add_index "issues", ["created_at"], name: "index_issues_on_created_at", using: :btree
95 95 add_index "issues", ["milestone_id"], name: "index_issues_on_milestone_id", using: :btree
  96 + add_index "issues", ["project_id", "iid"], name: "index_issues_on_project_id_and_iid", unique: true, using: :btree
96 97 add_index "issues", ["project_id"], name: "index_issues_on_project_id", using: :btree
97 98 add_index "issues", ["title"], name: "index_issues_on_title", using: :btree
98 99  
... ... @@ -143,6 +144,7 @@ ActiveRecord::Schema.define(version: 20140416185734) do
143 144 add_index "merge_requests", ["source_branch"], name: "index_merge_requests_on_source_branch", using: :btree
144 145 add_index "merge_requests", ["source_project_id"], name: "index_merge_requests_on_source_project_id", using: :btree
145 146 add_index "merge_requests", ["target_branch"], name: "index_merge_requests_on_target_branch", using: :btree
  147 + add_index "merge_requests", ["target_project_id", "iid"], name: "index_merge_requests_on_target_project_id_and_iid", unique: true, using: :btree
146 148 add_index "merge_requests", ["title"], name: "index_merge_requests_on_title", using: :btree
147 149  
148 150 create_table "milestones", force: true do |t|
... ... @@ -157,6 +159,7 @@ ActiveRecord::Schema.define(version: 20140416185734) do
157 159 end
158 160  
159 161 add_index "milestones", ["due_date"], name: "index_milestones_on_due_date", using: :btree
  162 + add_index "milestones", ["project_id", "iid"], name: "index_milestones_on_project_id_and_iid", unique: true, using: :btree
160 163 add_index "milestones", ["project_id"], name: "index_milestones_on_project_id", using: :btree
161 164  
162 165 create_table "namespaces", force: true do |t|
... ...