Index: lib/tasks/migrate_from_mantis.rake =================================================================== --- lib/tasks/migrate_from_mantis.rake (Revision 13419) +++ lib/tasks/migrate_from_mantis.rake (Arbeitskopie) @@ -41,13 +41,13 @@ } priorities = IssuePriority.all - DEFAULT_PRIORITY = priorities[2] - PRIORITY_MAPPING = {10 => priorities[1], # none - 20 => priorities[1], # low - 30 => priorities[2], # normal - 40 => priorities[3], # high - 50 => priorities[4], # urgent - 60 => priorities[5] # immediate + DEFAULT_PRIORITY = priorities[1] + PRIORITY_MAPPING = {10 => priorities[0], # none + 20 => priorities[0], # low + 30 => priorities[1], # normal + 40 => priorities[2], # high + 50 => priorities[3], # urgent + 60 => priorities[4] # immediate } TRACKER_BUG = Tracker.find_by_position(1) @@ -119,10 +119,14 @@ has_many :members, :class_name => "MantisProjectUser", :foreign_key => :project_id def identifier - read_attribute(:name).downcase.gsub(/[^a-z0-9\-]+/, '-').slice(0, Project::IDENTIFIER_MAX_LENGTH) + read_attribute(:name).slice(0, Project::IDENTIFIER_MAX_LENGTH).downcase.gsub(/[^a-z0-9\-]+/, '-') end end - + + class MantisProjectHierarchy < ActiveRecord::Base + self.table_name = :mantis_project_hierarchy_table + end + class MantisVersion < ActiveRecord::Base self.table_name = :mantis_project_version_table @@ -136,7 +140,10 @@ end class MantisCategory < ActiveRecord::Base - self.table_name = :mantis_project_category_table + self.table_name = :mantis_category_table + def category + read_attribute(:name).slice(0,30) + end end class MantisProjectUser < ActiveRecord::Base @@ -146,6 +153,7 @@ class MantisBug < ActiveRecord::Base self.table_name = :mantis_bug_table belongs_to :bug_text, :class_name => "MantisBugText", :foreign_key => :bug_text_id + belongs_to :category, :class_name => "MantisCategory", :foreign_key => :category_id has_many :bug_notes, :class_name => "MantisBugNote", :foreign_key => :bug_id has_many :bug_files, :class_name => "MantisBugFile", :foreign_key => :bug_id has_many :bug_monitors, :class_name => "MantisBugMonitor", :foreign_key => :bug_id @@ -213,7 +221,7 @@ class MantisCustomField < ActiveRecord::Base self.table_name = :mantis_custom_field_table - set_inheritance_column :none + self.inheritance_column = :none has_many :values, :class_name => "MantisCustomFieldString", :foreign_key => :field_id has_many :projects, :class_name => "MantisCustomFieldProject", :foreign_key => :field_id @@ -245,7 +253,7 @@ u = User.new :firstname => encode(user.firstname), :lastname => encode(user.lastname), :mail => user.email, - :last_login_on => user.last_visit + :last_login_on => (user.last_visit ? Time.at(user.last_visit).to_date : nil) u.login = user.username u.password = 'mantis' u.status = User::STATUS_LOCKED if user.enabled != 1 @@ -262,14 +270,18 @@ Project.destroy_all projects_map = {} versions_map = {} + versions_locked = [] categories_map = {} + inheritable_categories = false MantisProject.all.each do |project| p = Project.new :name => encode(project.name), :description => encode(project.description) + inheritable_categories = p.has_attribute?("inherit_categs") unless inheritable_categories p.identifier = project.identifier + p.is_public = (project.view_state == 10) next unless p.save projects_map[project.id] = p.id - p.enabled_module_names = ['issue_tracking', 'news', 'wiki'] + p.enabled_module_names = ['issue_tracking', 'news', 'wiki', 'calendar', 'gantt', 'time_tracking'] p.trackers << TRACKER_BUG unless p.trackers.include?(TRACKER_BUG) p.trackers << TRACKER_FEATURE unless p.trackers.include?(TRACKER_FEATURE) print '.' @@ -286,8 +298,12 @@ project.versions.each do |version| v = Version.new :name => encode(version.version), :description => encode(version.description), - :effective_date => (version.date_order ? version.date_order.to_date : nil) + :effective_date => (version.date_order ? Time.at(version.date_order).to_date : nil) v.project = p + # we cannot directly lock versions, cause otherwise some bugs will not be migrated, that why we remember the versions to be locked after issues migration + if version.obsolete == 1 + versions_locked << v + end v.save versions_map[version.id] = v.id end @@ -294,7 +310,7 @@ # Project categories project.categories.each do |category| - g = IssueCategory.new :name => category.category[0,30] + g = IssueCategory.new :name => category.category g.project = p g.save categories_map[category.category] = g.id @@ -301,9 +317,33 @@ end end puts + + # Project Hierarchy + print "Making Project Hierarchy" + MantisProjectHierarchy.find(:all).each do |link| + next unless p = Project.find_by_id(projects_map[link.child_id]) + p.set_parent!(projects_map[link.parent_id]) + if link.inherit_parent == 1 + if inheritable_categories # p.has_attribute?("inherit_categs") + p.inherit_categs = 1 + end + # Turn on the users inheritance + p.inherit_members = 1 + p.save + # Turn on the versions inheritance + parent_project = Project.find_by_id(projects_map[link.parent_id]) + parent_project.versions.each do |version| + version.sharing = 'descendants' + version.save + end + end + print '.' + end + puts # Bugs print "Migrating bugs" + ActiveRecord::Base.record_timestamps = false Issue.destroy_all issues_map = {} keep_bug_ids = (Issue.count == 0) @@ -313,13 +353,31 @@ :subject => encode(bug.summary), :description => encode(bug.bug_text.full_description), :priority => PRIORITY_MAPPING[bug.priority] || DEFAULT_PRIORITY, - :created_on => bug.date_submitted, - :updated_on => bug.last_updated + :created_on => (bug.date_submitted ? Time.at(bug.date_submitted).to_date : nil), + :updated_on => (bug.last_updated ? Time.at(bug.last_updated).to_date : nil) i.author = User.find_by_id(users_map[bug.reporter_id]) - i.category = IssueCategory.find_by_project_id_and_name(i.project_id, bug.category[0,30]) unless bug.category.blank? - i.fixed_version = Version.find_by_project_id_and_name(i.project_id, bug.fixed_in_version) unless bug.fixed_in_version.blank? + i.category = IssueCategory.find_by_project_id_and_name(i.project_id, bug.category.category) unless bug.category.blank? + if (i.category.nil?) && inheritable_categories + p = Project.find_by_id(projects_map[bug.project_id]) + i.category = p.inherited_categories.find{|c| c.name == bug.category.category} + end + if !(bug.fixed_in_version.blank? && bug.target_version.blank?) + p = Project.find_by_id(projects_map[bug.project_id]) + if !bug.fixed_in_version.blank? + vv = bug.fixed_in_version + else + vv = bug.target_version + end + i.fixed_version = p.shared_versions.find{|v| v.name == vv} + end i.status = STATUS_MAPPING[bug.status] || DEFAULT_STATUS i.tracker = (bug.severity == 10 ? TRACKER_FEATURE : TRACKER_BUG) + i.start_date = Time.at(bug.date_submitted).to_date + if bug.due_date > bug.date_submitted + i.due_date = Time.at(bug.due_date).to_date + elsif bug.status >= 80 && i.updated_on > i.start_date + i.due_date = i.updated_on + end i.id = bug.id if keep_bug_ids next unless i.save issues_map[bug.id] = i.id @@ -337,7 +395,7 @@ bug.bug_notes.each do |note| next unless users_map[note.reporter_id] n = Journal.new :notes => encode(note.bug_note_text.note), - :created_on => note.date_submitted + :created_on => Time.at(note.date_submitted).to_date n.user = User.find_by_id(users_map[note.reporter_id]) n.journalized = i n.save @@ -345,7 +403,7 @@ # Bug files bug.bug_files.each do |file| - a = Attachment.new :created_on => file.date_added + a = Attachment.new :created_on => (file.date_added ? Time.at(file.date_added).to_date : nil) a.file = file a.author = User.first a.container = i @@ -359,11 +417,19 @@ end end + # Locking versions after all bugs where migrated, otherwise no bugs with the locked version as target will be migrated + versions_locked.each do |version| + version.status = 'locked' + version.save + end + # update issue id sequence if needed (postgresql) Issue.connection.reset_pk_sequence!(Issue.table_name) if Issue.connection.respond_to?('reset_pk_sequence!') puts # Bug relationships + #debugger + ActiveRecord::Base.record_timestamps = true print "Migrating bug relations" MantisBugRelationship.all.each do |relation| next unless issues_map[relation.source_bug_id] && issues_map[relation.destination_bug_id] @@ -384,7 +450,7 @@ n = News.new :project_id => projects_map[news.project_id], :title => encode(news.headline[0..59]), :description => encode(news.body), - :created_on => news.date_posted + :created_on => Time.at(news.date_posted).to_date n.author = User.find_by_id(users_map[news.poster_id]) n.save print '.'