diff --git a/db/migrate/20100313132032_add_issues_nested_sets_columns.rb b/db/migrate/20100313132032_add_issues_nested_sets_columns.rb index e7d03e9..ee3e89b 100644 --- a/db/migrate/20100313132032_add_issues_nested_sets_columns.rb +++ b/db/migrate/20100313132032_add_issues_nested_sets_columns.rb @@ -1,11 +1,22 @@ class AddIssuesNestedSetsColumns < ActiveRecord::Migration def self.up - add_column :issues, :parent_id, :integer, :default => nil - add_column :issues, :root_id, :integer, :default => nil - add_column :issues, :lft, :integer, :default => nil - add_column :issues, :rgt, :integer, :default => nil - - Issue.update_all("parent_id = NULL, root_id = id, lft = 1, rgt = 2") + # when the database already used redmine_subtasks plugin + if ['parent_id','lft','rgt'].all? {|c| Issue.column_names.include?(c) } + add_column :issues, :root_id, :integer, :default => nil + Issue.reset_column_information + + Issue.update_all("parent_id = NULL, root_id = id, lft = 1, rgt = 2", "parent_id IS NULL") + 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| + issue.update_attribute(:parent_issue_id, issue.parent_id) + end + else + add_column :issues, :parent_id, :integer, :default => nil + add_column :issues, :root_id, :integer, :default => nil + add_column :issues, :lft, :integer, :default => nil + add_column :issues, :rgt, :integer, :default => nil + + Issue.update_all("parent_id = NULL, root_id = id, lft = 1, rgt = 2") + end end def self.down