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