comparison lib/tasks/migrate_from_mantis.rake @ 1295:622f24f53b42 redmine-2.3

Update to Redmine SVN revision 11972 on 2.3-stable branch
author Chris Cannam
date Fri, 14 Jun 2013 09:02:21 +0100
parents 433d4f72a19b
children
comparison
equal deleted inserted replaced
1294:3e4c3460b6ca 1295:622f24f53b42
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 desc 'Mantis migration script' 18 desc 'Mantis migration script'
19 19
20 require 'active_record' 20 require 'active_record'
21 require 'iconv' 21 require 'iconv' if RUBY_VERSION < '1.9'
22 require 'pp' 22 require 'pp'
23 23
24 namespace :redmine do 24 namespace :redmine do
25 task :migrate_from_mantis => :environment do 25 task :migrate_from_mantis => :environment do
26 26
28 28
29 DEFAULT_STATUS = IssueStatus.default 29 DEFAULT_STATUS = IssueStatus.default
30 assigned_status = IssueStatus.find_by_position(2) 30 assigned_status = IssueStatus.find_by_position(2)
31 resolved_status = IssueStatus.find_by_position(3) 31 resolved_status = IssueStatus.find_by_position(3)
32 feedback_status = IssueStatus.find_by_position(4) 32 feedback_status = IssueStatus.find_by_position(4)
33 closed_status = IssueStatus.find :first, :conditions => { :is_closed => true } 33 closed_status = IssueStatus.where(:is_closed => true).first
34 STATUS_MAPPING = {10 => DEFAULT_STATUS, # new 34 STATUS_MAPPING = {10 => DEFAULT_STATUS, # new
35 20 => feedback_status, # feedback 35 20 => feedback_status, # feedback
36 30 => DEFAULT_STATUS, # acknowledged 36 30 => DEFAULT_STATUS, # acknowledged
37 40 => DEFAULT_STATUS, # confirmed 37 40 => DEFAULT_STATUS, # confirmed
38 50 => assigned_status, # assigned 38 50 => assigned_status, # assigned
51 } 51 }
52 52
53 TRACKER_BUG = Tracker.find_by_position(1) 53 TRACKER_BUG = Tracker.find_by_position(1)
54 TRACKER_FEATURE = Tracker.find_by_position(2) 54 TRACKER_FEATURE = Tracker.find_by_position(2)
55 55
56 roles = Role.find(:all, :conditions => {:builtin => 0}, :order => 'position ASC') 56 roles = Role.where(:builtin => 0).order('position ASC').all
57 manager_role = roles[0] 57 manager_role = roles[0]
58 developer_role = roles[1] 58 developer_role = roles[1]
59 DEFAULT_ROLE = roles.last 59 DEFAULT_ROLE = roles.last
60 ROLE_MAPPING = {10 => DEFAULT_ROLE, # viewer 60 ROLE_MAPPING = {10 => DEFAULT_ROLE, # viewer
61 25 => DEFAULT_ROLE, # reporter 61 25 => DEFAULT_ROLE, # reporter
239 # Users 239 # Users
240 print "Migrating users" 240 print "Migrating users"
241 User.delete_all "login <> 'admin'" 241 User.delete_all "login <> 'admin'"
242 users_map = {} 242 users_map = {}
243 users_migrated = 0 243 users_migrated = 0
244 MantisUser.find(:all).each do |user| 244 MantisUser.all.each do |user|
245 u = User.new :firstname => encode(user.firstname), 245 u = User.new :firstname => encode(user.firstname),
246 :lastname => encode(user.lastname), 246 :lastname => encode(user.lastname),
247 :mail => user.email, 247 :mail => user.email,
248 :last_login_on => user.last_visit 248 :last_login_on => user.last_visit
249 u.login = user.username 249 u.login = user.username
261 print "Migrating projects" 261 print "Migrating projects"
262 Project.destroy_all 262 Project.destroy_all
263 projects_map = {} 263 projects_map = {}
264 versions_map = {} 264 versions_map = {}
265 categories_map = {} 265 categories_map = {}
266 MantisProject.find(:all).each do |project| 266 MantisProject.all.each do |project|
267 p = Project.new :name => encode(project.name), 267 p = Project.new :name => encode(project.name),
268 :description => encode(project.description) 268 :description => encode(project.description)
269 p.identifier = project.identifier 269 p.identifier = project.identifier
270 next unless p.save 270 next unless p.save
271 projects_map[project.id] = p.id 271 projects_map[project.id] = p.id
345 345
346 # Bug files 346 # Bug files
347 bug.bug_files.each do |file| 347 bug.bug_files.each do |file|
348 a = Attachment.new :created_on => file.date_added 348 a = Attachment.new :created_on => file.date_added
349 a.file = file 349 a.file = file
350 a.author = User.find :first 350 a.author = User.first
351 a.container = i 351 a.container = i
352 a.save 352 a.save
353 end 353 end
354 354
355 # Bug monitors 355 # Bug monitors
363 Issue.connection.reset_pk_sequence!(Issue.table_name) if Issue.connection.respond_to?('reset_pk_sequence!') 363 Issue.connection.reset_pk_sequence!(Issue.table_name) if Issue.connection.respond_to?('reset_pk_sequence!')
364 puts 364 puts
365 365
366 # Bug relationships 366 # Bug relationships
367 print "Migrating bug relations" 367 print "Migrating bug relations"
368 MantisBugRelationship.find(:all).each do |relation| 368 MantisBugRelationship.all.each do |relation|
369 next unless issues_map[relation.source_bug_id] && issues_map[relation.destination_bug_id] 369 next unless issues_map[relation.source_bug_id] && issues_map[relation.destination_bug_id]
370 r = IssueRelation.new :relation_type => RELATION_TYPE_MAPPING[relation.relationship_type] 370 r = IssueRelation.new :relation_type => RELATION_TYPE_MAPPING[relation.relationship_type]
371 r.issue_from = Issue.find_by_id(issues_map[relation.source_bug_id]) 371 r.issue_from = Issue.find_by_id(issues_map[relation.source_bug_id])
372 r.issue_to = Issue.find_by_id(issues_map[relation.destination_bug_id]) 372 r.issue_to = Issue.find_by_id(issues_map[relation.destination_bug_id])
373 pp r unless r.save 373 pp r unless r.save
377 puts 377 puts
378 378
379 # News 379 # News
380 print "Migrating news" 380 print "Migrating news"
381 News.destroy_all 381 News.destroy_all
382 MantisNews.find(:all, :conditions => 'project_id > 0').each do |news| 382 MantisNews.where('project_id > 0').all.each do |news|
383 next unless projects_map[news.project_id] 383 next unless projects_map[news.project_id]
384 n = News.new :project_id => projects_map[news.project_id], 384 n = News.new :project_id => projects_map[news.project_id],
385 :title => encode(news.headline[0..59]), 385 :title => encode(news.headline[0..59]),
386 :description => encode(news.body), 386 :description => encode(news.body),
387 :created_on => news.date_posted 387 :created_on => news.date_posted
393 puts 393 puts
394 394
395 # Custom fields 395 # Custom fields
396 print "Migrating custom fields" 396 print "Migrating custom fields"
397 IssueCustomField.destroy_all 397 IssueCustomField.destroy_all
398 MantisCustomField.find(:all).each do |field| 398 MantisCustomField.all.each do |field|
399 f = IssueCustomField.new :name => field.name[0..29], 399 f = IssueCustomField.new :name => field.name[0..29],
400 :field_format => CUSTOM_FIELD_TYPE_MAPPING[field.format], 400 :field_format => CUSTOM_FIELD_TYPE_MAPPING[field.format],
401 :min_length => field.length_min, 401 :min_length => field.length_min,
402 :max_length => field.length_max, 402 :max_length => field.length_max,
403 :regexp => field.valid_regexp, 403 :regexp => field.valid_regexp,
405 :is_required => field.require_report? 405 :is_required => field.require_report?
406 next unless f.save 406 next unless f.save
407 print '.' 407 print '.'
408 STDOUT.flush 408 STDOUT.flush
409 # Trackers association 409 # Trackers association
410 f.trackers = Tracker.find :all 410 f.trackers = Tracker.all
411 411
412 # Projects association 412 # Projects association
413 field.projects.each do |project| 413 field.projects.each do |project|
414 f.projects << Project.find_by_id(projects_map[project.project_id]) if projects_map[project.project_id] 414 f.projects << Project.find_by_id(projects_map[project.project_id]) if projects_map[project.project_id]
415 end 415 end
438 puts "News: #{News.count}/#{MantisNews.count}" 438 puts "News: #{News.count}/#{MantisNews.count}"
439 puts "Custom fields: #{IssueCustomField.count}/#{MantisCustomField.count}" 439 puts "Custom fields: #{IssueCustomField.count}/#{MantisCustomField.count}"
440 end 440 end
441 441
442 def self.encoding(charset) 442 def self.encoding(charset)
443 @ic = Iconv.new('UTF-8', charset) 443 @charset = charset
444 rescue Iconv::InvalidEncoding
445 return false
446 end 444 end
447 445
448 def self.establish_connection(params) 446 def self.establish_connection(params)
449 constants.each do |const| 447 constants.each do |const|
450 klass = const_get(const) 448 klass = const_get(const)
452 klass.establish_connection params 450 klass.establish_connection params
453 end 451 end
454 end 452 end
455 453
456 def self.encode(text) 454 def self.encode(text)
457 @ic.iconv text 455 if RUBY_VERSION < '1.9'
458 rescue 456 @ic ||= Iconv.new('UTF-8', @charset)
459 text 457 @ic.iconv text
458 else
459 text.to_s.force_encoding(@charset).encode('UTF-8')
460 end
460 end 461 end
461 end 462 end
462 463
463 puts 464 puts
464 if Redmine::DefaultData::Loader.no_data? 465 if Redmine::DefaultData::Loader.no_data?