OSDN Git Service

Add iids to milestones. Moved iids logic to separate concern
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Wed, 21 Aug 2013 09:16:26 +0000 (12:16 +0300)
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Wed, 21 Aug 2013 09:16:26 +0000 (12:16 +0300)
app/models/concerns/internal_id.rb [new file with mode: 0644]
app/models/concerns/issuable.rb
app/models/issue.rb
app/models/merge_request.rb
app/models/milestone.rb
db/migrate/20130821090531_add_internal_ids_to_milestones.rb [new file with mode: 0644]
db/schema.rb
lib/tasks/migrate/migrate_iids.rake

diff --git a/app/models/concerns/internal_id.rb b/app/models/concerns/internal_id.rb
new file mode 100644 (file)
index 0000000..821ed54
--- /dev/null
@@ -0,0 +1,17 @@
+module InternalId
+  extend ActiveSupport::Concern
+
+  included do
+    validate :set_iid, on: :create
+    validates :iid, presence: true, numericality: true
+  end
+
+  def set_iid
+    max_iid = project.send(self.class.name.tableize).maximum(:iid)
+    self.iid = max_iid.to_i + 1
+  end
+
+  def to_param
+    iid.to_s
+  end
+end
index fb08a5a..a05dba7 100644 (file)
@@ -16,8 +16,6 @@ module Issuable
 
     validates :author, presence: true
     validates :title, presence: true, length: { within: 0..255 }
-    validate :set_iid, on: :create
-    validates :iid, presence: true, numericality: true
 
     scope :authored, ->(user) { where(author_id: user) }
     scope :assigned_to, ->(u) { where(assignee_id: u.id)}
@@ -47,15 +45,6 @@ module Issuable
     end
   end
 
-  def set_iid
-    max_iid = project.send(self.class.name.tableize).maximum(:iid)
-    self.iid = max_iid.to_i + 1
-  end
-
-  def to_param
-    iid.to_s
-  end
-
   def today?
     Date.today == created_at.to_date
   end
index ecb881a..35b487d 100644 (file)
@@ -17,8 +17,8 @@
 #
 
 class Issue < ActiveRecord::Base
-
   include Issuable
+  include InternalId
 
   belongs_to :project
   validates :project, presence: true
index d525ad1..190f6ab 100644 (file)
@@ -23,8 +23,8 @@ require Rails.root.join("app/models/commit")
 require Rails.root.join("lib/static_model")
 
 class MergeRequest < ActiveRecord::Base
-
   include Issuable
+  include InternalId
 
   belongs_to :target_project, foreign_key: :target_project_id, class_name: "Project"
   belongs_to :source_project, foreign_key: :source_project_id, class_name: "Project"
index 023b8dd..cfa47f3 100644 (file)
@@ -13,6 +13,8 @@
 #
 
 class Milestone < ActiveRecord::Base
+  include InternalId
+
   attr_accessible :title, :description, :due_date, :state_event, :author_id_of_changes
   attr_accessor :author_id_of_changes
 
diff --git a/db/migrate/20130821090531_add_internal_ids_to_milestones.rb b/db/migrate/20130821090531_add_internal_ids_to_milestones.rb
new file mode 100644 (file)
index 0000000..33e5bae
--- /dev/null
@@ -0,0 +1,5 @@
+class AddInternalIdsToMilestones < ActiveRecord::Migration
+  def change
+    add_column :milestones, :iid, :integer
+  end
+end
index b8e7d3d..429551a 100644 (file)
@@ -11,7 +11,7 @@
 #
 # It's strongly recommended to check this file into your version control system.
 
-ActiveRecord::Schema.define(:version => 20130821090530) do
+ActiveRecord::Schema.define(:version => 20130821090531) do
 
   create_table "deploy_keys_projects", :force => true do |t|
     t.integer  "deploy_key_id", :null => false
@@ -119,6 +119,7 @@ ActiveRecord::Schema.define(:version => 20130821090530) do
     t.datetime "created_at",  :null => false
     t.datetime "updated_at",  :null => false
     t.string   "state"
+    t.integer  "iid"
   end
 
   add_index "milestones", ["due_date"], :name => "index_milestones_on_due_date"
index bc612cd..33271e1 100644 (file)
@@ -30,4 +30,19 @@ task migrate_iids: :environment do
   end
 
   puts 'done'
+  puts 'Milestones'.yellow
+  Milestone.where(iid: nil).find_each(batch_size: 100) do |m|
+    begin
+      m.set_iid
+      if m.update_attribute(:iid, m.iid)
+        print '.'
+      else
+        print 'F'
+      end
+    rescue
+      print 'F'
+    end
+  end
+
+  puts 'done'
 end