##// END OF EJS Templates
Mantis importer: replaced find by find_by_id...
Jean-Philippe Lang -
r630:5313f48ff48b
parent child
Show More
@@ -1,471 +1,471
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 task :migrate_from_mantis => :environment do
24 task :migrate_from_mantis => :environment do
25
25
26 module MantisMigrate
26 module MantisMigrate
27
27
28 default_status = IssueStatus.default
28 default_status = IssueStatus.default
29 assigned_status = IssueStatus.find_by_position(2)
29 assigned_status = IssueStatus.find_by_position(2)
30 resolved_status = IssueStatus.find_by_position(3)
30 resolved_status = IssueStatus.find_by_position(3)
31 feedback_status = IssueStatus.find_by_position(4)
31 feedback_status = IssueStatus.find_by_position(4)
32 closed_status = IssueStatus.find :first, :conditions => { :is_closed => true }
32 closed_status = IssueStatus.find :first, :conditions => { :is_closed => true }
33 STATUS_MAPPING = {10 => default_status, # new
33 STATUS_MAPPING = {10 => default_status, # new
34 20 => feedback_status, # feedback
34 20 => feedback_status, # feedback
35 30 => default_status, # acknowledged
35 30 => default_status, # acknowledged
36 40 => default_status, # confirmed
36 40 => default_status, # confirmed
37 50 => assigned_status, # assigned
37 50 => assigned_status, # assigned
38 80 => resolved_status, # resolved
38 80 => resolved_status, # resolved
39 90 => closed_status # closed
39 90 => closed_status # closed
40 }
40 }
41
41
42 priorities = Enumeration.get_values('IPRI')
42 priorities = Enumeration.get_values('IPRI')
43 PRIORITY_MAPPING = {10 => priorities[1], # none
43 PRIORITY_MAPPING = {10 => priorities[1], # none
44 20 => priorities[1], # low
44 20 => priorities[1], # low
45 30 => priorities[2], # normal
45 30 => priorities[2], # normal
46 40 => priorities[3], # high
46 40 => priorities[3], # high
47 50 => priorities[4], # urgent
47 50 => priorities[4], # urgent
48 60 => priorities[5] # immediate
48 60 => priorities[5] # immediate
49 }
49 }
50
50
51 TARGET_TRACKER = Tracker.find :first
51 TARGET_TRACKER = Tracker.find :first
52
52
53 default_role = Role.find_by_position(3)
53 default_role = Role.find_by_position(3)
54 manager_role = Role.find_by_position(1)
54 manager_role = Role.find_by_position(1)
55 developer_role = Role.find_by_position(2)
55 developer_role = Role.find_by_position(2)
56 ROLE_MAPPING = {10 => default_role, # viewer
56 ROLE_MAPPING = {10 => default_role, # viewer
57 25 => default_role, # reporter
57 25 => default_role, # reporter
58 40 => default_role, # updater
58 40 => default_role, # updater
59 55 => developer_role, # developer
59 55 => developer_role, # developer
60 70 => manager_role, # manager
60 70 => manager_role, # manager
61 90 => manager_role # administrator
61 90 => manager_role # administrator
62 }
62 }
63
63
64 CUSTOM_FIELD_TYPE_MAPPING = {0 => 'string', # String
64 CUSTOM_FIELD_TYPE_MAPPING = {0 => 'string', # String
65 1 => 'int', # Numeric
65 1 => 'int', # Numeric
66 2 => 'int', # Float
66 2 => 'int', # Float
67 3 => 'list', # Enumeration
67 3 => 'list', # Enumeration
68 4 => 'string', # Email
68 4 => 'string', # Email
69 5 => 'bool', # Checkbox
69 5 => 'bool', # Checkbox
70 6 => 'list', # List
70 6 => 'list', # List
71 7 => 'list', # Multiselection list
71 7 => 'list', # Multiselection list
72 8 => 'date', # Date
72 8 => 'date', # Date
73 }
73 }
74
74
75 RELATION_TYPE_MAPPING = {1 => IssueRelation::TYPE_RELATES, # related to
75 RELATION_TYPE_MAPPING = {1 => IssueRelation::TYPE_RELATES, # related to
76 2 => IssueRelation::TYPE_RELATES, # parent of
76 2 => IssueRelation::TYPE_RELATES, # parent of
77 3 => IssueRelation::TYPE_RELATES, # child of
77 3 => IssueRelation::TYPE_RELATES, # child of
78 0 => IssueRelation::TYPE_DUPLICATES, # duplicate of
78 0 => IssueRelation::TYPE_DUPLICATES, # duplicate of
79 4 => IssueRelation::TYPE_DUPLICATES # has duplicate
79 4 => IssueRelation::TYPE_DUPLICATES # has duplicate
80 }
80 }
81
81
82 class MantisUser < ActiveRecord::Base
82 class MantisUser < ActiveRecord::Base
83 set_table_name :mantis_user_table
83 set_table_name :mantis_user_table
84
84
85 def firstname
85 def firstname
86 realname.blank? ? username : realname.split.first
86 realname.blank? ? username : realname.split.first[0..29]
87 end
87 end
88
88
89 def lastname
89 def lastname
90 realname.blank? ? username : realname.split[1..-1].join(' ')
90 realname.blank? ? username : realname.split[1..-1].join(' ')[0..29]
91 end
91 end
92
92
93 def email
93 def email
94 if read_attribute(:email).match(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i)
94 if read_attribute(:email).match(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i)
95 read_attribute(:email)
95 read_attribute(:email)
96 else
96 else
97 "#{username}@foo.bar"
97 "#{username}@foo.bar"
98 end
98 end
99 end
99 end
100 end
100 end
101
101
102 class MantisProject < ActiveRecord::Base
102 class MantisProject < ActiveRecord::Base
103 set_table_name :mantis_project_table
103 set_table_name :mantis_project_table
104 has_many :versions, :class_name => "MantisVersion", :foreign_key => :project_id
104 has_many :versions, :class_name => "MantisVersion", :foreign_key => :project_id
105 has_many :categories, :class_name => "MantisCategory", :foreign_key => :project_id
105 has_many :categories, :class_name => "MantisCategory", :foreign_key => :project_id
106 has_many :news, :class_name => "MantisNews", :foreign_key => :project_id
106 has_many :news, :class_name => "MantisNews", :foreign_key => :project_id
107 has_many :members, :class_name => "MantisProjectUser", :foreign_key => :project_id
107 has_many :members, :class_name => "MantisProjectUser", :foreign_key => :project_id
108
108
109 def name
109 def name
110 read_attribute(:name)[0..29].gsub(/[^\w\s\'\-]/, '-')
110 read_attribute(:name)[0..29].gsub(/[^\w\s\'\-]/, '-')
111 end
111 end
112
112
113 def description
113 def description
114 read_attribute(:description).blank? ? read_attribute(:name) : read_attribute(:description)[0..254]
114 read_attribute(:description).blank? ? read_attribute(:name) : read_attribute(:description)[0..254]
115 end
115 end
116
116
117 def identifier
117 def identifier
118 read_attribute(:name).underscore[0..11].gsub(/[^a-z0-9\-]/, '-')
118 read_attribute(:name).underscore[0..11].gsub(/[^a-z0-9\-]/, '-')
119 end
119 end
120 end
120 end
121
121
122 class MantisVersion < ActiveRecord::Base
122 class MantisVersion < ActiveRecord::Base
123 set_table_name :mantis_project_version_table
123 set_table_name :mantis_project_version_table
124
124
125 def version
125 def version
126 read_attribute(:version)[0..29]
126 read_attribute(:version)[0..29]
127 end
127 end
128
128
129 def description
129 def description
130 read_attribute(:description)[0..254]
130 read_attribute(:description)[0..254]
131 end
131 end
132 end
132 end
133
133
134 class MantisCategory < ActiveRecord::Base
134 class MantisCategory < ActiveRecord::Base
135 set_table_name :mantis_project_category_table
135 set_table_name :mantis_project_category_table
136 end
136 end
137
137
138 class MantisProjectUser < ActiveRecord::Base
138 class MantisProjectUser < ActiveRecord::Base
139 set_table_name :mantis_project_user_list_table
139 set_table_name :mantis_project_user_list_table
140 end
140 end
141
141
142 class MantisBug < ActiveRecord::Base
142 class MantisBug < ActiveRecord::Base
143 set_table_name :mantis_bug_table
143 set_table_name :mantis_bug_table
144 belongs_to :bug_text, :class_name => "MantisBugText", :foreign_key => :bug_text_id
144 belongs_to :bug_text, :class_name => "MantisBugText", :foreign_key => :bug_text_id
145 has_many :bug_notes, :class_name => "MantisBugNote", :foreign_key => :bug_id
145 has_many :bug_notes, :class_name => "MantisBugNote", :foreign_key => :bug_id
146 has_many :bug_files, :class_name => "MantisBugFile", :foreign_key => :bug_id
146 has_many :bug_files, :class_name => "MantisBugFile", :foreign_key => :bug_id
147 has_many :bug_monitors, :class_name => "MantisBugMonitor", :foreign_key => :bug_id
147 has_many :bug_monitors, :class_name => "MantisBugMonitor", :foreign_key => :bug_id
148 end
148 end
149
149
150 class MantisBugText < ActiveRecord::Base
150 class MantisBugText < ActiveRecord::Base
151 set_table_name :mantis_bug_text_table
151 set_table_name :mantis_bug_text_table
152
152
153 # Adds Mantis steps_to_reproduce and additional_information fields
153 # Adds Mantis steps_to_reproduce and additional_information fields
154 # to description if any
154 # to description if any
155 def full_description
155 def full_description
156 full_description = description
156 full_description = description
157 full_description += "\n\n*Steps to reproduce:*\n\n#{steps_to_reproduce}" unless steps_to_reproduce.blank?
157 full_description += "\n\n*Steps to reproduce:*\n\n#{steps_to_reproduce}" unless steps_to_reproduce.blank?
158 full_description += "\n\n*Additional information:*\n\n#{additional_information}" unless additional_information.blank?
158 full_description += "\n\n*Additional information:*\n\n#{additional_information}" unless additional_information.blank?
159 full_description
159 full_description
160 end
160 end
161 end
161 end
162
162
163 class MantisBugNote < ActiveRecord::Base
163 class MantisBugNote < ActiveRecord::Base
164 set_table_name :mantis_bugnote_table
164 set_table_name :mantis_bugnote_table
165 belongs_to :bug, :class_name => "MantisBug", :foreign_key => :bug_id
165 belongs_to :bug, :class_name => "MantisBug", :foreign_key => :bug_id
166 belongs_to :bug_note_text, :class_name => "MantisBugNoteText", :foreign_key => :bugnote_text_id
166 belongs_to :bug_note_text, :class_name => "MantisBugNoteText", :foreign_key => :bugnote_text_id
167 end
167 end
168
168
169 class MantisBugNoteText < ActiveRecord::Base
169 class MantisBugNoteText < ActiveRecord::Base
170 set_table_name :mantis_bugnote_text_table
170 set_table_name :mantis_bugnote_text_table
171 end
171 end
172
172
173 class MantisBugFile < ActiveRecord::Base
173 class MantisBugFile < ActiveRecord::Base
174 set_table_name :mantis_bug_file_table
174 set_table_name :mantis_bug_file_table
175
175
176 def size
176 def size
177 filesize
177 filesize
178 end
178 end
179
179
180 def original_filename
180 def original_filename
181 filename
181 filename
182 end
182 end
183
183
184 def content_type
184 def content_type
185 file_type
185 file_type
186 end
186 end
187
187
188 def read
188 def read
189 content
189 content
190 end
190 end
191 end
191 end
192
192
193 class MantisBugRelationship < ActiveRecord::Base
193 class MantisBugRelationship < ActiveRecord::Base
194 set_table_name :mantis_bug_relationship_table
194 set_table_name :mantis_bug_relationship_table
195 end
195 end
196
196
197 class MantisBugMonitor < ActiveRecord::Base
197 class MantisBugMonitor < ActiveRecord::Base
198 set_table_name :mantis_bug_monitor_table
198 set_table_name :mantis_bug_monitor_table
199 end
199 end
200
200
201 class MantisNews < ActiveRecord::Base
201 class MantisNews < ActiveRecord::Base
202 set_table_name :mantis_news_table
202 set_table_name :mantis_news_table
203 end
203 end
204
204
205 class MantisCustomField < ActiveRecord::Base
205 class MantisCustomField < ActiveRecord::Base
206 set_table_name :mantis_custom_field_table
206 set_table_name :mantis_custom_field_table
207 set_inheritance_column :none
207 set_inheritance_column :none
208 has_many :values, :class_name => "MantisCustomFieldString", :foreign_key => :field_id
208 has_many :values, :class_name => "MantisCustomFieldString", :foreign_key => :field_id
209 has_many :projects, :class_name => "MantisCustomFieldProject", :foreign_key => :field_id
209 has_many :projects, :class_name => "MantisCustomFieldProject", :foreign_key => :field_id
210
210
211 def format
211 def format
212 read_attribute :type
212 read_attribute :type
213 end
213 end
214
214
215 def name
215 def name
216 read_attribute(:name)[0..29].gsub(/[^\w\s\'\-]/, '-')
216 read_attribute(:name)[0..29].gsub(/[^\w\s\'\-]/, '-')
217 end
217 end
218 end
218 end
219
219
220 class MantisCustomFieldProject < ActiveRecord::Base
220 class MantisCustomFieldProject < ActiveRecord::Base
221 set_table_name :mantis_custom_field_project_table
221 set_table_name :mantis_custom_field_project_table
222 end
222 end
223
223
224 class MantisCustomFieldString < ActiveRecord::Base
224 class MantisCustomFieldString < ActiveRecord::Base
225 set_table_name :mantis_custom_field_string_table
225 set_table_name :mantis_custom_field_string_table
226 end
226 end
227
227
228
228
229 def self.migrate
229 def self.migrate
230
230
231 # Users
231 # Users
232 print "Migrating users"
232 print "Migrating users"
233 User.delete_all "login <> 'admin'"
233 User.delete_all "login <> 'admin'"
234 users_map = {}
234 users_map = {}
235 users_migrated = 0
235 users_migrated = 0
236 MantisUser.find(:all).each do |user|
236 MantisUser.find(:all).each do |user|
237 u = User.new :firstname => encode(user.firstname),
237 u = User.new :firstname => encode(user.firstname),
238 :lastname => encode(user.lastname),
238 :lastname => encode(user.lastname),
239 :mail => user.email,
239 :mail => user.email,
240 :last_login_on => user.last_visit
240 :last_login_on => user.last_visit
241 u.login = user.username
241 u.login = user.username
242 u.password = 'mantis'
242 u.password = 'mantis'
243 u.status = User::STATUS_LOCKED if user.enabled != 1
243 u.status = User::STATUS_LOCKED if user.enabled != 1
244 u.admin = true if user.access_level == 90
244 u.admin = true if user.access_level == 90
245 next unless u.save
245 next unless u.save
246 users_migrated += 1
246 users_migrated += 1
247 users_map[user.id] = u.id
247 users_map[user.id] = u.id
248 print '.'
248 print '.'
249 end
249 end
250 puts
250 puts
251
251
252 # Projects
252 # Projects
253 print "Migrating projects"
253 print "Migrating projects"
254 Project.destroy_all
254 Project.destroy_all
255 projects_map = {}
255 projects_map = {}
256 versions_map = {}
256 versions_map = {}
257 categories_map = {}
257 categories_map = {}
258 MantisProject.find(:all).each do |project|
258 MantisProject.find(:all).each do |project|
259 p = Project.new :name => encode(project.name),
259 p = Project.new :name => encode(project.name),
260 :description => encode(project.description)
260 :description => encode(project.description)
261 p.identifier = project.identifier
261 p.identifier = project.identifier
262 next unless p.save
262 next unless p.save
263 projects_map[project.id] = p.id
263 projects_map[project.id] = p.id
264 print '.'
264 print '.'
265
265
266 # Project members
266 # Project members
267 project.members.each do |member|
267 project.members.each do |member|
268 m = Member.new :user => User.find(users_map[member.user_id]),
268 m = Member.new :user => User.find_by_id(users_map[member.user_id]),
269 :role => ROLE_MAPPING[member.access_level] || default_role
269 :role => ROLE_MAPPING[member.access_level] || default_role
270 m.project = p
270 m.project = p
271 m.save
271 m.save
272 end
272 end
273
273
274 # Project versions
274 # Project versions
275 project.versions.each do |version|
275 project.versions.each do |version|
276 v = Version.new :name => encode(version.version),
276 v = Version.new :name => encode(version.version),
277 :description => encode(version.description),
277 :description => encode(version.description),
278 :effective_date => version.date_order.to_date
278 :effective_date => version.date_order.to_date
279 v.project = p
279 v.project = p
280 v.save
280 v.save
281 versions_map[version.id] = v.id
281 versions_map[version.id] = v.id
282 end
282 end
283
283
284 # Project categories
284 # Project categories
285 project.categories.each do |category|
285 project.categories.each do |category|
286 g = IssueCategory.new :name => category.category
286 g = IssueCategory.new :name => category.category
287 g.project = p
287 g.project = p
288 g.save
288 g.save
289 categories_map[category.category] = g.id
289 categories_map[category.category] = g.id
290 end
290 end
291 end
291 end
292 puts
292 puts
293
293
294 # Bugs
294 # Bugs
295 print "Migrating bugs"
295 print "Migrating bugs"
296 Issue.destroy_all
296 Issue.destroy_all
297 issues_map = {}
297 issues_map = {}
298 MantisBug.find(:all).each do |bug|
298 MantisBug.find(:all).each do |bug|
299 next unless projects_map[bug.project_id]
299 next unless projects_map[bug.project_id]
300 i = Issue.new :project_id => projects_map[bug.project_id],
300 i = Issue.new :project_id => projects_map[bug.project_id],
301 :subject => encode(bug.summary),
301 :subject => encode(bug.summary),
302 :description => encode(bug.bug_text.full_description),
302 :description => encode(bug.bug_text.full_description),
303 :priority => PRIORITY_MAPPING[bug.priority],
303 :priority => PRIORITY_MAPPING[bug.priority],
304 :created_on => bug.date_submitted,
304 :created_on => bug.date_submitted,
305 :updated_on => bug.last_updated
305 :updated_on => bug.last_updated
306 i.author = User.find(users_map[bug.reporter_id] || :first)
306 i.author = User.find_by_id(users_map[bug.reporter_id])
307 i.assigned_to = User.find(users_map[bug.handler_id]) if bug.handler_id && users_map[bug.handler_id]
307 i.assigned_to = User.find_by_id(users_map[bug.handler_id]) if bug.handler_id && users_map[bug.handler_id]
308 i.category = IssueCategory.find_by_project_id_and_name(i.project_id, bug.category) unless bug.category.blank?
308 i.category = IssueCategory.find_by_project_id_and_name(i.project_id, bug.category) unless bug.category.blank?
309 i.fixed_version = Version.find_by_project_id_and_name(i.project_id, bug.fixed_in_version) unless bug.fixed_in_version.blank?
309 i.fixed_version = Version.find_by_project_id_and_name(i.project_id, bug.fixed_in_version) unless bug.fixed_in_version.blank?
310 i.status = STATUS_MAPPING[bug.status] || default_status
310 i.status = STATUS_MAPPING[bug.status] || default_status
311 i.tracker = TARGET_TRACKER
311 i.tracker = TARGET_TRACKER
312 next unless i.save
312 next unless i.save
313 issues_map[bug.id] = i.id
313 issues_map[bug.id] = i.id
314 print '.'
314 print '.'
315
315
316 # Bug notes
316 # Bug notes
317 bug.bug_notes.each do |note|
317 bug.bug_notes.each do |note|
318 n = Journal.new :notes => encode(note.bug_note_text.note),
318 n = Journal.new :notes => encode(note.bug_note_text.note),
319 :created_on => note.date_submitted
319 :created_on => note.date_submitted
320 n.user = User.find(users_map[note.reporter_id] || :first)
320 n.user = User.find_by_id(users_map[note.reporter_id])
321 n.journalized = i
321 n.journalized = i
322 n.save
322 n.save
323 end
323 end
324
324
325 # Bug files
325 # Bug files
326 bug.bug_files.each do |file|
326 bug.bug_files.each do |file|
327 a = Attachment.new :created_on => file.date_added
327 a = Attachment.new :created_on => file.date_added
328 a.file = file
328 a.file = file
329 a.author = User.find :first
329 a.author = User.find :first
330 a.container = i
330 a.container = i
331 a.save
331 a.save
332 end
332 end
333
333
334 # Bug monitors
334 # Bug monitors
335 bug.bug_monitors.each do |monitor|
335 bug.bug_monitors.each do |monitor|
336 i.add_watcher(User.find_by_id(users_map[monitor.user_id]))
336 i.add_watcher(User.find_by_id(users_map[monitor.user_id]))
337 end
337 end
338 end
338 end
339 puts
339 puts
340
340
341 # Bug relationships
341 # Bug relationships
342 print "Migrating bug relations"
342 print "Migrating bug relations"
343 MantisBugRelationship.find(:all).each do |relation|
343 MantisBugRelationship.find(:all).each do |relation|
344 next unless issues_map[relation.source_bug_id] && issues_map[relation.destination_bug_id]
344 next unless issues_map[relation.source_bug_id] && issues_map[relation.destination_bug_id]
345 r = IssueRelation.new :relation_type => RELATION_TYPE_MAPPING[relation.relationship_type]
345 r = IssueRelation.new :relation_type => RELATION_TYPE_MAPPING[relation.relationship_type]
346 r.issue_from = Issue.find_by_id(issues_map[relation.source_bug_id])
346 r.issue_from = Issue.find_by_id(issues_map[relation.source_bug_id])
347 r.issue_to = Issue.find_by_id(issues_map[relation.destination_bug_id])
347 r.issue_to = Issue.find_by_id(issues_map[relation.destination_bug_id])
348 pp r unless r.save
348 pp r unless r.save
349 print '.'
349 print '.'
350 end
350 end
351 puts
351 puts
352
352
353 # News
353 # News
354 print "Migrating news"
354 print "Migrating news"
355 News.destroy_all
355 News.destroy_all
356 MantisNews.find(:all, :conditions => 'project_id > 0').each do |news|
356 MantisNews.find(:all, :conditions => 'project_id > 0').each do |news|
357 next unless projects_map[news.project_id]
357 next unless projects_map[news.project_id]
358 n = News.new :project_id => projects_map[news.project_id],
358 n = News.new :project_id => projects_map[news.project_id],
359 :title => encode(news.headline[0..59]),
359 :title => encode(news.headline[0..59]),
360 :description => encode(news.body),
360 :description => encode(news.body),
361 :created_on => news.date_posted
361 :created_on => news.date_posted
362 n.author = User.find(users_map[news.poster_id] || :first)
362 n.author = User.find_by_id(users_map[news.poster_id])
363 n.save
363 n.save
364 print '.'
364 print '.'
365 end
365 end
366 puts
366 puts
367
367
368 # Custom fields
368 # Custom fields
369 print "Migrating custom fields"
369 print "Migrating custom fields"
370 IssueCustomField.destroy_all
370 IssueCustomField.destroy_all
371 MantisCustomField.find(:all).each do |field|
371 MantisCustomField.find(:all).each do |field|
372 f = IssueCustomField.new :name => field.name[0..29],
372 f = IssueCustomField.new :name => field.name[0..29],
373 :field_format => CUSTOM_FIELD_TYPE_MAPPING[field.format],
373 :field_format => CUSTOM_FIELD_TYPE_MAPPING[field.format],
374 :min_length => field.length_min,
374 :min_length => field.length_min,
375 :max_length => field.length_max,
375 :max_length => field.length_max,
376 :regexp => field.valid_regexp,
376 :regexp => field.valid_regexp,
377 :possible_values => field.possible_values.split('|'),
377 :possible_values => field.possible_values.split('|'),
378 :is_required => (field.require_report > 0)
378 :is_required => (field.require_report > 0)
379 next unless f.save
379 next unless f.save
380 print '.'
380 print '.'
381
381
382 # Trackers association
382 # Trackers association
383 f.trackers = Tracker.find :all
383 f.trackers = Tracker.find :all
384
384
385 # Projects association
385 # Projects association
386 field.projects.each do |project|
386 field.projects.each do |project|
387 f.projects << Project.find_by_id(projects_map[project.project_id]) if projects_map[project.project_id]
387 f.projects << Project.find_by_id(projects_map[project.project_id]) if projects_map[project.project_id]
388 end
388 end
389
389
390 # Values
390 # Values
391 field.values.each do |value|
391 field.values.each do |value|
392 v = CustomValue.new :custom_field_id => f.id,
392 v = CustomValue.new :custom_field_id => f.id,
393 :value => value.value
393 :value => value.value
394 v.customized = Issue.find_by_id(issues_map[value.bug_id]) if issues_map[value.bug_id]
394 v.customized = Issue.find_by_id(issues_map[value.bug_id]) if issues_map[value.bug_id]
395 v.save
395 v.save
396 end unless f.new_record?
396 end unless f.new_record?
397 end
397 end
398 puts
398 puts
399
399
400 puts
400 puts
401 puts "Users: #{users_migrated}/#{MantisUser.count}"
401 puts "Users: #{users_migrated}/#{MantisUser.count}"
402 puts "Projects: #{Project.count}/#{MantisProject.count}"
402 puts "Projects: #{Project.count}/#{MantisProject.count}"
403 puts "Memberships: #{Member.count}/#{MantisProjectUser.count}"
403 puts "Memberships: #{Member.count}/#{MantisProjectUser.count}"
404 puts "Versions: #{Version.count}/#{MantisVersion.count}"
404 puts "Versions: #{Version.count}/#{MantisVersion.count}"
405 puts "Categories: #{IssueCategory.count}/#{MantisCategory.count}"
405 puts "Categories: #{IssueCategory.count}/#{MantisCategory.count}"
406 puts "Bugs: #{Issue.count}/#{MantisBug.count}"
406 puts "Bugs: #{Issue.count}/#{MantisBug.count}"
407 puts "Bug notes: #{Journal.count}/#{MantisBugNote.count}"
407 puts "Bug notes: #{Journal.count}/#{MantisBugNote.count}"
408 puts "Bug files: #{Attachment.count}/#{MantisBugFile.count}"
408 puts "Bug files: #{Attachment.count}/#{MantisBugFile.count}"
409 puts "Bug relations: #{IssueRelation.count}/#{MantisBugRelationship.count}"
409 puts "Bug relations: #{IssueRelation.count}/#{MantisBugRelationship.count}"
410 puts "Bug monitors: #{Watcher.count}/#{MantisBugMonitor.count}"
410 puts "Bug monitors: #{Watcher.count}/#{MantisBugMonitor.count}"
411 puts "News: #{News.count}/#{MantisNews.count}"
411 puts "News: #{News.count}/#{MantisNews.count}"
412 puts "Custom fields: #{IssueCustomField.count}/#{MantisCustomField.count}"
412 puts "Custom fields: #{IssueCustomField.count}/#{MantisCustomField.count}"
413 end
413 end
414
414
415 def self.encoding(charset)
415 def self.encoding(charset)
416 @ic = Iconv.new('UTF-8', charset)
416 @ic = Iconv.new('UTF-8', charset)
417 rescue Iconv::InvalidEncoding
417 rescue Iconv::InvalidEncoding
418 return false
418 return false
419 end
419 end
420
420
421 def self.establish_connection(params)
421 def self.establish_connection(params)
422 constants.each do |const|
422 constants.each do |const|
423 klass = const_get(const)
423 klass = const_get(const)
424 next unless klass.respond_to? 'establish_connection'
424 next unless klass.respond_to? 'establish_connection'
425 klass.establish_connection params
425 klass.establish_connection params
426 end
426 end
427 end
427 end
428
428
429 private
429 private
430 def self.encode(text)
430 def self.encode(text)
431 @ic.iconv text
431 @ic.iconv text
432 rescue
432 rescue
433 text
433 text
434 end
434 end
435 end
435 end
436
436
437 puts
437 puts
438 puts "WARNING: Your Redmine data will be deleted during this process."
438 puts "WARNING: Your Redmine data will be deleted during this process."
439 print "Are you sure you want to continue ? [y/N] "
439 print "Are you sure you want to continue ? [y/N] "
440 break unless STDIN.gets.match(/^y$/i)
440 break unless STDIN.gets.match(/^y$/i)
441
441
442 # Default Mantis database settings
442 # Default Mantis database settings
443 db_params = {:adapter => 'mysql',
443 db_params = {:adapter => 'mysql',
444 :database => 'bugtracker',
444 :database => 'bugtracker',
445 :host => 'localhost',
445 :host => 'localhost',
446 :username => 'root',
446 :username => 'root',
447 :password => '' }
447 :password => '' }
448
448
449 puts
449 puts
450 puts "Please enter settings for your Mantis database"
450 puts "Please enter settings for your Mantis database"
451 [:adapter, :host, :database, :username, :password].each do |param|
451 [:adapter, :host, :database, :username, :password].each do |param|
452 print "#{param} [#{db_params[param]}]: "
452 print "#{param} [#{db_params[param]}]: "
453 value = STDIN.gets.chomp!
453 value = STDIN.gets.chomp!
454 db_params[param] = value unless value.blank?
454 db_params[param] = value unless value.blank?
455 end
455 end
456
456
457 while true
457 while true
458 print "encoding [ISO-8859-1]: "
458 print "encoding [ISO-8859-1]: "
459 encoding = STDIN.gets.chomp!
459 encoding = STDIN.gets.chomp!
460 encoding = 'ISO-8859-1' if encoding.blank?
460 encoding = 'ISO-8859-1' if encoding.blank?
461 break if MantisMigrate.encoding encoding
461 break if MantisMigrate.encoding encoding
462 puts "Invalid encoding!"
462 puts "Invalid encoding!"
463 end
463 end
464 puts
464 puts
465
465
466 # Make sure bugs can refer bugs in other projects
466 # Make sure bugs can refer bugs in other projects
467 Setting.cross_project_issue_relations = 1
467 Setting.cross_project_issue_relations = 1
468
468
469 MantisMigrate.establish_connection db_params
469 MantisMigrate.establish_connection db_params
470 MantisMigrate.migrate
470 MantisMigrate.migrate
471 end
471 end
General Comments 0
You need to be logged in to leave comments. Login now