##// END OF EJS Templates
Fixes r2707....
Jean-Philippe Lang -
r2612:0a0d14d5a797
parent child
Show More
@@ -1,505 +1,510
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 = Enumeration.priorities
43 priorities = Enumeration.priorities
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
123 def name
124 read_attribute(:name)[0..29]
124 read_attribute(:name)[0..29]
125 end
125 end
126
126
127 def identifier
127 def identifier
128 read_attribute(:name).underscore[0..19].gsub(/[^a-z0-9\-]/, '-')
128 read_attribute(:name).underscore[0..19].gsub(/[^a-z0-9\-]/, '-')
129 end
129 end
130 end
130 end
131
131
132 class MantisVersion < ActiveRecord::Base
132 class MantisVersion < ActiveRecord::Base
133 set_table_name :mantis_project_version_table
133 set_table_name :mantis_project_version_table
134
134
135 def version
135 def version
136 read_attribute(:version)[0..29]
136 read_attribute(:version)[0..29]
137 end
137 end
138
138
139 def description
139 def description
140 read_attribute(:description)[0..254]
140 read_attribute(:description)[0..254]
141 end
141 end
142 end
142 end
143
143
144 class MantisCategory < ActiveRecord::Base
144 class MantisCategory < ActiveRecord::Base
145 set_table_name :mantis_project_category_table
145 set_table_name :mantis_project_category_table
146 end
146 end
147
147
148 class MantisProjectUser < ActiveRecord::Base
148 class MantisProjectUser < ActiveRecord::Base
149 set_table_name :mantis_project_user_list_table
149 set_table_name :mantis_project_user_list_table
150 end
150 end
151
151
152 class MantisBug < ActiveRecord::Base
152 class MantisBug < ActiveRecord::Base
153 set_table_name :mantis_bug_table
153 set_table_name :mantis_bug_table
154 belongs_to :bug_text, :class_name => "MantisBugText", :foreign_key => :bug_text_id
154 belongs_to :bug_text, :class_name => "MantisBugText", :foreign_key => :bug_text_id
155 has_many :bug_notes, :class_name => "MantisBugNote", :foreign_key => :bug_id
155 has_many :bug_notes, :class_name => "MantisBugNote", :foreign_key => :bug_id
156 has_many :bug_files, :class_name => "MantisBugFile", :foreign_key => :bug_id
156 has_many :bug_files, :class_name => "MantisBugFile", :foreign_key => :bug_id
157 has_many :bug_monitors, :class_name => "MantisBugMonitor", :foreign_key => :bug_id
157 has_many :bug_monitors, :class_name => "MantisBugMonitor", :foreign_key => :bug_id
158 end
158 end
159
159
160 class MantisBugText < ActiveRecord::Base
160 class MantisBugText < ActiveRecord::Base
161 set_table_name :mantis_bug_text_table
161 set_table_name :mantis_bug_text_table
162
162
163 # Adds Mantis steps_to_reproduce and additional_information fields
163 # Adds Mantis steps_to_reproduce and additional_information fields
164 # to description if any
164 # to description if any
165 def full_description
165 def full_description
166 full_description = description
166 full_description = description
167 full_description += "\n\n*Steps to reproduce:*\n\n#{steps_to_reproduce}" unless steps_to_reproduce.blank?
167 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?
168 full_description += "\n\n*Additional information:*\n\n#{additional_information}" unless additional_information.blank?
169 full_description
169 full_description
170 end
170 end
171 end
171 end
172
172
173 class MantisBugNote < ActiveRecord::Base
173 class MantisBugNote < ActiveRecord::Base
174 set_table_name :mantis_bugnote_table
174 set_table_name :mantis_bugnote_table
175 belongs_to :bug, :class_name => "MantisBug", :foreign_key => :bug_id
175 belongs_to :bug, :class_name => "MantisBug", :foreign_key => :bug_id
176 belongs_to :bug_note_text, :class_name => "MantisBugNoteText", :foreign_key => :bugnote_text_id
176 belongs_to :bug_note_text, :class_name => "MantisBugNoteText", :foreign_key => :bugnote_text_id
177 end
177 end
178
178
179 class MantisBugNoteText < ActiveRecord::Base
179 class MantisBugNoteText < ActiveRecord::Base
180 set_table_name :mantis_bugnote_text_table
180 set_table_name :mantis_bugnote_text_table
181 end
181 end
182
182
183 class MantisBugFile < ActiveRecord::Base
183 class MantisBugFile < ActiveRecord::Base
184 set_table_name :mantis_bug_file_table
184 set_table_name :mantis_bug_file_table
185
185
186 def size
186 def size
187 filesize
187 filesize
188 end
188 end
189
189
190 def original_filename
190 def original_filename
191 MantisMigrate.encode(filename)
191 MantisMigrate.encode(filename)
192 end
192 end
193
193
194 def content_type
194 def content_type
195 file_type
195 file_type
196 end
196 end
197
197
198 def read(*args)
198 def read(*args)
199 content
199 if @read_finished
200 nil
201 else
202 @read_finished = true
203 content
204 end
200 end
205 end
201 end
206 end
202
207
203 class MantisBugRelationship < ActiveRecord::Base
208 class MantisBugRelationship < ActiveRecord::Base
204 set_table_name :mantis_bug_relationship_table
209 set_table_name :mantis_bug_relationship_table
205 end
210 end
206
211
207 class MantisBugMonitor < ActiveRecord::Base
212 class MantisBugMonitor < ActiveRecord::Base
208 set_table_name :mantis_bug_monitor_table
213 set_table_name :mantis_bug_monitor_table
209 end
214 end
210
215
211 class MantisNews < ActiveRecord::Base
216 class MantisNews < ActiveRecord::Base
212 set_table_name :mantis_news_table
217 set_table_name :mantis_news_table
213 end
218 end
214
219
215 class MantisCustomField < ActiveRecord::Base
220 class MantisCustomField < ActiveRecord::Base
216 set_table_name :mantis_custom_field_table
221 set_table_name :mantis_custom_field_table
217 set_inheritance_column :none
222 set_inheritance_column :none
218 has_many :values, :class_name => "MantisCustomFieldString", :foreign_key => :field_id
223 has_many :values, :class_name => "MantisCustomFieldString", :foreign_key => :field_id
219 has_many :projects, :class_name => "MantisCustomFieldProject", :foreign_key => :field_id
224 has_many :projects, :class_name => "MantisCustomFieldProject", :foreign_key => :field_id
220
225
221 def format
226 def format
222 read_attribute :type
227 read_attribute :type
223 end
228 end
224
229
225 def name
230 def name
226 read_attribute(:name)[0..29].gsub(/[^\w\s\'\-]/, '-')
231 read_attribute(:name)[0..29].gsub(/[^\w\s\'\-]/, '-')
227 end
232 end
228 end
233 end
229
234
230 class MantisCustomFieldProject < ActiveRecord::Base
235 class MantisCustomFieldProject < ActiveRecord::Base
231 set_table_name :mantis_custom_field_project_table
236 set_table_name :mantis_custom_field_project_table
232 end
237 end
233
238
234 class MantisCustomFieldString < ActiveRecord::Base
239 class MantisCustomFieldString < ActiveRecord::Base
235 set_table_name :mantis_custom_field_string_table
240 set_table_name :mantis_custom_field_string_table
236 end
241 end
237
242
238
243
239 def self.migrate
244 def self.migrate
240
245
241 # Users
246 # Users
242 print "Migrating users"
247 print "Migrating users"
243 User.delete_all "login <> 'admin'"
248 User.delete_all "login <> 'admin'"
244 users_map = {}
249 users_map = {}
245 users_migrated = 0
250 users_migrated = 0
246 MantisUser.find(:all).each do |user|
251 MantisUser.find(:all).each do |user|
247 u = User.new :firstname => encode(user.firstname),
252 u = User.new :firstname => encode(user.firstname),
248 :lastname => encode(user.lastname),
253 :lastname => encode(user.lastname),
249 :mail => user.email,
254 :mail => user.email,
250 :last_login_on => user.last_visit
255 :last_login_on => user.last_visit
251 u.login = user.username
256 u.login = user.username
252 u.password = 'mantis'
257 u.password = 'mantis'
253 u.status = User::STATUS_LOCKED if user.enabled != 1
258 u.status = User::STATUS_LOCKED if user.enabled != 1
254 u.admin = true if user.access_level == 90
259 u.admin = true if user.access_level == 90
255 next unless u.save!
260 next unless u.save!
256 users_migrated += 1
261 users_migrated += 1
257 users_map[user.id] = u.id
262 users_map[user.id] = u.id
258 print '.'
263 print '.'
259 end
264 end
260 puts
265 puts
261
266
262 # Projects
267 # Projects
263 print "Migrating projects"
268 print "Migrating projects"
264 Project.destroy_all
269 Project.destroy_all
265 projects_map = {}
270 projects_map = {}
266 versions_map = {}
271 versions_map = {}
267 categories_map = {}
272 categories_map = {}
268 MantisProject.find(:all).each do |project|
273 MantisProject.find(:all).each do |project|
269 p = Project.new :name => encode(project.name),
274 p = Project.new :name => encode(project.name),
270 :description => encode(project.description)
275 :description => encode(project.description)
271 p.identifier = project.identifier
276 p.identifier = project.identifier
272 next unless p.save
277 next unless p.save
273 projects_map[project.id] = p.id
278 projects_map[project.id] = p.id
274 p.enabled_module_names = ['issue_tracking', 'news', 'wiki']
279 p.enabled_module_names = ['issue_tracking', 'news', 'wiki']
275 p.trackers << TRACKER_BUG
280 p.trackers << TRACKER_BUG
276 p.trackers << TRACKER_FEATURE
281 p.trackers << TRACKER_FEATURE
277 print '.'
282 print '.'
278
283
279 # Project members
284 # Project members
280 project.members.each do |member|
285 project.members.each do |member|
281 m = Member.new :user => User.find_by_id(users_map[member.user_id]),
286 m = Member.new :user => User.find_by_id(users_map[member.user_id]),
282 :role => ROLE_MAPPING[member.access_level] || DEFAULT_ROLE
287 :role => ROLE_MAPPING[member.access_level] || DEFAULT_ROLE
283 m.project = p
288 m.project = p
284 m.save
289 m.save
285 end
290 end
286
291
287 # Project versions
292 # Project versions
288 project.versions.each do |version|
293 project.versions.each do |version|
289 v = Version.new :name => encode(version.version),
294 v = Version.new :name => encode(version.version),
290 :description => encode(version.description),
295 :description => encode(version.description),
291 :effective_date => version.date_order.to_date
296 :effective_date => version.date_order.to_date
292 v.project = p
297 v.project = p
293 v.save
298 v.save
294 versions_map[version.id] = v.id
299 versions_map[version.id] = v.id
295 end
300 end
296
301
297 # Project categories
302 # Project categories
298 project.categories.each do |category|
303 project.categories.each do |category|
299 g = IssueCategory.new :name => category.category[0,30]
304 g = IssueCategory.new :name => category.category[0,30]
300 g.project = p
305 g.project = p
301 g.save
306 g.save
302 categories_map[category.category] = g.id
307 categories_map[category.category] = g.id
303 end
308 end
304 end
309 end
305 puts
310 puts
306
311
307 # Bugs
312 # Bugs
308 print "Migrating bugs"
313 print "Migrating bugs"
309 Issue.destroy_all
314 Issue.destroy_all
310 issues_map = {}
315 issues_map = {}
311 keep_bug_ids = (Issue.count == 0)
316 keep_bug_ids = (Issue.count == 0)
312 MantisBug.find(:all, :order => 'id ASC').each do |bug|
317 MantisBug.find(:all, :order => 'id ASC').each do |bug|
313 next unless projects_map[bug.project_id] && users_map[bug.reporter_id]
318 next unless projects_map[bug.project_id] && users_map[bug.reporter_id]
314 i = Issue.new :project_id => projects_map[bug.project_id],
319 i = Issue.new :project_id => projects_map[bug.project_id],
315 :subject => encode(bug.summary),
320 :subject => encode(bug.summary),
316 :description => encode(bug.bug_text.full_description),
321 :description => encode(bug.bug_text.full_description),
317 :priority => PRIORITY_MAPPING[bug.priority] || DEFAULT_PRIORITY,
322 :priority => PRIORITY_MAPPING[bug.priority] || DEFAULT_PRIORITY,
318 :created_on => bug.date_submitted,
323 :created_on => bug.date_submitted,
319 :updated_on => bug.last_updated
324 :updated_on => bug.last_updated
320 i.author = User.find_by_id(users_map[bug.reporter_id])
325 i.author = User.find_by_id(users_map[bug.reporter_id])
321 i.category = IssueCategory.find_by_project_id_and_name(i.project_id, bug.category[0,30]) unless bug.category.blank?
326 i.category = IssueCategory.find_by_project_id_and_name(i.project_id, bug.category[0,30]) unless bug.category.blank?
322 i.fixed_version = Version.find_by_project_id_and_name(i.project_id, bug.fixed_in_version) unless bug.fixed_in_version.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.status = STATUS_MAPPING[bug.status] || DEFAULT_STATUS
328 i.status = STATUS_MAPPING[bug.status] || DEFAULT_STATUS
324 i.tracker = (bug.severity == 10 ? TRACKER_FEATURE : TRACKER_BUG)
329 i.tracker = (bug.severity == 10 ? TRACKER_FEATURE : TRACKER_BUG)
325 i.id = bug.id if keep_bug_ids
330 i.id = bug.id if keep_bug_ids
326 next unless i.save
331 next unless i.save
327 issues_map[bug.id] = i.id
332 issues_map[bug.id] = i.id
328 print '.'
333 print '.'
329
334
330 # Assignee
335 # Assignee
331 # Redmine checks that the assignee is a project member
336 # Redmine checks that the assignee is a project member
332 if (bug.handler_id && users_map[bug.handler_id])
337 if (bug.handler_id && users_map[bug.handler_id])
333 i.assigned_to = User.find_by_id(users_map[bug.handler_id])
338 i.assigned_to = User.find_by_id(users_map[bug.handler_id])
334 i.save_with_validation(false)
339 i.save_with_validation(false)
335 end
340 end
336
341
337 # Bug notes
342 # Bug notes
338 bug.bug_notes.each do |note|
343 bug.bug_notes.each do |note|
339 next unless users_map[note.reporter_id]
344 next unless users_map[note.reporter_id]
340 n = Journal.new :notes => encode(note.bug_note_text.note),
345 n = Journal.new :notes => encode(note.bug_note_text.note),
341 :created_on => note.date_submitted
346 :created_on => note.date_submitted
342 n.user = User.find_by_id(users_map[note.reporter_id])
347 n.user = User.find_by_id(users_map[note.reporter_id])
343 n.journalized = i
348 n.journalized = i
344 n.save
349 n.save
345 end
350 end
346
351
347 # Bug files
352 # Bug files
348 bug.bug_files.each do |file|
353 bug.bug_files.each do |file|
349 a = Attachment.new :created_on => file.date_added
354 a = Attachment.new :created_on => file.date_added
350 a.file = file
355 a.file = file
351 a.author = User.find :first
356 a.author = User.find :first
352 a.container = i
357 a.container = i
353 a.save
358 a.save
354 end
359 end
355
360
356 # Bug monitors
361 # Bug monitors
357 bug.bug_monitors.each do |monitor|
362 bug.bug_monitors.each do |monitor|
358 next unless users_map[monitor.user_id]
363 next unless users_map[monitor.user_id]
359 i.add_watcher(User.find_by_id(users_map[monitor.user_id]))
364 i.add_watcher(User.find_by_id(users_map[monitor.user_id]))
360 end
365 end
361 end
366 end
362
367
363 # update issue id sequence if needed (postgresql)
368 # update issue id sequence if needed (postgresql)
364 Issue.connection.reset_pk_sequence!(Issue.table_name) if Issue.connection.respond_to?('reset_pk_sequence!')
369 Issue.connection.reset_pk_sequence!(Issue.table_name) if Issue.connection.respond_to?('reset_pk_sequence!')
365 puts
370 puts
366
371
367 # Bug relationships
372 # Bug relationships
368 print "Migrating bug relations"
373 print "Migrating bug relations"
369 MantisBugRelationship.find(:all).each do |relation|
374 MantisBugRelationship.find(:all).each do |relation|
370 next unless issues_map[relation.source_bug_id] && issues_map[relation.destination_bug_id]
375 next unless issues_map[relation.source_bug_id] && issues_map[relation.destination_bug_id]
371 r = IssueRelation.new :relation_type => RELATION_TYPE_MAPPING[relation.relationship_type]
376 r = IssueRelation.new :relation_type => RELATION_TYPE_MAPPING[relation.relationship_type]
372 r.issue_from = Issue.find_by_id(issues_map[relation.source_bug_id])
377 r.issue_from = Issue.find_by_id(issues_map[relation.source_bug_id])
373 r.issue_to = Issue.find_by_id(issues_map[relation.destination_bug_id])
378 r.issue_to = Issue.find_by_id(issues_map[relation.destination_bug_id])
374 pp r unless r.save
379 pp r unless r.save
375 print '.'
380 print '.'
376 end
381 end
377 puts
382 puts
378
383
379 # News
384 # News
380 print "Migrating news"
385 print "Migrating news"
381 News.destroy_all
386 News.destroy_all
382 MantisNews.find(:all, :conditions => 'project_id > 0').each do |news|
387 MantisNews.find(:all, :conditions => 'project_id > 0').each do |news|
383 next unless projects_map[news.project_id]
388 next unless projects_map[news.project_id]
384 n = News.new :project_id => projects_map[news.project_id],
389 n = News.new :project_id => projects_map[news.project_id],
385 :title => encode(news.headline[0..59]),
390 :title => encode(news.headline[0..59]),
386 :description => encode(news.body),
391 :description => encode(news.body),
387 :created_on => news.date_posted
392 :created_on => news.date_posted
388 n.author = User.find_by_id(users_map[news.poster_id])
393 n.author = User.find_by_id(users_map[news.poster_id])
389 n.save
394 n.save
390 print '.'
395 print '.'
391 end
396 end
392 puts
397 puts
393
398
394 # Custom fields
399 # Custom fields
395 print "Migrating custom fields"
400 print "Migrating custom fields"
396 IssueCustomField.destroy_all
401 IssueCustomField.destroy_all
397 MantisCustomField.find(:all).each do |field|
402 MantisCustomField.find(:all).each do |field|
398 f = IssueCustomField.new :name => field.name[0..29],
403 f = IssueCustomField.new :name => field.name[0..29],
399 :field_format => CUSTOM_FIELD_TYPE_MAPPING[field.format],
404 :field_format => CUSTOM_FIELD_TYPE_MAPPING[field.format],
400 :min_length => field.length_min,
405 :min_length => field.length_min,
401 :max_length => field.length_max,
406 :max_length => field.length_max,
402 :regexp => field.valid_regexp,
407 :regexp => field.valid_regexp,
403 :possible_values => field.possible_values.split('|'),
408 :possible_values => field.possible_values.split('|'),
404 :is_required => field.require_report?
409 :is_required => field.require_report?
405 next unless f.save
410 next unless f.save
406 print '.'
411 print '.'
407
412
408 # Trackers association
413 # Trackers association
409 f.trackers = Tracker.find :all
414 f.trackers = Tracker.find :all
410
415
411 # Projects association
416 # Projects association
412 field.projects.each do |project|
417 field.projects.each do |project|
413 f.projects << Project.find_by_id(projects_map[project.project_id]) if projects_map[project.project_id]
418 f.projects << Project.find_by_id(projects_map[project.project_id]) if projects_map[project.project_id]
414 end
419 end
415
420
416 # Values
421 # Values
417 field.values.each do |value|
422 field.values.each do |value|
418 v = CustomValue.new :custom_field_id => f.id,
423 v = CustomValue.new :custom_field_id => f.id,
419 :value => value.value
424 :value => value.value
420 v.customized = Issue.find_by_id(issues_map[value.bug_id]) if issues_map[value.bug_id]
425 v.customized = Issue.find_by_id(issues_map[value.bug_id]) if issues_map[value.bug_id]
421 v.save
426 v.save
422 end unless f.new_record?
427 end unless f.new_record?
423 end
428 end
424 puts
429 puts
425
430
426 puts
431 puts
427 puts "Users: #{users_migrated}/#{MantisUser.count}"
432 puts "Users: #{users_migrated}/#{MantisUser.count}"
428 puts "Projects: #{Project.count}/#{MantisProject.count}"
433 puts "Projects: #{Project.count}/#{MantisProject.count}"
429 puts "Memberships: #{Member.count}/#{MantisProjectUser.count}"
434 puts "Memberships: #{Member.count}/#{MantisProjectUser.count}"
430 puts "Versions: #{Version.count}/#{MantisVersion.count}"
435 puts "Versions: #{Version.count}/#{MantisVersion.count}"
431 puts "Categories: #{IssueCategory.count}/#{MantisCategory.count}"
436 puts "Categories: #{IssueCategory.count}/#{MantisCategory.count}"
432 puts "Bugs: #{Issue.count}/#{MantisBug.count}"
437 puts "Bugs: #{Issue.count}/#{MantisBug.count}"
433 puts "Bug notes: #{Journal.count}/#{MantisBugNote.count}"
438 puts "Bug notes: #{Journal.count}/#{MantisBugNote.count}"
434 puts "Bug files: #{Attachment.count}/#{MantisBugFile.count}"
439 puts "Bug files: #{Attachment.count}/#{MantisBugFile.count}"
435 puts "Bug relations: #{IssueRelation.count}/#{MantisBugRelationship.count}"
440 puts "Bug relations: #{IssueRelation.count}/#{MantisBugRelationship.count}"
436 puts "Bug monitors: #{Watcher.count}/#{MantisBugMonitor.count}"
441 puts "Bug monitors: #{Watcher.count}/#{MantisBugMonitor.count}"
437 puts "News: #{News.count}/#{MantisNews.count}"
442 puts "News: #{News.count}/#{MantisNews.count}"
438 puts "Custom fields: #{IssueCustomField.count}/#{MantisCustomField.count}"
443 puts "Custom fields: #{IssueCustomField.count}/#{MantisCustomField.count}"
439 end
444 end
440
445
441 def self.encoding(charset)
446 def self.encoding(charset)
442 @ic = Iconv.new('UTF-8', charset)
447 @ic = Iconv.new('UTF-8', charset)
443 rescue Iconv::InvalidEncoding
448 rescue Iconv::InvalidEncoding
444 return false
449 return false
445 end
450 end
446
451
447 def self.establish_connection(params)
452 def self.establish_connection(params)
448 constants.each do |const|
453 constants.each do |const|
449 klass = const_get(const)
454 klass = const_get(const)
450 next unless klass.respond_to? 'establish_connection'
455 next unless klass.respond_to? 'establish_connection'
451 klass.establish_connection params
456 klass.establish_connection params
452 end
457 end
453 end
458 end
454
459
455 def self.encode(text)
460 def self.encode(text)
456 @ic.iconv text
461 @ic.iconv text
457 rescue
462 rescue
458 text
463 text
459 end
464 end
460 end
465 end
461
466
462 puts
467 puts
463 if Redmine::DefaultData::Loader.no_data?
468 if Redmine::DefaultData::Loader.no_data?
464 puts "Redmine configuration need to be loaded before importing data."
469 puts "Redmine configuration need to be loaded before importing data."
465 puts "Please, run this first:"
470 puts "Please, run this first:"
466 puts
471 puts
467 puts " rake redmine:load_default_data RAILS_ENV=\"#{ENV['RAILS_ENV']}\""
472 puts " rake redmine:load_default_data RAILS_ENV=\"#{ENV['RAILS_ENV']}\""
468 exit
473 exit
469 end
474 end
470
475
471 puts "WARNING: Your Redmine data will be deleted during this process."
476 puts "WARNING: Your Redmine data will be deleted during this process."
472 print "Are you sure you want to continue ? [y/N] "
477 print "Are you sure you want to continue ? [y/N] "
473 break unless STDIN.gets.match(/^y$/i)
478 break unless STDIN.gets.match(/^y$/i)
474
479
475 # Default Mantis database settings
480 # Default Mantis database settings
476 db_params = {:adapter => 'mysql',
481 db_params = {:adapter => 'mysql',
477 :database => 'bugtracker',
482 :database => 'bugtracker',
478 :host => 'localhost',
483 :host => 'localhost',
479 :username => 'root',
484 :username => 'root',
480 :password => '' }
485 :password => '' }
481
486
482 puts
487 puts
483 puts "Please enter settings for your Mantis database"
488 puts "Please enter settings for your Mantis database"
484 [:adapter, :host, :database, :username, :password].each do |param|
489 [:adapter, :host, :database, :username, :password].each do |param|
485 print "#{param} [#{db_params[param]}]: "
490 print "#{param} [#{db_params[param]}]: "
486 value = STDIN.gets.chomp!
491 value = STDIN.gets.chomp!
487 db_params[param] = value unless value.blank?
492 db_params[param] = value unless value.blank?
488 end
493 end
489
494
490 while true
495 while true
491 print "encoding [UTF-8]: "
496 print "encoding [UTF-8]: "
492 encoding = STDIN.gets.chomp!
497 encoding = STDIN.gets.chomp!
493 encoding = 'UTF-8' if encoding.blank?
498 encoding = 'UTF-8' if encoding.blank?
494 break if MantisMigrate.encoding encoding
499 break if MantisMigrate.encoding encoding
495 puts "Invalid encoding!"
500 puts "Invalid encoding!"
496 end
501 end
497 puts
502 puts
498
503
499 # Make sure bugs can refer bugs in other projects
504 # Make sure bugs can refer bugs in other projects
500 Setting.cross_project_issue_relations = 1 if Setting.respond_to? 'cross_project_issue_relations'
505 Setting.cross_project_issue_relations = 1 if Setting.respond_to? 'cross_project_issue_relations'
501
506
502 MantisMigrate.establish_connection db_params
507 MantisMigrate.establish_connection db_params
503 MantisMigrate.migrate
508 MantisMigrate.migrate
504 end
509 end
505 end
510 end
General Comments 0
You need to be logged in to leave comments. Login now