annotate lib/tasks/migrate_from_mantis.rake @ 1524:82fac3dcf466 redmine-2.5-integration

Fix failure to interpret Javascript when autocompleting members for project
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 11 Sep 2014 10:24:38 +0100
parents e248c7af89ec
children
rev   line source
Chris@1115 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1115 3 #
Chris@1115 4 # This program is free software; you can redistribute it and/or
Chris@1115 5 # modify it under the terms of the GNU General Public License
Chris@1115 6 # as published by the Free Software Foundation; either version 2
Chris@1115 7 # of the License, or (at your option) any later version.
Chris@1115 8 #
Chris@1115 9 # This program is distributed in the hope that it will be useful,
Chris@1115 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1115 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1115 12 # GNU General Public License for more details.
Chris@1115 13 #
Chris@1115 14 # You should have received a copy of the GNU General Public License
Chris@1115 15 # along with this program; if not, write to the Free Software
Chris@1115 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1115 17
Chris@1115 18 desc 'Mantis migration script'
Chris@1115 19
Chris@1115 20 require 'active_record'
Chris@1464 21 require 'iconv' if RUBY_VERSION < '1.9'
Chris@1115 22 require 'pp'
Chris@1115 23
Chris@1115 24 namespace :redmine do
Chris@1115 25 task :migrate_from_mantis => :environment do
Chris@1115 26
Chris@1115 27 module MantisMigrate
Chris@1115 28
Chris@1115 29 DEFAULT_STATUS = IssueStatus.default
Chris@1115 30 assigned_status = IssueStatus.find_by_position(2)
Chris@1115 31 resolved_status = IssueStatus.find_by_position(3)
Chris@1115 32 feedback_status = IssueStatus.find_by_position(4)
Chris@1464 33 closed_status = IssueStatus.where(:is_closed => true).first
Chris@1115 34 STATUS_MAPPING = {10 => DEFAULT_STATUS, # new
Chris@1115 35 20 => feedback_status, # feedback
Chris@1115 36 30 => DEFAULT_STATUS, # acknowledged
Chris@1115 37 40 => DEFAULT_STATUS, # confirmed
Chris@1115 38 50 => assigned_status, # assigned
Chris@1115 39 80 => resolved_status, # resolved
Chris@1115 40 90 => closed_status # closed
Chris@1115 41 }
Chris@1115 42
Chris@1115 43 priorities = IssuePriority.all
Chris@1115 44 DEFAULT_PRIORITY = priorities[2]
Chris@1115 45 PRIORITY_MAPPING = {10 => priorities[1], # none
Chris@1115 46 20 => priorities[1], # low
Chris@1115 47 30 => priorities[2], # normal
Chris@1115 48 40 => priorities[3], # high
Chris@1115 49 50 => priorities[4], # urgent
Chris@1115 50 60 => priorities[5] # immediate
Chris@1115 51 }
Chris@1115 52
Chris@1115 53 TRACKER_BUG = Tracker.find_by_position(1)
Chris@1115 54 TRACKER_FEATURE = Tracker.find_by_position(2)
Chris@1115 55
Chris@1464 56 roles = Role.where(:builtin => 0).order('position ASC').all
Chris@1115 57 manager_role = roles[0]
Chris@1115 58 developer_role = roles[1]
Chris@1115 59 DEFAULT_ROLE = roles.last
Chris@1115 60 ROLE_MAPPING = {10 => DEFAULT_ROLE, # viewer
Chris@1115 61 25 => DEFAULT_ROLE, # reporter
Chris@1115 62 40 => DEFAULT_ROLE, # updater
Chris@1115 63 55 => developer_role, # developer
Chris@1115 64 70 => manager_role, # manager
Chris@1115 65 90 => manager_role # administrator
Chris@1115 66 }
Chris@1115 67
Chris@1115 68 CUSTOM_FIELD_TYPE_MAPPING = {0 => 'string', # String
Chris@1115 69 1 => 'int', # Numeric
Chris@1115 70 2 => 'int', # Float
Chris@1115 71 3 => 'list', # Enumeration
Chris@1115 72 4 => 'string', # Email
Chris@1115 73 5 => 'bool', # Checkbox
Chris@1115 74 6 => 'list', # List
Chris@1115 75 7 => 'list', # Multiselection list
Chris@1115 76 8 => 'date', # Date
Chris@1115 77 }
Chris@1115 78
Chris@1115 79 RELATION_TYPE_MAPPING = {1 => IssueRelation::TYPE_RELATES, # related to
Chris@1115 80 2 => IssueRelation::TYPE_RELATES, # parent of
Chris@1115 81 3 => IssueRelation::TYPE_RELATES, # child of
Chris@1115 82 0 => IssueRelation::TYPE_DUPLICATES, # duplicate of
Chris@1115 83 4 => IssueRelation::TYPE_DUPLICATES # has duplicate
Chris@1115 84 }
Chris@1115 85
Chris@1115 86 class MantisUser < ActiveRecord::Base
Chris@1115 87 self.table_name = :mantis_user_table
Chris@1115 88
Chris@1115 89 def firstname
Chris@1115 90 @firstname = realname.blank? ? username : realname.split.first[0..29]
Chris@1115 91 @firstname
Chris@1115 92 end
Chris@1115 93
Chris@1115 94 def lastname
Chris@1115 95 @lastname = realname.blank? ? '-' : realname.split[1..-1].join(' ')[0..29]
Chris@1115 96 @lastname = '-' if @lastname.blank?
Chris@1115 97 @lastname
Chris@1115 98 end
Chris@1115 99
Chris@1115 100 def email
Chris@1115 101 if read_attribute(:email).match(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i) &&
Chris@1115 102 !User.find_by_mail(read_attribute(:email))
Chris@1115 103 @email = read_attribute(:email)
Chris@1115 104 else
Chris@1115 105 @email = "#{username}@foo.bar"
Chris@1115 106 end
Chris@1115 107 end
Chris@1115 108
Chris@1115 109 def username
Chris@1115 110 read_attribute(:username)[0..29].gsub(/[^a-zA-Z0-9_\-@\.]/, '-')
Chris@1115 111 end
Chris@1115 112 end
Chris@1115 113
Chris@1115 114 class MantisProject < ActiveRecord::Base
Chris@1115 115 self.table_name = :mantis_project_table
Chris@1115 116 has_many :versions, :class_name => "MantisVersion", :foreign_key => :project_id
Chris@1115 117 has_many :categories, :class_name => "MantisCategory", :foreign_key => :project_id
Chris@1115 118 has_many :news, :class_name => "MantisNews", :foreign_key => :project_id
Chris@1115 119 has_many :members, :class_name => "MantisProjectUser", :foreign_key => :project_id
Chris@1115 120
Chris@1115 121 def identifier
Chris@1464 122 read_attribute(:name).downcase.gsub(/[^a-z0-9\-]+/, '-').slice(0, Project::IDENTIFIER_MAX_LENGTH)
Chris@1115 123 end
Chris@1115 124 end
Chris@1115 125
Chris@1115 126 class MantisVersion < ActiveRecord::Base
Chris@1115 127 self.table_name = :mantis_project_version_table
Chris@1115 128
Chris@1115 129 def version
Chris@1115 130 read_attribute(:version)[0..29]
Chris@1115 131 end
Chris@1115 132
Chris@1115 133 def description
Chris@1115 134 read_attribute(:description)[0..254]
Chris@1115 135 end
Chris@1115 136 end
Chris@1115 137
Chris@1115 138 class MantisCategory < ActiveRecord::Base
Chris@1115 139 self.table_name = :mantis_project_category_table
Chris@1115 140 end
Chris@1115 141
Chris@1115 142 class MantisProjectUser < ActiveRecord::Base
Chris@1115 143 self.table_name = :mantis_project_user_list_table
Chris@1115 144 end
Chris@1115 145
Chris@1115 146 class MantisBug < ActiveRecord::Base
Chris@1115 147 self.table_name = :mantis_bug_table
Chris@1115 148 belongs_to :bug_text, :class_name => "MantisBugText", :foreign_key => :bug_text_id
Chris@1115 149 has_many :bug_notes, :class_name => "MantisBugNote", :foreign_key => :bug_id
Chris@1115 150 has_many :bug_files, :class_name => "MantisBugFile", :foreign_key => :bug_id
Chris@1115 151 has_many :bug_monitors, :class_name => "MantisBugMonitor", :foreign_key => :bug_id
Chris@1115 152 end
Chris@1115 153
Chris@1115 154 class MantisBugText < ActiveRecord::Base
Chris@1115 155 self.table_name = :mantis_bug_text_table
Chris@1115 156
Chris@1115 157 # Adds Mantis steps_to_reproduce and additional_information fields
Chris@1115 158 # to description if any
Chris@1115 159 def full_description
Chris@1115 160 full_description = description
Chris@1115 161 full_description += "\n\n*Steps to reproduce:*\n\n#{steps_to_reproduce}" unless steps_to_reproduce.blank?
Chris@1115 162 full_description += "\n\n*Additional information:*\n\n#{additional_information}" unless additional_information.blank?
Chris@1115 163 full_description
Chris@1115 164 end
Chris@1115 165 end
Chris@1115 166
Chris@1115 167 class MantisBugNote < ActiveRecord::Base
Chris@1115 168 self.table_name = :mantis_bugnote_table
Chris@1115 169 belongs_to :bug, :class_name => "MantisBug", :foreign_key => :bug_id
Chris@1115 170 belongs_to :bug_note_text, :class_name => "MantisBugNoteText", :foreign_key => :bugnote_text_id
Chris@1115 171 end
Chris@1115 172
Chris@1115 173 class MantisBugNoteText < ActiveRecord::Base
Chris@1115 174 self.table_name = :mantis_bugnote_text_table
Chris@1115 175 end
Chris@1115 176
Chris@1115 177 class MantisBugFile < ActiveRecord::Base
Chris@1115 178 self.table_name = :mantis_bug_file_table
Chris@1115 179
Chris@1115 180 def size
Chris@1115 181 filesize
Chris@1115 182 end
Chris@1115 183
Chris@1115 184 def original_filename
Chris@1115 185 MantisMigrate.encode(filename)
Chris@1115 186 end
Chris@1115 187
Chris@1115 188 def content_type
Chris@1115 189 file_type
Chris@1115 190 end
Chris@1115 191
Chris@1115 192 def read(*args)
Chris@1115 193 if @read_finished
Chris@1115 194 nil
Chris@1115 195 else
Chris@1115 196 @read_finished = true
Chris@1115 197 content
Chris@1115 198 end
Chris@1115 199 end
Chris@1115 200 end
Chris@1115 201
Chris@1115 202 class MantisBugRelationship < ActiveRecord::Base
Chris@1115 203 self.table_name = :mantis_bug_relationship_table
Chris@1115 204 end
Chris@1115 205
Chris@1115 206 class MantisBugMonitor < ActiveRecord::Base
Chris@1115 207 self.table_name = :mantis_bug_monitor_table
Chris@1115 208 end
Chris@1115 209
Chris@1115 210 class MantisNews < ActiveRecord::Base
Chris@1115 211 self.table_name = :mantis_news_table
Chris@1115 212 end
Chris@1115 213
Chris@1115 214 class MantisCustomField < ActiveRecord::Base
Chris@1115 215 self.table_name = :mantis_custom_field_table
Chris@1115 216 set_inheritance_column :none
Chris@1115 217 has_many :values, :class_name => "MantisCustomFieldString", :foreign_key => :field_id
Chris@1115 218 has_many :projects, :class_name => "MantisCustomFieldProject", :foreign_key => :field_id
Chris@1115 219
Chris@1115 220 def format
Chris@1115 221 read_attribute :type
Chris@1115 222 end
Chris@1115 223
Chris@1115 224 def name
Chris@1115 225 read_attribute(:name)[0..29]
Chris@1115 226 end
Chris@1115 227 end
Chris@1115 228
Chris@1115 229 class MantisCustomFieldProject < ActiveRecord::Base
Chris@1115 230 self.table_name = :mantis_custom_field_project_table
Chris@1115 231 end
Chris@1115 232
Chris@1115 233 class MantisCustomFieldString < ActiveRecord::Base
Chris@1115 234 self.table_name = :mantis_custom_field_string_table
Chris@1115 235 end
Chris@1115 236
Chris@1115 237 def self.migrate
Chris@1115 238
Chris@1115 239 # Users
Chris@1115 240 print "Migrating users"
Chris@1115 241 User.delete_all "login <> 'admin'"
Chris@1115 242 users_map = {}
Chris@1115 243 users_migrated = 0
Chris@1464 244 MantisUser.all.each do |user|
Chris@1115 245 u = User.new :firstname => encode(user.firstname),
Chris@1115 246 :lastname => encode(user.lastname),
Chris@1115 247 :mail => user.email,
Chris@1115 248 :last_login_on => user.last_visit
Chris@1115 249 u.login = user.username
Chris@1115 250 u.password = 'mantis'
Chris@1115 251 u.status = User::STATUS_LOCKED if user.enabled != 1
Chris@1115 252 u.admin = true if user.access_level == 90
Chris@1115 253 next unless u.save!
Chris@1115 254 users_migrated += 1
Chris@1115 255 users_map[user.id] = u.id
Chris@1115 256 print '.'
Chris@1115 257 end
Chris@1115 258 puts
Chris@1115 259
Chris@1115 260 # Projects
Chris@1115 261 print "Migrating projects"
Chris@1115 262 Project.destroy_all
Chris@1115 263 projects_map = {}
Chris@1115 264 versions_map = {}
Chris@1115 265 categories_map = {}
Chris@1464 266 MantisProject.all.each do |project|
Chris@1115 267 p = Project.new :name => encode(project.name),
Chris@1115 268 :description => encode(project.description)
Chris@1115 269 p.identifier = project.identifier
Chris@1115 270 next unless p.save
Chris@1115 271 projects_map[project.id] = p.id
Chris@1115 272 p.enabled_module_names = ['issue_tracking', 'news', 'wiki']
Chris@1115 273 p.trackers << TRACKER_BUG unless p.trackers.include?(TRACKER_BUG)
Chris@1115 274 p.trackers << TRACKER_FEATURE unless p.trackers.include?(TRACKER_FEATURE)
Chris@1115 275 print '.'
Chris@1115 276
Chris@1115 277 # Project members
Chris@1115 278 project.members.each do |member|
Chris@1115 279 m = Member.new :user => User.find_by_id(users_map[member.user_id]),
Chris@1115 280 :roles => [ROLE_MAPPING[member.access_level] || DEFAULT_ROLE]
Chris@1115 281 m.project = p
Chris@1115 282 m.save
Chris@1115 283 end
Chris@1115 284
Chris@1115 285 # Project versions
Chris@1115 286 project.versions.each do |version|
Chris@1115 287 v = Version.new :name => encode(version.version),
Chris@1115 288 :description => encode(version.description),
Chris@1115 289 :effective_date => (version.date_order ? version.date_order.to_date : nil)
Chris@1115 290 v.project = p
Chris@1115 291 v.save
Chris@1115 292 versions_map[version.id] = v.id
Chris@1115 293 end
Chris@1115 294
Chris@1115 295 # Project categories
Chris@1115 296 project.categories.each do |category|
Chris@1115 297 g = IssueCategory.new :name => category.category[0,30]
Chris@1115 298 g.project = p
Chris@1115 299 g.save
Chris@1115 300 categories_map[category.category] = g.id
Chris@1115 301 end
Chris@1115 302 end
Chris@1115 303 puts
Chris@1115 304
Chris@1115 305 # Bugs
Chris@1115 306 print "Migrating bugs"
Chris@1115 307 Issue.destroy_all
Chris@1115 308 issues_map = {}
Chris@1115 309 keep_bug_ids = (Issue.count == 0)
Chris@1115 310 MantisBug.find_each(:batch_size => 200) do |bug|
Chris@1115 311 next unless projects_map[bug.project_id] && users_map[bug.reporter_id]
Chris@1115 312 i = Issue.new :project_id => projects_map[bug.project_id],
Chris@1115 313 :subject => encode(bug.summary),
Chris@1115 314 :description => encode(bug.bug_text.full_description),
Chris@1115 315 :priority => PRIORITY_MAPPING[bug.priority] || DEFAULT_PRIORITY,
Chris@1115 316 :created_on => bug.date_submitted,
Chris@1115 317 :updated_on => bug.last_updated
Chris@1115 318 i.author = User.find_by_id(users_map[bug.reporter_id])
Chris@1115 319 i.category = IssueCategory.find_by_project_id_and_name(i.project_id, bug.category[0,30]) unless bug.category.blank?
Chris@1115 320 i.fixed_version = Version.find_by_project_id_and_name(i.project_id, bug.fixed_in_version) unless bug.fixed_in_version.blank?
Chris@1115 321 i.status = STATUS_MAPPING[bug.status] || DEFAULT_STATUS
Chris@1115 322 i.tracker = (bug.severity == 10 ? TRACKER_FEATURE : TRACKER_BUG)
Chris@1115 323 i.id = bug.id if keep_bug_ids
Chris@1115 324 next unless i.save
Chris@1115 325 issues_map[bug.id] = i.id
Chris@1115 326 print '.'
Chris@1115 327 STDOUT.flush
Chris@1115 328
Chris@1115 329 # Assignee
Chris@1115 330 # Redmine checks that the assignee is a project member
Chris@1115 331 if (bug.handler_id && users_map[bug.handler_id])
Chris@1115 332 i.assigned_to = User.find_by_id(users_map[bug.handler_id])
Chris@1115 333 i.save(:validate => false)
Chris@1115 334 end
Chris@1115 335
Chris@1115 336 # Bug notes
Chris@1115 337 bug.bug_notes.each do |note|
Chris@1115 338 next unless users_map[note.reporter_id]
Chris@1115 339 n = Journal.new :notes => encode(note.bug_note_text.note),
Chris@1115 340 :created_on => note.date_submitted
Chris@1115 341 n.user = User.find_by_id(users_map[note.reporter_id])
Chris@1115 342 n.journalized = i
Chris@1115 343 n.save
Chris@1115 344 end
Chris@1115 345
Chris@1115 346 # Bug files
Chris@1115 347 bug.bug_files.each do |file|
Chris@1115 348 a = Attachment.new :created_on => file.date_added
Chris@1115 349 a.file = file
Chris@1464 350 a.author = User.first
Chris@1115 351 a.container = i
Chris@1115 352 a.save
Chris@1115 353 end
Chris@1115 354
Chris@1115 355 # Bug monitors
Chris@1115 356 bug.bug_monitors.each do |monitor|
Chris@1115 357 next unless users_map[monitor.user_id]
Chris@1115 358 i.add_watcher(User.find_by_id(users_map[monitor.user_id]))
Chris@1115 359 end
Chris@1115 360 end
Chris@1115 361
Chris@1115 362 # update issue id sequence if needed (postgresql)
Chris@1115 363 Issue.connection.reset_pk_sequence!(Issue.table_name) if Issue.connection.respond_to?('reset_pk_sequence!')
Chris@1115 364 puts
Chris@1115 365
Chris@1115 366 # Bug relationships
Chris@1115 367 print "Migrating bug relations"
Chris@1464 368 MantisBugRelationship.all.each do |relation|
Chris@1115 369 next unless issues_map[relation.source_bug_id] && issues_map[relation.destination_bug_id]
Chris@1115 370 r = IssueRelation.new :relation_type => RELATION_TYPE_MAPPING[relation.relationship_type]
Chris@1115 371 r.issue_from = Issue.find_by_id(issues_map[relation.source_bug_id])
Chris@1115 372 r.issue_to = Issue.find_by_id(issues_map[relation.destination_bug_id])
Chris@1115 373 pp r unless r.save
Chris@1115 374 print '.'
Chris@1115 375 STDOUT.flush
Chris@1115 376 end
Chris@1115 377 puts
Chris@1115 378
Chris@1115 379 # News
Chris@1115 380 print "Migrating news"
Chris@1115 381 News.destroy_all
Chris@1464 382 MantisNews.where('project_id > 0').all.each do |news|
Chris@1115 383 next unless projects_map[news.project_id]
Chris@1115 384 n = News.new :project_id => projects_map[news.project_id],
Chris@1115 385 :title => encode(news.headline[0..59]),
Chris@1115 386 :description => encode(news.body),
Chris@1115 387 :created_on => news.date_posted
Chris@1115 388 n.author = User.find_by_id(users_map[news.poster_id])
Chris@1115 389 n.save
Chris@1115 390 print '.'
Chris@1115 391 STDOUT.flush
Chris@1115 392 end
Chris@1115 393 puts
Chris@1115 394
Chris@1115 395 # Custom fields
Chris@1115 396 print "Migrating custom fields"
Chris@1115 397 IssueCustomField.destroy_all
Chris@1464 398 MantisCustomField.all.each do |field|
Chris@1115 399 f = IssueCustomField.new :name => field.name[0..29],
Chris@1115 400 :field_format => CUSTOM_FIELD_TYPE_MAPPING[field.format],
Chris@1115 401 :min_length => field.length_min,
Chris@1115 402 :max_length => field.length_max,
Chris@1115 403 :regexp => field.valid_regexp,
Chris@1115 404 :possible_values => field.possible_values.split('|'),
Chris@1115 405 :is_required => field.require_report?
Chris@1115 406 next unless f.save
Chris@1115 407 print '.'
Chris@1115 408 STDOUT.flush
Chris@1115 409 # Trackers association
Chris@1464 410 f.trackers = Tracker.all
Chris@1115 411
Chris@1115 412 # Projects association
Chris@1115 413 field.projects.each do |project|
Chris@1115 414 f.projects << Project.find_by_id(projects_map[project.project_id]) if projects_map[project.project_id]
Chris@1115 415 end
Chris@1115 416
Chris@1115 417 # Values
Chris@1115 418 field.values.each do |value|
Chris@1115 419 v = CustomValue.new :custom_field_id => f.id,
Chris@1115 420 :value => value.value
Chris@1115 421 v.customized = Issue.find_by_id(issues_map[value.bug_id]) if issues_map[value.bug_id]
Chris@1115 422 v.save
Chris@1115 423 end unless f.new_record?
Chris@1115 424 end
Chris@1115 425 puts
Chris@1115 426
Chris@1115 427 puts
Chris@1115 428 puts "Users: #{users_migrated}/#{MantisUser.count}"
Chris@1115 429 puts "Projects: #{Project.count}/#{MantisProject.count}"
Chris@1115 430 puts "Memberships: #{Member.count}/#{MantisProjectUser.count}"
Chris@1115 431 puts "Versions: #{Version.count}/#{MantisVersion.count}"
Chris@1115 432 puts "Categories: #{IssueCategory.count}/#{MantisCategory.count}"
Chris@1115 433 puts "Bugs: #{Issue.count}/#{MantisBug.count}"
Chris@1115 434 puts "Bug notes: #{Journal.count}/#{MantisBugNote.count}"
Chris@1115 435 puts "Bug files: #{Attachment.count}/#{MantisBugFile.count}"
Chris@1115 436 puts "Bug relations: #{IssueRelation.count}/#{MantisBugRelationship.count}"
Chris@1115 437 puts "Bug monitors: #{Watcher.count}/#{MantisBugMonitor.count}"
Chris@1115 438 puts "News: #{News.count}/#{MantisNews.count}"
Chris@1115 439 puts "Custom fields: #{IssueCustomField.count}/#{MantisCustomField.count}"
Chris@1115 440 end
Chris@1115 441
Chris@1115 442 def self.encoding(charset)
Chris@1464 443 @charset = charset
Chris@1115 444 end
Chris@1115 445
Chris@1115 446 def self.establish_connection(params)
Chris@1115 447 constants.each do |const|
Chris@1115 448 klass = const_get(const)
Chris@1115 449 next unless klass.respond_to? 'establish_connection'
Chris@1115 450 klass.establish_connection params
Chris@1115 451 end
Chris@1115 452 end
Chris@1115 453
Chris@1115 454 def self.encode(text)
Chris@1464 455 if RUBY_VERSION < '1.9'
Chris@1464 456 @ic ||= Iconv.new('UTF-8', @charset)
Chris@1464 457 @ic.iconv text
Chris@1464 458 else
Chris@1464 459 text.to_s.force_encoding(@charset).encode('UTF-8')
Chris@1464 460 end
Chris@1115 461 end
Chris@1115 462 end
Chris@1115 463
Chris@1115 464 puts
Chris@1115 465 if Redmine::DefaultData::Loader.no_data?
Chris@1115 466 puts "Redmine configuration need to be loaded before importing data."
Chris@1115 467 puts "Please, run this first:"
Chris@1115 468 puts
Chris@1115 469 puts " rake redmine:load_default_data RAILS_ENV=\"#{ENV['RAILS_ENV']}\""
Chris@1115 470 exit
Chris@1115 471 end
Chris@1115 472
Chris@1115 473 puts "WARNING: Your Redmine data will be deleted during this process."
Chris@1115 474 print "Are you sure you want to continue ? [y/N] "
Chris@1115 475 STDOUT.flush
Chris@1115 476 break unless STDIN.gets.match(/^y$/i)
Chris@1115 477
Chris@1115 478 # Default Mantis database settings
Chris@1115 479 db_params = {:adapter => 'mysql2',
Chris@1115 480 :database => 'bugtracker',
Chris@1115 481 :host => 'localhost',
Chris@1115 482 :username => 'root',
Chris@1115 483 :password => '' }
Chris@1115 484
Chris@1115 485 puts
Chris@1115 486 puts "Please enter settings for your Mantis database"
Chris@1115 487 [:adapter, :host, :database, :username, :password].each do |param|
Chris@1115 488 print "#{param} [#{db_params[param]}]: "
Chris@1115 489 value = STDIN.gets.chomp!
Chris@1115 490 db_params[param] = value unless value.blank?
Chris@1115 491 end
Chris@1115 492
Chris@1115 493 while true
Chris@1115 494 print "encoding [UTF-8]: "
Chris@1115 495 STDOUT.flush
Chris@1115 496 encoding = STDIN.gets.chomp!
Chris@1115 497 encoding = 'UTF-8' if encoding.blank?
Chris@1115 498 break if MantisMigrate.encoding encoding
Chris@1115 499 puts "Invalid encoding!"
Chris@1115 500 end
Chris@1115 501 puts
Chris@1115 502
Chris@1115 503 # Make sure bugs can refer bugs in other projects
Chris@1115 504 Setting.cross_project_issue_relations = 1 if Setting.respond_to? 'cross_project_issue_relations'
Chris@1115 505
Chris@1464 506 old_notified_events = Setting.notified_events
Chris@1464 507 old_password_min_length = Setting.password_min_length
Chris@1464 508 begin
Chris@1464 509 # Turn off email notifications temporarily
Chris@1464 510 Setting.notified_events = []
Chris@1464 511 Setting.password_min_length = 4
Chris@1464 512 # Run the migration
Chris@1464 513 MantisMigrate.establish_connection db_params
Chris@1464 514 MantisMigrate.migrate
Chris@1464 515 ensure
Chris@1464 516 # Restore previous settings
Chris@1464 517 Setting.notified_events = old_notified_events
Chris@1464 518 Setting.password_min_length = old_password_min_length
Chris@1464 519 end
Chris@1115 520
Chris@1115 521 end
Chris@1115 522 end