Project

General

Profile

Patch #6035 » subtasking_2.patch

Yoichi Izumi, 2010-10-09 06:39

View differences:

db/migrate/20100313132032_add_issues_nested_sets_columns.rb
1 1
class AddIssuesNestedSetsColumns < ActiveRecord::Migration
2 2
  def self.up
3
    add_column :issues, :parent_id, :integer, :default => nil
4
    add_column :issues, :root_id, :integer, :default => nil
5
    add_column :issues, :lft, :integer, :default => nil
6
    add_column :issues, :rgt, :integer, :default => nil
7
    
8
    Issue.update_all("parent_id = NULL, root_id = id, lft = 1, rgt = 2")
3
    # when the database already used redmine_subtasks plugin
4
    if ['parent_id','lft','rgt'].all? {|c| Issue.column_names.include?(c) }
5
      add_column :issues, :root_id, :integer, :default => nil
6
      Issue.reset_column_information
7
      
8
      Issue.update_all("parent_id = NULL, root_id = id, lft = 1, rgt = 2", "parent_id IS NULL")
9
      Issue.find_by_sql("SELECT t1.* FROM issues t1 LEFT JOIN issues t2 ON t1.lft BETWEEN t2.lft AND t2.rgt WHERE t1.parent_id is not null GROUP BY t1.id ORDER BY count(t2.id)").each do |issue|
10
         issue.update_attribute(:parent_issue_id, issue.parent_id)
11
      end
12
    else
13
      add_column :issues, :parent_id, :integer, :default => nil
14
      add_column :issues, :root_id, :integer, :default => nil
15
      add_column :issues, :lft, :integer, :default => nil
16
      add_column :issues, :rgt, :integer, :default => nil
17

  
18
      Issue.update_all("parent_id = NULL, root_id = id, lft = 1, rgt = 2")
19
    end
9 20
  end
10 21

  
11 22
  def self.down
(3-3/3)