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