Commit 625fb2f29d99ab21de2497a36512be69c80e39be

Authored by Dmitriy Zaporozhets
1 parent 0a0b0e1e

Add iids to milestones. Moved iids logic to separate concern

app/models/concerns/internal_id.rb 0 → 100644
@@ -0,0 +1,17 @@ @@ -0,0 +1,17 @@
  1 +module InternalId
  2 + extend ActiveSupport::Concern
  3 +
  4 + included do
  5 + validate :set_iid, on: :create
  6 + validates :iid, presence: true, numericality: true
  7 + end
  8 +
  9 + def set_iid
  10 + max_iid = project.send(self.class.name.tableize).maximum(:iid)
  11 + self.iid = max_iid.to_i + 1
  12 + end
  13 +
  14 + def to_param
  15 + iid.to_s
  16 + end
  17 +end
app/models/concerns/issuable.rb
@@ -16,8 +16,6 @@ module Issuable @@ -16,8 +16,6 @@ module Issuable
16 16
17 validates :author, presence: true 17 validates :author, presence: true
18 validates :title, presence: true, length: { within: 0..255 } 18 validates :title, presence: true, length: { within: 0..255 }
19 - validate :set_iid, on: :create  
20 - validates :iid, presence: true, numericality: true  
21 19
22 scope :authored, ->(user) { where(author_id: user) } 20 scope :authored, ->(user) { where(author_id: user) }
23 scope :assigned_to, ->(u) { where(assignee_id: u.id)} 21 scope :assigned_to, ->(u) { where(assignee_id: u.id)}
@@ -47,15 +45,6 @@ module Issuable @@ -47,15 +45,6 @@ module Issuable
47 end 45 end
48 end 46 end
49 47
50 - def set_iid  
51 - max_iid = project.send(self.class.name.tableize).maximum(:iid)  
52 - self.iid = max_iid.to_i + 1  
53 - end  
54 -  
55 - def to_param  
56 - iid.to_s  
57 - end  
58 -  
59 def today? 48 def today?
60 Date.today == created_at.to_date 49 Date.today == created_at.to_date
61 end 50 end
app/models/issue.rb
@@ -17,8 +17,8 @@ @@ -17,8 +17,8 @@
17 # 17 #
18 18
19 class Issue < ActiveRecord::Base 19 class Issue < ActiveRecord::Base
20 -  
21 include Issuable 20 include Issuable
  21 + include InternalId
22 22
23 belongs_to :project 23 belongs_to :project
24 validates :project, presence: true 24 validates :project, presence: true
app/models/merge_request.rb
@@ -23,8 +23,8 @@ require Rails.root.join(&quot;app/models/commit&quot;) @@ -23,8 +23,8 @@ require Rails.root.join(&quot;app/models/commit&quot;)
23 require Rails.root.join("lib/static_model") 23 require Rails.root.join("lib/static_model")
24 24
25 class MergeRequest < ActiveRecord::Base 25 class MergeRequest < ActiveRecord::Base
26 -  
27 include Issuable 26 include Issuable
  27 + include InternalId
28 28
29 belongs_to :target_project, foreign_key: :target_project_id, class_name: "Project" 29 belongs_to :target_project, foreign_key: :target_project_id, class_name: "Project"
30 belongs_to :source_project, foreign_key: :source_project_id, class_name: "Project" 30 belongs_to :source_project, foreign_key: :source_project_id, class_name: "Project"
app/models/milestone.rb
@@ -13,6 +13,8 @@ @@ -13,6 +13,8 @@
13 # 13 #
14 14
15 class Milestone < ActiveRecord::Base 15 class Milestone < ActiveRecord::Base
  16 + include InternalId
  17 +
16 attr_accessible :title, :description, :due_date, :state_event, :author_id_of_changes 18 attr_accessible :title, :description, :due_date, :state_event, :author_id_of_changes
17 attr_accessor :author_id_of_changes 19 attr_accessor :author_id_of_changes
18 20
db/migrate/20130821090531_add_internal_ids_to_milestones.rb 0 → 100644
@@ -0,0 +1,5 @@ @@ -0,0 +1,5 @@
  1 +class AddInternalIdsToMilestones < ActiveRecord::Migration
  2 + def change
  3 + add_column :milestones, :iid, :integer
  4 + end
  5 +end
@@ -11,7 +11,7 @@ @@ -11,7 +11,7 @@
11 # 11 #
12 # It's strongly recommended to check this file into your version control system. 12 # It's strongly recommended to check this file into your version control system.
13 13
14 -ActiveRecord::Schema.define(:version => 20130821090530) do 14 +ActiveRecord::Schema.define(:version => 20130821090531) do
15 15
16 create_table "deploy_keys_projects", :force => true do |t| 16 create_table "deploy_keys_projects", :force => true do |t|
17 t.integer "deploy_key_id", :null => false 17 t.integer "deploy_key_id", :null => false
@@ -119,6 +119,7 @@ ActiveRecord::Schema.define(:version =&gt; 20130821090530) do @@ -119,6 +119,7 @@ ActiveRecord::Schema.define(:version =&gt; 20130821090530) do
119 t.datetime "created_at", :null => false 119 t.datetime "created_at", :null => false
120 t.datetime "updated_at", :null => false 120 t.datetime "updated_at", :null => false
121 t.string "state" 121 t.string "state"
  122 + t.integer "iid"
122 end 123 end
123 124
124 add_index "milestones", ["due_date"], :name => "index_milestones_on_due_date" 125 add_index "milestones", ["due_date"], :name => "index_milestones_on_due_date"
lib/tasks/migrate/migrate_iids.rake
@@ -30,4 +30,19 @@ task migrate_iids: :environment do @@ -30,4 +30,19 @@ task migrate_iids: :environment do
30 end 30 end
31 31
32 puts 'done' 32 puts 'done'
  33 + puts 'Milestones'.yellow
  34 + Milestone.where(iid: nil).find_each(batch_size: 100) do |m|
  35 + begin
  36 + m.set_iid
  37 + if m.update_attribute(:iid, m.iid)
  38 + print '.'
  39 + else
  40 + print 'F'
  41 + end
  42 + rescue
  43 + print 'F'
  44 + end
  45 + end
  46 +
  47 + puts 'done'
33 end 48 end