##// END OF EJS Templates
Trac and Mantis importers: check that default configuration is loaded before processing....
Jean-Philippe Lang -
r1207:494a6ecfb0a9
parent child
Show More
@@ -1,497 +1,505
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.get_values('IPRI')
43 priorities = Enumeration.get_values('IPRI')
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
198 def read
199 content
199 content
200 end
200 end
201 end
201 end
202
202
203 class MantisBugRelationship < ActiveRecord::Base
203 class MantisBugRelationship < ActiveRecord::Base
204 set_table_name :mantis_bug_relationship_table
204 set_table_name :mantis_bug_relationship_table
205 end
205 end
206
206
207 class MantisBugMonitor < ActiveRecord::Base
207 class MantisBugMonitor < ActiveRecord::Base
208 set_table_name :mantis_bug_monitor_table
208 set_table_name :mantis_bug_monitor_table
209 end
209 end
210
210
211 class MantisNews < ActiveRecord::Base
211 class MantisNews < ActiveRecord::Base
212 set_table_name :mantis_news_table
212 set_table_name :mantis_news_table
213 end
213 end
214
214
215 class MantisCustomField < ActiveRecord::Base
215 class MantisCustomField < ActiveRecord::Base
216 set_table_name :mantis_custom_field_table
216 set_table_name :mantis_custom_field_table
217 set_inheritance_column :none
217 set_inheritance_column :none
218 has_many :values, :class_name => "MantisCustomFieldString", :foreign_key => :field_id
218 has_many :values, :class_name => "MantisCustomFieldString", :foreign_key => :field_id
219 has_many :projects, :class_name => "MantisCustomFieldProject", :foreign_key => :field_id
219 has_many :projects, :class_name => "MantisCustomFieldProject", :foreign_key => :field_id
220
220
221 def format
221 def format
222 read_attribute :type
222 read_attribute :type
223 end
223 end
224
224
225 def name
225 def name
226 read_attribute(:name)[0..29].gsub(/[^\w\s\'\-]/, '-')
226 read_attribute(:name)[0..29].gsub(/[^\w\s\'\-]/, '-')
227 end
227 end
228 end
228 end
229
229
230 class MantisCustomFieldProject < ActiveRecord::Base
230 class MantisCustomFieldProject < ActiveRecord::Base
231 set_table_name :mantis_custom_field_project_table
231 set_table_name :mantis_custom_field_project_table
232 end
232 end
233
233
234 class MantisCustomFieldString < ActiveRecord::Base
234 class MantisCustomFieldString < ActiveRecord::Base
235 set_table_name :mantis_custom_field_string_table
235 set_table_name :mantis_custom_field_string_table
236 end
236 end
237
237
238
238
239 def self.migrate
239 def self.migrate
240
240
241 # Users
241 # Users
242 print "Migrating users"
242 print "Migrating users"
243 User.delete_all "login <> 'admin'"
243 User.delete_all "login <> 'admin'"
244 users_map = {}
244 users_map = {}
245 users_migrated = 0
245 users_migrated = 0
246 MantisUser.find(:all).each do |user|
246 MantisUser.find(:all).each do |user|
247 u = User.new :firstname => encode(user.firstname),
247 u = User.new :firstname => encode(user.firstname),
248 :lastname => encode(user.lastname),
248 :lastname => encode(user.lastname),
249 :mail => user.email,
249 :mail => user.email,
250 :last_login_on => user.last_visit
250 :last_login_on => user.last_visit
251 u.login = user.username
251 u.login = user.username
252 u.password = 'mantis'
252 u.password = 'mantis'
253 u.status = User::STATUS_LOCKED if user.enabled != 1
253 u.status = User::STATUS_LOCKED if user.enabled != 1
254 u.admin = true if user.access_level == 90
254 u.admin = true if user.access_level == 90
255 next unless u.save!
255 next unless u.save!
256 users_migrated += 1
256 users_migrated += 1
257 users_map[user.id] = u.id
257 users_map[user.id] = u.id
258 print '.'
258 print '.'
259 end
259 end
260 puts
260 puts
261
261
262 # Projects
262 # Projects
263 print "Migrating projects"
263 print "Migrating projects"
264 Project.destroy_all
264 Project.destroy_all
265 projects_map = {}
265 projects_map = {}
266 versions_map = {}
266 versions_map = {}
267 categories_map = {}
267 categories_map = {}
268 MantisProject.find(:all).each do |project|
268 MantisProject.find(:all).each do |project|
269 p = Project.new :name => encode(project.name),
269 p = Project.new :name => encode(project.name),
270 :description => encode(project.description)
270 :description => encode(project.description)
271 p.identifier = project.identifier
271 p.identifier = project.identifier
272 next unless p.save
272 next unless p.save
273 projects_map[project.id] = p.id
273 projects_map[project.id] = p.id
274 p.enabled_module_names = ['issue_tracking', 'news', 'wiki']
274 p.enabled_module_names = ['issue_tracking', 'news', 'wiki']
275 p.trackers << TRACKER_BUG
275 p.trackers << TRACKER_BUG
276 p.trackers << TRACKER_FEATURE
276 p.trackers << TRACKER_FEATURE
277 print '.'
277 print '.'
278
278
279 # Project members
279 # Project members
280 project.members.each do |member|
280 project.members.each do |member|
281 m = Member.new :user => User.find_by_id(users_map[member.user_id]),
281 m = Member.new :user => User.find_by_id(users_map[member.user_id]),
282 :role => ROLE_MAPPING[member.access_level] || DEFAULT_ROLE
282 :role => ROLE_MAPPING[member.access_level] || DEFAULT_ROLE
283 m.project = p
283 m.project = p
284 m.save
284 m.save
285 end
285 end
286
286
287 # Project versions
287 # Project versions
288 project.versions.each do |version|
288 project.versions.each do |version|
289 v = Version.new :name => encode(version.version),
289 v = Version.new :name => encode(version.version),
290 :description => encode(version.description),
290 :description => encode(version.description),
291 :effective_date => version.date_order.to_date
291 :effective_date => version.date_order.to_date
292 v.project = p
292 v.project = p
293 v.save
293 v.save
294 versions_map[version.id] = v.id
294 versions_map[version.id] = v.id
295 end
295 end
296
296
297 # Project categories
297 # Project categories
298 project.categories.each do |category|
298 project.categories.each do |category|
299 g = IssueCategory.new :name => category.category[0,30]
299 g = IssueCategory.new :name => category.category[0,30]
300 g.project = p
300 g.project = p
301 g.save
301 g.save
302 categories_map[category.category] = g.id
302 categories_map[category.category] = g.id
303 end
303 end
304 end
304 end
305 puts
305 puts
306
306
307 # Bugs
307 # Bugs
308 print "Migrating bugs"
308 print "Migrating bugs"
309 Issue.destroy_all
309 Issue.destroy_all
310 issues_map = {}
310 issues_map = {}
311 keep_bug_ids = (Issue.count == 0)
311 keep_bug_ids = (Issue.count == 0)
312 MantisBug.find(:all, :order => 'id ASC').each do |bug|
312 MantisBug.find(:all, :order => 'id ASC').each do |bug|
313 next unless projects_map[bug.project_id] && users_map[bug.reporter_id]
313 next unless projects_map[bug.project_id] && users_map[bug.reporter_id]
314 i = Issue.new :project_id => projects_map[bug.project_id],
314 i = Issue.new :project_id => projects_map[bug.project_id],
315 :subject => encode(bug.summary),
315 :subject => encode(bug.summary),
316 :description => encode(bug.bug_text.full_description),
316 :description => encode(bug.bug_text.full_description),
317 :priority => PRIORITY_MAPPING[bug.priority] || DEFAULT_PRIORITY,
317 :priority => PRIORITY_MAPPING[bug.priority] || DEFAULT_PRIORITY,
318 :created_on => bug.date_submitted,
318 :created_on => bug.date_submitted,
319 :updated_on => bug.last_updated
319 :updated_on => bug.last_updated
320 i.author = User.find_by_id(users_map[bug.reporter_id])
320 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?
321 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?
322 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
323 i.status = STATUS_MAPPING[bug.status] || DEFAULT_STATUS
324 i.tracker = (bug.severity == 10 ? TRACKER_FEATURE : TRACKER_BUG)
324 i.tracker = (bug.severity == 10 ? TRACKER_FEATURE : TRACKER_BUG)
325 i.id = bug.id if keep_bug_ids
325 i.id = bug.id if keep_bug_ids
326 next unless i.save
326 next unless i.save
327 issues_map[bug.id] = i.id
327 issues_map[bug.id] = i.id
328 print '.'
328 print '.'
329
329
330 # Assignee
330 # Assignee
331 # Redmine checks that the assignee is a project member
331 # Redmine checks that the assignee is a project member
332 if (bug.handler_id && users_map[bug.handler_id])
332 if (bug.handler_id && users_map[bug.handler_id])
333 i.assigned_to = User.find_by_id(users_map[bug.handler_id])
333 i.assigned_to = User.find_by_id(users_map[bug.handler_id])
334 i.save_with_validation(false)
334 i.save_with_validation(false)
335 end
335 end
336
336
337 # Bug notes
337 # Bug notes
338 bug.bug_notes.each do |note|
338 bug.bug_notes.each do |note|
339 next unless users_map[note.reporter_id]
339 next unless users_map[note.reporter_id]
340 n = Journal.new :notes => encode(note.bug_note_text.note),
340 n = Journal.new :notes => encode(note.bug_note_text.note),
341 :created_on => note.date_submitted
341 :created_on => note.date_submitted
342 n.user = User.find_by_id(users_map[note.reporter_id])
342 n.user = User.find_by_id(users_map[note.reporter_id])
343 n.journalized = i
343 n.journalized = i
344 n.save
344 n.save
345 end
345 end
346
346
347 # Bug files
347 # Bug files
348 bug.bug_files.each do |file|
348 bug.bug_files.each do |file|
349 a = Attachment.new :created_on => file.date_added
349 a = Attachment.new :created_on => file.date_added
350 a.file = file
350 a.file = file
351 a.author = User.find :first
351 a.author = User.find :first
352 a.container = i
352 a.container = i
353 a.save
353 a.save
354 end
354 end
355
355
356 # Bug monitors
356 # Bug monitors
357 bug.bug_monitors.each do |monitor|
357 bug.bug_monitors.each do |monitor|
358 next unless users_map[monitor.user_id]
358 next unless users_map[monitor.user_id]
359 i.add_watcher(User.find_by_id(users_map[monitor.user_id]))
359 i.add_watcher(User.find_by_id(users_map[monitor.user_id]))
360 end
360 end
361 end
361 end
362
362
363 # update issue id sequence if needed (postgresql)
363 # update issue id sequence if needed (postgresql)
364 Issue.connection.reset_pk_sequence!(Issue.table_name) if Issue.connection.respond_to?('reset_pk_sequence!')
364 Issue.connection.reset_pk_sequence!(Issue.table_name) if Issue.connection.respond_to?('reset_pk_sequence!')
365 puts
365 puts
366
366
367 # Bug relationships
367 # Bug relationships
368 print "Migrating bug relations"
368 print "Migrating bug relations"
369 MantisBugRelationship.find(:all).each do |relation|
369 MantisBugRelationship.find(:all).each do |relation|
370 next unless issues_map[relation.source_bug_id] && issues_map[relation.destination_bug_id]
370 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]
371 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])
372 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])
373 r.issue_to = Issue.find_by_id(issues_map[relation.destination_bug_id])
374 pp r unless r.save
374 pp r unless r.save
375 print '.'
375 print '.'
376 end
376 end
377 puts
377 puts
378
378
379 # News
379 # News
380 print "Migrating news"
380 print "Migrating news"
381 News.destroy_all
381 News.destroy_all
382 MantisNews.find(:all, :conditions => 'project_id > 0').each do |news|
382 MantisNews.find(:all, :conditions => 'project_id > 0').each do |news|
383 next unless projects_map[news.project_id]
383 next unless projects_map[news.project_id]
384 n = News.new :project_id => projects_map[news.project_id],
384 n = News.new :project_id => projects_map[news.project_id],
385 :title => encode(news.headline[0..59]),
385 :title => encode(news.headline[0..59]),
386 :description => encode(news.body),
386 :description => encode(news.body),
387 :created_on => news.date_posted
387 :created_on => news.date_posted
388 n.author = User.find_by_id(users_map[news.poster_id])
388 n.author = User.find_by_id(users_map[news.poster_id])
389 n.save
389 n.save
390 print '.'
390 print '.'
391 end
391 end
392 puts
392 puts
393
393
394 # Custom fields
394 # Custom fields
395 print "Migrating custom fields"
395 print "Migrating custom fields"
396 IssueCustomField.destroy_all
396 IssueCustomField.destroy_all
397 MantisCustomField.find(:all).each do |field|
397 MantisCustomField.find(:all).each do |field|
398 f = IssueCustomField.new :name => field.name[0..29],
398 f = IssueCustomField.new :name => field.name[0..29],
399 :field_format => CUSTOM_FIELD_TYPE_MAPPING[field.format],
399 :field_format => CUSTOM_FIELD_TYPE_MAPPING[field.format],
400 :min_length => field.length_min,
400 :min_length => field.length_min,
401 :max_length => field.length_max,
401 :max_length => field.length_max,
402 :regexp => field.valid_regexp,
402 :regexp => field.valid_regexp,
403 :possible_values => field.possible_values.split('|'),
403 :possible_values => field.possible_values.split('|'),
404 :is_required => field.require_report?
404 :is_required => field.require_report?
405 next unless f.save
405 next unless f.save
406 print '.'
406 print '.'
407
407
408 # Trackers association
408 # Trackers association
409 f.trackers = Tracker.find :all
409 f.trackers = Tracker.find :all
410
410
411 # Projects association
411 # Projects association
412 field.projects.each do |project|
412 field.projects.each do |project|
413 f.projects << Project.find_by_id(projects_map[project.project_id]) if projects_map[project.project_id]
413 f.projects << Project.find_by_id(projects_map[project.project_id]) if projects_map[project.project_id]
414 end
414 end
415
415
416 # Values
416 # Values
417 field.values.each do |value|
417 field.values.each do |value|
418 v = CustomValue.new :custom_field_id => f.id,
418 v = CustomValue.new :custom_field_id => f.id,
419 :value => value.value
419 :value => value.value
420 v.customized = Issue.find_by_id(issues_map[value.bug_id]) if issues_map[value.bug_id]
420 v.customized = Issue.find_by_id(issues_map[value.bug_id]) if issues_map[value.bug_id]
421 v.save
421 v.save
422 end unless f.new_record?
422 end unless f.new_record?
423 end
423 end
424 puts
424 puts
425
425
426 puts
426 puts
427 puts "Users: #{users_migrated}/#{MantisUser.count}"
427 puts "Users: #{users_migrated}/#{MantisUser.count}"
428 puts "Projects: #{Project.count}/#{MantisProject.count}"
428 puts "Projects: #{Project.count}/#{MantisProject.count}"
429 puts "Memberships: #{Member.count}/#{MantisProjectUser.count}"
429 puts "Memberships: #{Member.count}/#{MantisProjectUser.count}"
430 puts "Versions: #{Version.count}/#{MantisVersion.count}"
430 puts "Versions: #{Version.count}/#{MantisVersion.count}"
431 puts "Categories: #{IssueCategory.count}/#{MantisCategory.count}"
431 puts "Categories: #{IssueCategory.count}/#{MantisCategory.count}"
432 puts "Bugs: #{Issue.count}/#{MantisBug.count}"
432 puts "Bugs: #{Issue.count}/#{MantisBug.count}"
433 puts "Bug notes: #{Journal.count}/#{MantisBugNote.count}"
433 puts "Bug notes: #{Journal.count}/#{MantisBugNote.count}"
434 puts "Bug files: #{Attachment.count}/#{MantisBugFile.count}"
434 puts "Bug files: #{Attachment.count}/#{MantisBugFile.count}"
435 puts "Bug relations: #{IssueRelation.count}/#{MantisBugRelationship.count}"
435 puts "Bug relations: #{IssueRelation.count}/#{MantisBugRelationship.count}"
436 puts "Bug monitors: #{Watcher.count}/#{MantisBugMonitor.count}"
436 puts "Bug monitors: #{Watcher.count}/#{MantisBugMonitor.count}"
437 puts "News: #{News.count}/#{MantisNews.count}"
437 puts "News: #{News.count}/#{MantisNews.count}"
438 puts "Custom fields: #{IssueCustomField.count}/#{MantisCustomField.count}"
438 puts "Custom fields: #{IssueCustomField.count}/#{MantisCustomField.count}"
439 end
439 end
440
440
441 def self.encoding(charset)
441 def self.encoding(charset)
442 @ic = Iconv.new('UTF-8', charset)
442 @ic = Iconv.new('UTF-8', charset)
443 rescue Iconv::InvalidEncoding
443 rescue Iconv::InvalidEncoding
444 return false
444 return false
445 end
445 end
446
446
447 def self.establish_connection(params)
447 def self.establish_connection(params)
448 constants.each do |const|
448 constants.each do |const|
449 klass = const_get(const)
449 klass = const_get(const)
450 next unless klass.respond_to? 'establish_connection'
450 next unless klass.respond_to? 'establish_connection'
451 klass.establish_connection params
451 klass.establish_connection params
452 end
452 end
453 end
453 end
454
454
455 def self.encode(text)
455 def self.encode(text)
456 @ic.iconv text
456 @ic.iconv text
457 rescue
457 rescue
458 text
458 text
459 end
459 end
460 end
460 end
461
461
462 puts
462 puts
463 if Redmine::DefaultData::Loader.no_data?
464 puts "Redmine configuration need to be loaded before importing data."
465 puts "Please, run this first:"
466 puts
467 puts " rake redmine:load_default_data RAILS_ENV=\"#{ENV['RAILS_ENV']}\""
468 exit
469 end
470
463 puts "WARNING: Your Redmine data will be deleted during this process."
471 puts "WARNING: Your Redmine data will be deleted during this process."
464 print "Are you sure you want to continue ? [y/N] "
472 print "Are you sure you want to continue ? [y/N] "
465 break unless STDIN.gets.match(/^y$/i)
473 break unless STDIN.gets.match(/^y$/i)
466
474
467 # Default Mantis database settings
475 # Default Mantis database settings
468 db_params = {:adapter => 'mysql',
476 db_params = {:adapter => 'mysql',
469 :database => 'bugtracker',
477 :database => 'bugtracker',
470 :host => 'localhost',
478 :host => 'localhost',
471 :username => 'root',
479 :username => 'root',
472 :password => '' }
480 :password => '' }
473
481
474 puts
482 puts
475 puts "Please enter settings for your Mantis database"
483 puts "Please enter settings for your Mantis database"
476 [:adapter, :host, :database, :username, :password].each do |param|
484 [:adapter, :host, :database, :username, :password].each do |param|
477 print "#{param} [#{db_params[param]}]: "
485 print "#{param} [#{db_params[param]}]: "
478 value = STDIN.gets.chomp!
486 value = STDIN.gets.chomp!
479 db_params[param] = value unless value.blank?
487 db_params[param] = value unless value.blank?
480 end
488 end
481
489
482 while true
490 while true
483 print "encoding [UTF-8]: "
491 print "encoding [UTF-8]: "
484 encoding = STDIN.gets.chomp!
492 encoding = STDIN.gets.chomp!
485 encoding = 'UTF-8' if encoding.blank?
493 encoding = 'UTF-8' if encoding.blank?
486 break if MantisMigrate.encoding encoding
494 break if MantisMigrate.encoding encoding
487 puts "Invalid encoding!"
495 puts "Invalid encoding!"
488 end
496 end
489 puts
497 puts
490
498
491 # Make sure bugs can refer bugs in other projects
499 # Make sure bugs can refer bugs in other projects
492 Setting.cross_project_issue_relations = 1 if Setting.respond_to? 'cross_project_issue_relations'
500 Setting.cross_project_issue_relations = 1 if Setting.respond_to? 'cross_project_issue_relations'
493
501
494 MantisMigrate.establish_connection db_params
502 MantisMigrate.establish_connection db_params
495 MantisMigrate.migrate
503 MantisMigrate.migrate
496 end
504 end
497 end
505 end
@@ -1,607 +1,615
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 require 'active_record'
18 require 'active_record'
19 require 'iconv'
19 require 'iconv'
20 require 'pp'
20 require 'pp'
21
21
22 namespace :redmine do
22 namespace :redmine do
23 desc 'Trac migration script'
23 desc 'Trac migration script'
24 task :migrate_from_trac => :environment do
24 task :migrate_from_trac => :environment do
25
25
26 module TracMigrate
26 module TracMigrate
27 TICKET_MAP = []
27 TICKET_MAP = []
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 = {'new' => DEFAULT_STATUS,
34 STATUS_MAPPING = {'new' => DEFAULT_STATUS,
35 'reopened' => feedback_status,
35 'reopened' => feedback_status,
36 'assigned' => assigned_status,
36 'assigned' => assigned_status,
37 'closed' => closed_status
37 'closed' => closed_status
38 }
38 }
39
39
40 priorities = Enumeration.get_values('IPRI')
40 priorities = Enumeration.get_values('IPRI')
41 DEFAULT_PRIORITY = priorities[0]
41 DEFAULT_PRIORITY = priorities[0]
42 PRIORITY_MAPPING = {'lowest' => priorities[0],
42 PRIORITY_MAPPING = {'lowest' => priorities[0],
43 'low' => priorities[0],
43 'low' => priorities[0],
44 'normal' => priorities[1],
44 'normal' => priorities[1],
45 'high' => priorities[2],
45 'high' => priorities[2],
46 'highest' => priorities[3],
46 'highest' => priorities[3],
47 # ---
47 # ---
48 'trivial' => priorities[0],
48 'trivial' => priorities[0],
49 'minor' => priorities[1],
49 'minor' => priorities[1],
50 'major' => priorities[2],
50 'major' => priorities[2],
51 'critical' => priorities[3],
51 'critical' => priorities[3],
52 'blocker' => priorities[4]
52 'blocker' => priorities[4]
53 }
53 }
54
54
55 TRACKER_BUG = Tracker.find_by_position(1)
55 TRACKER_BUG = Tracker.find_by_position(1)
56 TRACKER_FEATURE = Tracker.find_by_position(2)
56 TRACKER_FEATURE = Tracker.find_by_position(2)
57 DEFAULT_TRACKER = TRACKER_BUG
57 DEFAULT_TRACKER = TRACKER_BUG
58 TRACKER_MAPPING = {'defect' => TRACKER_BUG,
58 TRACKER_MAPPING = {'defect' => TRACKER_BUG,
59 'enhancement' => TRACKER_FEATURE,
59 'enhancement' => TRACKER_FEATURE,
60 'task' => TRACKER_FEATURE,
60 'task' => TRACKER_FEATURE,
61 'patch' =>TRACKER_FEATURE
61 'patch' =>TRACKER_FEATURE
62 }
62 }
63
63
64 roles = Role.find(:all, :conditions => {:builtin => 0}, :order => 'position ASC')
64 roles = Role.find(:all, :conditions => {:builtin => 0}, :order => 'position ASC')
65 manager_role = roles[0]
65 manager_role = roles[0]
66 developer_role = roles[1]
66 developer_role = roles[1]
67 DEFAULT_ROLE = roles.last
67 DEFAULT_ROLE = roles.last
68 ROLE_MAPPING = {'admin' => manager_role,
68 ROLE_MAPPING = {'admin' => manager_role,
69 'developer' => developer_role
69 'developer' => developer_role
70 }
70 }
71
71
72 class TracComponent < ActiveRecord::Base
72 class TracComponent < ActiveRecord::Base
73 set_table_name :component
73 set_table_name :component
74 end
74 end
75
75
76 class TracMilestone < ActiveRecord::Base
76 class TracMilestone < ActiveRecord::Base
77 set_table_name :milestone
77 set_table_name :milestone
78
78
79 def due
79 def due
80 if read_attribute(:due) > 0
80 if read_attribute(:due) > 0
81 Time.at(read_attribute(:due)).to_date
81 Time.at(read_attribute(:due)).to_date
82 else
82 else
83 nil
83 nil
84 end
84 end
85 end
85 end
86 end
86 end
87
87
88 class TracTicketCustom < ActiveRecord::Base
88 class TracTicketCustom < ActiveRecord::Base
89 set_table_name :ticket_custom
89 set_table_name :ticket_custom
90 end
90 end
91
91
92 class TracAttachment < ActiveRecord::Base
92 class TracAttachment < ActiveRecord::Base
93 set_table_name :attachment
93 set_table_name :attachment
94 set_inheritance_column :none
94 set_inheritance_column :none
95
95
96 def time; Time.at(read_attribute(:time)) end
96 def time; Time.at(read_attribute(:time)) end
97
97
98 def original_filename
98 def original_filename
99 filename
99 filename
100 end
100 end
101
101
102 def content_type
102 def content_type
103 Redmine::MimeType.of(filename) || ''
103 Redmine::MimeType.of(filename) || ''
104 end
104 end
105
105
106 def exist?
106 def exist?
107 File.file? trac_fullpath
107 File.file? trac_fullpath
108 end
108 end
109
109
110 def read
110 def read
111 File.open("#{trac_fullpath}", 'rb').read
111 File.open("#{trac_fullpath}", 'rb').read
112 end
112 end
113
113
114 private
114 private
115 def trac_fullpath
115 def trac_fullpath
116 attachment_type = read_attribute(:type)
116 attachment_type = read_attribute(:type)
117 trac_file = filename.gsub( /[^a-zA-Z0-9\-_\.!~*']/n ) {|x| sprintf('%%%02x', x[0]) }
117 trac_file = filename.gsub( /[^a-zA-Z0-9\-_\.!~*']/n ) {|x| sprintf('%%%02x', x[0]) }
118 "#{TracMigrate.trac_attachments_directory}/#{attachment_type}/#{id}/#{trac_file}"
118 "#{TracMigrate.trac_attachments_directory}/#{attachment_type}/#{id}/#{trac_file}"
119 end
119 end
120 end
120 end
121
121
122 class TracTicket < ActiveRecord::Base
122 class TracTicket < ActiveRecord::Base
123 set_table_name :ticket
123 set_table_name :ticket
124 set_inheritance_column :none
124 set_inheritance_column :none
125
125
126 # ticket changes: only migrate status changes and comments
126 # ticket changes: only migrate status changes and comments
127 has_many :changes, :class_name => "TracTicketChange", :foreign_key => :ticket
127 has_many :changes, :class_name => "TracTicketChange", :foreign_key => :ticket
128 has_many :attachments, :class_name => "TracAttachment", :foreign_key => :id, :conditions => "#{TracMigrate::TracAttachment.table_name}.type = 'ticket'"
128 has_many :attachments, :class_name => "TracAttachment", :foreign_key => :id, :conditions => "#{TracMigrate::TracAttachment.table_name}.type = 'ticket'"
129 has_many :customs, :class_name => "TracTicketCustom", :foreign_key => :ticket
129 has_many :customs, :class_name => "TracTicketCustom", :foreign_key => :ticket
130
130
131 def ticket_type
131 def ticket_type
132 read_attribute(:type)
132 read_attribute(:type)
133 end
133 end
134
134
135 def summary
135 def summary
136 read_attribute(:summary).blank? ? "(no subject)" : read_attribute(:summary)
136 read_attribute(:summary).blank? ? "(no subject)" : read_attribute(:summary)
137 end
137 end
138
138
139 def description
139 def description
140 read_attribute(:description).blank? ? summary : read_attribute(:description)
140 read_attribute(:description).blank? ? summary : read_attribute(:description)
141 end
141 end
142
142
143 def time; Time.at(read_attribute(:time)) end
143 def time; Time.at(read_attribute(:time)) end
144 end
144 end
145
145
146 class TracTicketChange < ActiveRecord::Base
146 class TracTicketChange < ActiveRecord::Base
147 set_table_name :ticket_change
147 set_table_name :ticket_change
148
148
149 def time; Time.at(read_attribute(:time)) end
149 def time; Time.at(read_attribute(:time)) end
150 end
150 end
151
151
152 TRAC_WIKI_PAGES = %w(TracAccessibility TracAdmin TracBackup TracBrowser TracCgi TracChangeset \
152 TRAC_WIKI_PAGES = %w(TracAccessibility TracAdmin TracBackup TracBrowser TracCgi TracChangeset \
153 TracEnvironment TracFastCgi TracGuide TracImport TracIni TracInstall TracInterfaceCustomization \
153 TracEnvironment TracFastCgi TracGuide TracImport TracIni TracInstall TracInterfaceCustomization \
154 TracLinks TracLogging TracModPython TracNotification TracPermissions TracPlugins TracQuery \
154 TracLinks TracLogging TracModPython TracNotification TracPermissions TracPlugins TracQuery \
155 TracReports TracRoadmap TracRss TracSearch TracStandalone TracSupport TracSyntaxColoring TracTickets \
155 TracReports TracRoadmap TracRss TracSearch TracStandalone TracSupport TracSyntaxColoring TracTickets \
156 TracTicketsCustomFields TracTimeline TracUnicode TracUpgrade TracWiki WikiDeletePage WikiFormatting \
156 TracTicketsCustomFields TracTimeline TracUnicode TracUpgrade TracWiki WikiDeletePage WikiFormatting \
157 WikiHtml WikiMacros WikiNewPage WikiPageNames WikiProcessors WikiRestructuredText WikiRestructuredTextLinks \
157 WikiHtml WikiMacros WikiNewPage WikiPageNames WikiProcessors WikiRestructuredText WikiRestructuredTextLinks \
158 CamelCase TitleIndex)
158 CamelCase TitleIndex)
159
159
160 class TracWikiPage < ActiveRecord::Base
160 class TracWikiPage < ActiveRecord::Base
161 set_table_name :wiki
161 set_table_name :wiki
162 set_primary_key :name
162 set_primary_key :name
163
163
164 has_many :attachments, :class_name => "TracAttachment", :foreign_key => :id, :conditions => "#{TracMigrate::TracAttachment.table_name}.type = 'wiki'"
164 has_many :attachments, :class_name => "TracAttachment", :foreign_key => :id, :conditions => "#{TracMigrate::TracAttachment.table_name}.type = 'wiki'"
165
165
166 def self.columns
166 def self.columns
167 # Hides readonly Trac field to prevent clash with AR readonly? method (Rails 2.0)
167 # Hides readonly Trac field to prevent clash with AR readonly? method (Rails 2.0)
168 super.select {|column| column.name.to_s != 'readonly'}
168 super.select {|column| column.name.to_s != 'readonly'}
169 end
169 end
170 end
170 end
171
171
172 class TracPermission < ActiveRecord::Base
172 class TracPermission < ActiveRecord::Base
173 set_table_name :permission
173 set_table_name :permission
174 end
174 end
175
175
176 def self.find_or_create_user(username, project_member = false)
176 def self.find_or_create_user(username, project_member = false)
177 return User.anonymous if username.blank?
177 return User.anonymous if username.blank?
178
178
179 u = User.find_by_login(username)
179 u = User.find_by_login(username)
180 if !u
180 if !u
181 # Create a new user if not found
181 # Create a new user if not found
182 mail = username[0,limit_for(User, 'mail')]
182 mail = username[0,limit_for(User, 'mail')]
183 mail = "#{mail}@foo.bar" unless mail.include?("@")
183 mail = "#{mail}@foo.bar" unless mail.include?("@")
184 u = User.new :firstname => username[0,limit_for(User, 'firstname')].gsub(/[^\w\s\'\-]/i, '-'),
184 u = User.new :firstname => username[0,limit_for(User, 'firstname')].gsub(/[^\w\s\'\-]/i, '-'),
185 :lastname => '-',
185 :lastname => '-',
186 :mail => mail.gsub(/[^-@a-z0-9\.]/i, '-')
186 :mail => mail.gsub(/[^-@a-z0-9\.]/i, '-')
187 u.login = username[0,limit_for(User, 'login')].gsub(/[^a-z0-9_\-@\.]/i, '-')
187 u.login = username[0,limit_for(User, 'login')].gsub(/[^a-z0-9_\-@\.]/i, '-')
188 u.password = 'trac'
188 u.password = 'trac'
189 u.admin = true if TracPermission.find_by_username_and_action(username, 'admin')
189 u.admin = true if TracPermission.find_by_username_and_action(username, 'admin')
190 # finally, a default user is used if the new user is not valid
190 # finally, a default user is used if the new user is not valid
191 u = User.find(:first) unless u.save
191 u = User.find(:first) unless u.save
192 end
192 end
193 # Make sure he is a member of the project
193 # Make sure he is a member of the project
194 if project_member && !u.member_of?(@target_project)
194 if project_member && !u.member_of?(@target_project)
195 role = DEFAULT_ROLE
195 role = DEFAULT_ROLE
196 if u.admin
196 if u.admin
197 role = ROLE_MAPPING['admin']
197 role = ROLE_MAPPING['admin']
198 elsif TracPermission.find_by_username_and_action(username, 'developer')
198 elsif TracPermission.find_by_username_and_action(username, 'developer')
199 role = ROLE_MAPPING['developer']
199 role = ROLE_MAPPING['developer']
200 end
200 end
201 Member.create(:user => u, :project => @target_project, :role => role)
201 Member.create(:user => u, :project => @target_project, :role => role)
202 u.reload
202 u.reload
203 end
203 end
204 u
204 u
205 end
205 end
206
206
207 # Basic wiki syntax conversion
207 # Basic wiki syntax conversion
208 def self.convert_wiki_text(text)
208 def self.convert_wiki_text(text)
209 # Titles
209 # Titles
210 text = text.gsub(/^(\=+)\s(.+)\s(\=+)/) {|s| "\nh#{$1.length}. #{$2}\n"}
210 text = text.gsub(/^(\=+)\s(.+)\s(\=+)/) {|s| "\nh#{$1.length}. #{$2}\n"}
211 # External Links
211 # External Links
212 text = text.gsub(/\[(http[^\s]+)\s+([^\]]+)\]/) {|s| "\"#{$2}\":#{$1}"}
212 text = text.gsub(/\[(http[^\s]+)\s+([^\]]+)\]/) {|s| "\"#{$2}\":#{$1}"}
213 # Internal Links
213 # Internal Links
214 text = text.gsub(/\[\[BR\]\]/, "\n") # This has to go before the rules below
214 text = text.gsub(/\[\[BR\]\]/, "\n") # This has to go before the rules below
215 text = text.gsub(/\[\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
215 text = text.gsub(/\[\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
216 text = text.gsub(/\[wiki:\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
216 text = text.gsub(/\[wiki:\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
217 text = text.gsub(/\[wiki:\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
217 text = text.gsub(/\[wiki:\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
218 text = text.gsub(/\[wiki:([^\s\]]+).*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
218 text = text.gsub(/\[wiki:([^\s\]]+).*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
219 # Revisions links
219 # Revisions links
220 text = text.gsub(/\[(\d+)\]/, 'r\1')
220 text = text.gsub(/\[(\d+)\]/, 'r\1')
221 # Ticket number re-writing
221 # Ticket number re-writing
222 text = text.gsub(/#(\d+)/) do |s|
222 text = text.gsub(/#(\d+)/) do |s|
223 if $1.length < 10
223 if $1.length < 10
224 TICKET_MAP[$1.to_i] ||= $1
224 TICKET_MAP[$1.to_i] ||= $1
225 "\##{TICKET_MAP[$1.to_i] || $1}"
225 "\##{TICKET_MAP[$1.to_i] || $1}"
226 else
226 else
227 s
227 s
228 end
228 end
229 end
229 end
230 # Preformatted blocks
230 # Preformatted blocks
231 text = text.gsub(/\{\{\{/, '<pre>')
231 text = text.gsub(/\{\{\{/, '<pre>')
232 text = text.gsub(/\}\}\}/, '</pre>')
232 text = text.gsub(/\}\}\}/, '</pre>')
233 # Highlighting
233 # Highlighting
234 text = text.gsub(/'''''([^\s])/, '_*\1')
234 text = text.gsub(/'''''([^\s])/, '_*\1')
235 text = text.gsub(/([^\s])'''''/, '\1*_')
235 text = text.gsub(/([^\s])'''''/, '\1*_')
236 text = text.gsub(/'''/, '*')
236 text = text.gsub(/'''/, '*')
237 text = text.gsub(/''/, '_')
237 text = text.gsub(/''/, '_')
238 text = text.gsub(/__/, '+')
238 text = text.gsub(/__/, '+')
239 text = text.gsub(/~~/, '-')
239 text = text.gsub(/~~/, '-')
240 text = text.gsub(/`/, '@')
240 text = text.gsub(/`/, '@')
241 text = text.gsub(/,,/, '~')
241 text = text.gsub(/,,/, '~')
242 # Lists
242 # Lists
243 text = text.gsub(/^([ ]+)\* /) {|s| '*' * $1.length + " "}
243 text = text.gsub(/^([ ]+)\* /) {|s| '*' * $1.length + " "}
244
244
245 text
245 text
246 end
246 end
247
247
248 def self.migrate
248 def self.migrate
249 establish_connection
249 establish_connection
250
250
251 # Quick database test
251 # Quick database test
252 TracComponent.count
252 TracComponent.count
253
253
254 migrated_components = 0
254 migrated_components = 0
255 migrated_milestones = 0
255 migrated_milestones = 0
256 migrated_tickets = 0
256 migrated_tickets = 0
257 migrated_custom_values = 0
257 migrated_custom_values = 0
258 migrated_ticket_attachments = 0
258 migrated_ticket_attachments = 0
259 migrated_wiki_edits = 0
259 migrated_wiki_edits = 0
260 migrated_wiki_attachments = 0
260 migrated_wiki_attachments = 0
261
261
262 # Components
262 # Components
263 print "Migrating components"
263 print "Migrating components"
264 issues_category_map = {}
264 issues_category_map = {}
265 TracComponent.find(:all).each do |component|
265 TracComponent.find(:all).each do |component|
266 print '.'
266 print '.'
267 STDOUT.flush
267 STDOUT.flush
268 c = IssueCategory.new :project => @target_project,
268 c = IssueCategory.new :project => @target_project,
269 :name => encode(component.name[0, limit_for(IssueCategory, 'name')])
269 :name => encode(component.name[0, limit_for(IssueCategory, 'name')])
270 next unless c.save
270 next unless c.save
271 issues_category_map[component.name] = c
271 issues_category_map[component.name] = c
272 migrated_components += 1
272 migrated_components += 1
273 end
273 end
274 puts
274 puts
275
275
276 # Milestones
276 # Milestones
277 print "Migrating milestones"
277 print "Migrating milestones"
278 version_map = {}
278 version_map = {}
279 TracMilestone.find(:all).each do |milestone|
279 TracMilestone.find(:all).each do |milestone|
280 print '.'
280 print '.'
281 STDOUT.flush
281 STDOUT.flush
282 v = Version.new :project => @target_project,
282 v = Version.new :project => @target_project,
283 :name => encode(milestone.name[0, limit_for(Version, 'name')]),
283 :name => encode(milestone.name[0, limit_for(Version, 'name')]),
284 :description => encode(milestone.description.to_s[0, limit_for(Version, 'description')]),
284 :description => encode(milestone.description.to_s[0, limit_for(Version, 'description')]),
285 :effective_date => milestone.due
285 :effective_date => milestone.due
286 next unless v.save
286 next unless v.save
287 version_map[milestone.name] = v
287 version_map[milestone.name] = v
288 migrated_milestones += 1
288 migrated_milestones += 1
289 end
289 end
290 puts
290 puts
291
291
292 # Custom fields
292 # Custom fields
293 # TODO: read trac.ini instead
293 # TODO: read trac.ini instead
294 print "Migrating custom fields"
294 print "Migrating custom fields"
295 custom_field_map = {}
295 custom_field_map = {}
296 TracTicketCustom.find_by_sql("SELECT DISTINCT name FROM #{TracTicketCustom.table_name}").each do |field|
296 TracTicketCustom.find_by_sql("SELECT DISTINCT name FROM #{TracTicketCustom.table_name}").each do |field|
297 print '.'
297 print '.'
298 STDOUT.flush
298 STDOUT.flush
299 # Redmine custom field name
299 # Redmine custom field name
300 field_name = encode(field.name[0, limit_for(IssueCustomField, 'name')]).humanize
300 field_name = encode(field.name[0, limit_for(IssueCustomField, 'name')]).humanize
301 # Find if the custom already exists in Redmine
301 # Find if the custom already exists in Redmine
302 f = IssueCustomField.find_by_name(field_name)
302 f = IssueCustomField.find_by_name(field_name)
303 # Or create a new one
303 # Or create a new one
304 f ||= IssueCustomField.create(:name => encode(field.name[0, limit_for(IssueCustomField, 'name')]).humanize,
304 f ||= IssueCustomField.create(:name => encode(field.name[0, limit_for(IssueCustomField, 'name')]).humanize,
305 :field_format => 'string')
305 :field_format => 'string')
306
306
307 next if f.new_record?
307 next if f.new_record?
308 f.trackers = Tracker.find(:all)
308 f.trackers = Tracker.find(:all)
309 f.projects << @target_project
309 f.projects << @target_project
310 custom_field_map[field.name] = f
310 custom_field_map[field.name] = f
311 end
311 end
312 puts
312 puts
313
313
314 # Trac 'resolution' field as a Redmine custom field
314 # Trac 'resolution' field as a Redmine custom field
315 r = IssueCustomField.find(:first, :conditions => { :name => "Resolution" })
315 r = IssueCustomField.find(:first, :conditions => { :name => "Resolution" })
316 r = IssueCustomField.new(:name => 'Resolution',
316 r = IssueCustomField.new(:name => 'Resolution',
317 :field_format => 'list',
317 :field_format => 'list',
318 :is_filter => true) if r.nil?
318 :is_filter => true) if r.nil?
319 r.trackers = Tracker.find(:all)
319 r.trackers = Tracker.find(:all)
320 r.projects << @target_project
320 r.projects << @target_project
321 r.possible_values = %w(fixed invalid wontfix duplicate worksforme)
321 r.possible_values = %w(fixed invalid wontfix duplicate worksforme)
322 custom_field_map['resolution'] = r if r.save
322 custom_field_map['resolution'] = r if r.save
323
323
324 # Tickets
324 # Tickets
325 print "Migrating tickets"
325 print "Migrating tickets"
326 TracTicket.find(:all, :order => 'id ASC').each do |ticket|
326 TracTicket.find(:all, :order => 'id ASC').each do |ticket|
327 print '.'
327 print '.'
328 STDOUT.flush
328 STDOUT.flush
329 i = Issue.new :project => @target_project,
329 i = Issue.new :project => @target_project,
330 :subject => encode(ticket.summary[0, limit_for(Issue, 'subject')]),
330 :subject => encode(ticket.summary[0, limit_for(Issue, 'subject')]),
331 :description => convert_wiki_text(encode(ticket.description)),
331 :description => convert_wiki_text(encode(ticket.description)),
332 :priority => PRIORITY_MAPPING[ticket.priority] || DEFAULT_PRIORITY,
332 :priority => PRIORITY_MAPPING[ticket.priority] || DEFAULT_PRIORITY,
333 :created_on => ticket.time
333 :created_on => ticket.time
334 i.author = find_or_create_user(ticket.reporter)
334 i.author = find_or_create_user(ticket.reporter)
335 i.category = issues_category_map[ticket.component] unless ticket.component.blank?
335 i.category = issues_category_map[ticket.component] unless ticket.component.blank?
336 i.fixed_version = version_map[ticket.milestone] unless ticket.milestone.blank?
336 i.fixed_version = version_map[ticket.milestone] unless ticket.milestone.blank?
337 i.status = STATUS_MAPPING[ticket.status] || DEFAULT_STATUS
337 i.status = STATUS_MAPPING[ticket.status] || DEFAULT_STATUS
338 i.tracker = TRACKER_MAPPING[ticket.ticket_type] || DEFAULT_TRACKER
338 i.tracker = TRACKER_MAPPING[ticket.ticket_type] || DEFAULT_TRACKER
339 i.custom_values << CustomValue.new(:custom_field => custom_field_map['resolution'], :value => ticket.resolution) unless ticket.resolution.blank?
339 i.custom_values << CustomValue.new(:custom_field => custom_field_map['resolution'], :value => ticket.resolution) unless ticket.resolution.blank?
340 i.id = ticket.id unless Issue.exists?(ticket.id)
340 i.id = ticket.id unless Issue.exists?(ticket.id)
341 next unless i.save
341 next unless i.save
342 TICKET_MAP[ticket.id] = i.id
342 TICKET_MAP[ticket.id] = i.id
343 migrated_tickets += 1
343 migrated_tickets += 1
344
344
345 # Owner
345 # Owner
346 unless ticket.owner.blank?
346 unless ticket.owner.blank?
347 i.assigned_to = find_or_create_user(ticket.owner, true)
347 i.assigned_to = find_or_create_user(ticket.owner, true)
348 i.save
348 i.save
349 end
349 end
350
350
351 # Comments and status/resolution changes
351 # Comments and status/resolution changes
352 ticket.changes.group_by(&:time).each do |time, changeset|
352 ticket.changes.group_by(&:time).each do |time, changeset|
353 status_change = changeset.select {|change| change.field == 'status'}.first
353 status_change = changeset.select {|change| change.field == 'status'}.first
354 resolution_change = changeset.select {|change| change.field == 'resolution'}.first
354 resolution_change = changeset.select {|change| change.field == 'resolution'}.first
355 comment_change = changeset.select {|change| change.field == 'comment'}.first
355 comment_change = changeset.select {|change| change.field == 'comment'}.first
356
356
357 n = Journal.new :notes => (comment_change ? convert_wiki_text(encode(comment_change.newvalue)) : ''),
357 n = Journal.new :notes => (comment_change ? convert_wiki_text(encode(comment_change.newvalue)) : ''),
358 :created_on => time
358 :created_on => time
359 n.user = find_or_create_user(changeset.first.author)
359 n.user = find_or_create_user(changeset.first.author)
360 n.journalized = i
360 n.journalized = i
361 if status_change &&
361 if status_change &&
362 STATUS_MAPPING[status_change.oldvalue] &&
362 STATUS_MAPPING[status_change.oldvalue] &&
363 STATUS_MAPPING[status_change.newvalue] &&
363 STATUS_MAPPING[status_change.newvalue] &&
364 (STATUS_MAPPING[status_change.oldvalue] != STATUS_MAPPING[status_change.newvalue])
364 (STATUS_MAPPING[status_change.oldvalue] != STATUS_MAPPING[status_change.newvalue])
365 n.details << JournalDetail.new(:property => 'attr',
365 n.details << JournalDetail.new(:property => 'attr',
366 :prop_key => 'status_id',
366 :prop_key => 'status_id',
367 :old_value => STATUS_MAPPING[status_change.oldvalue].id,
367 :old_value => STATUS_MAPPING[status_change.oldvalue].id,
368 :value => STATUS_MAPPING[status_change.newvalue].id)
368 :value => STATUS_MAPPING[status_change.newvalue].id)
369 end
369 end
370 if resolution_change
370 if resolution_change
371 n.details << JournalDetail.new(:property => 'cf',
371 n.details << JournalDetail.new(:property => 'cf',
372 :prop_key => custom_field_map['resolution'].id,
372 :prop_key => custom_field_map['resolution'].id,
373 :old_value => resolution_change.oldvalue,
373 :old_value => resolution_change.oldvalue,
374 :value => resolution_change.newvalue)
374 :value => resolution_change.newvalue)
375 end
375 end
376 n.save unless n.details.empty? && n.notes.blank?
376 n.save unless n.details.empty? && n.notes.blank?
377 end
377 end
378
378
379 # Attachments
379 # Attachments
380 ticket.attachments.each do |attachment|
380 ticket.attachments.each do |attachment|
381 next unless attachment.exist?
381 next unless attachment.exist?
382 a = Attachment.new :created_on => attachment.time
382 a = Attachment.new :created_on => attachment.time
383 a.file = attachment
383 a.file = attachment
384 a.author = find_or_create_user(attachment.author)
384 a.author = find_or_create_user(attachment.author)
385 a.container = i
385 a.container = i
386 migrated_ticket_attachments += 1 if a.save
386 migrated_ticket_attachments += 1 if a.save
387 end
387 end
388
388
389 # Custom fields
389 # Custom fields
390 ticket.customs.each do |custom|
390 ticket.customs.each do |custom|
391 next if custom_field_map[custom.name].nil?
391 next if custom_field_map[custom.name].nil?
392 v = CustomValue.new :custom_field => custom_field_map[custom.name],
392 v = CustomValue.new :custom_field => custom_field_map[custom.name],
393 :value => custom.value
393 :value => custom.value
394 v.customized = i
394 v.customized = i
395 next unless v.save
395 next unless v.save
396 migrated_custom_values += 1
396 migrated_custom_values += 1
397 end
397 end
398 end
398 end
399
399
400 # update issue id sequence if needed (postgresql)
400 # update issue id sequence if needed (postgresql)
401 Issue.connection.reset_pk_sequence!(Issue.table_name) if Issue.connection.respond_to?('reset_pk_sequence!')
401 Issue.connection.reset_pk_sequence!(Issue.table_name) if Issue.connection.respond_to?('reset_pk_sequence!')
402 puts
402 puts
403
403
404 # Wiki
404 # Wiki
405 print "Migrating wiki"
405 print "Migrating wiki"
406 @target_project.wiki.destroy if @target_project.wiki
406 @target_project.wiki.destroy if @target_project.wiki
407 @target_project.reload
407 @target_project.reload
408 wiki = Wiki.new(:project => @target_project, :start_page => 'WikiStart')
408 wiki = Wiki.new(:project => @target_project, :start_page => 'WikiStart')
409 wiki_edit_count = 0
409 wiki_edit_count = 0
410 if wiki.save
410 if wiki.save
411 TracWikiPage.find(:all, :order => 'name, version').each do |page|
411 TracWikiPage.find(:all, :order => 'name, version').each do |page|
412 # Do not migrate Trac manual wiki pages
412 # Do not migrate Trac manual wiki pages
413 next if TRAC_WIKI_PAGES.include?(page.name)
413 next if TRAC_WIKI_PAGES.include?(page.name)
414 wiki_edit_count += 1
414 wiki_edit_count += 1
415 print '.'
415 print '.'
416 STDOUT.flush
416 STDOUT.flush
417 p = wiki.find_or_new_page(page.name)
417 p = wiki.find_or_new_page(page.name)
418 p.content = WikiContent.new(:page => p) if p.new_record?
418 p.content = WikiContent.new(:page => p) if p.new_record?
419 p.content.text = page.text
419 p.content.text = page.text
420 p.content.author = find_or_create_user(page.author) unless page.author.blank? || page.author == 'trac'
420 p.content.author = find_or_create_user(page.author) unless page.author.blank? || page.author == 'trac'
421 p.content.comments = page.comment
421 p.content.comments = page.comment
422 p.new_record? ? p.save : p.content.save
422 p.new_record? ? p.save : p.content.save
423
423
424 next if p.content.new_record?
424 next if p.content.new_record?
425 migrated_wiki_edits += 1
425 migrated_wiki_edits += 1
426
426
427 # Attachments
427 # Attachments
428 page.attachments.each do |attachment|
428 page.attachments.each do |attachment|
429 next unless attachment.exist?
429 next unless attachment.exist?
430 next if p.attachments.find_by_filename(attachment.filename.gsub(/^.*(\\|\/)/, '').gsub(/[^\w\.\-]/,'_')) #add only once per page
430 next if p.attachments.find_by_filename(attachment.filename.gsub(/^.*(\\|\/)/, '').gsub(/[^\w\.\-]/,'_')) #add only once per page
431 a = Attachment.new :created_on => attachment.time
431 a = Attachment.new :created_on => attachment.time
432 a.file = attachment
432 a.file = attachment
433 a.author = find_or_create_user(attachment.author)
433 a.author = find_or_create_user(attachment.author)
434 a.container = p
434 a.container = p
435 migrated_wiki_attachments += 1 if a.save
435 migrated_wiki_attachments += 1 if a.save
436 end
436 end
437 end
437 end
438
438
439 wiki.reload
439 wiki.reload
440 wiki.pages.each do |page|
440 wiki.pages.each do |page|
441 page.content.text = convert_wiki_text(page.content.text)
441 page.content.text = convert_wiki_text(page.content.text)
442 page.content.save
442 page.content.save
443 end
443 end
444 end
444 end
445 puts
445 puts
446
446
447 puts
447 puts
448 puts "Components: #{migrated_components}/#{TracComponent.count}"
448 puts "Components: #{migrated_components}/#{TracComponent.count}"
449 puts "Milestones: #{migrated_milestones}/#{TracMilestone.count}"
449 puts "Milestones: #{migrated_milestones}/#{TracMilestone.count}"
450 puts "Tickets: #{migrated_tickets}/#{TracTicket.count}"
450 puts "Tickets: #{migrated_tickets}/#{TracTicket.count}"
451 puts "Ticket files: #{migrated_ticket_attachments}/" + TracAttachment.count(:conditions => {:type => 'ticket'}).to_s
451 puts "Ticket files: #{migrated_ticket_attachments}/" + TracAttachment.count(:conditions => {:type => 'ticket'}).to_s
452 puts "Custom values: #{migrated_custom_values}/#{TracTicketCustom.count}"
452 puts "Custom values: #{migrated_custom_values}/#{TracTicketCustom.count}"
453 puts "Wiki edits: #{migrated_wiki_edits}/#{wiki_edit_count}"
453 puts "Wiki edits: #{migrated_wiki_edits}/#{wiki_edit_count}"
454 puts "Wiki files: #{migrated_wiki_attachments}/" + TracAttachment.count(:conditions => {:type => 'wiki'}).to_s
454 puts "Wiki files: #{migrated_wiki_attachments}/" + TracAttachment.count(:conditions => {:type => 'wiki'}).to_s
455 end
455 end
456
456
457 def self.limit_for(klass, attribute)
457 def self.limit_for(klass, attribute)
458 klass.columns_hash[attribute.to_s].limit
458 klass.columns_hash[attribute.to_s].limit
459 end
459 end
460
460
461 def self.encoding(charset)
461 def self.encoding(charset)
462 @ic = Iconv.new('UTF-8', charset)
462 @ic = Iconv.new('UTF-8', charset)
463 rescue Iconv::InvalidEncoding
463 rescue Iconv::InvalidEncoding
464 puts "Invalid encoding!"
464 puts "Invalid encoding!"
465 return false
465 return false
466 end
466 end
467
467
468 def self.set_trac_directory(path)
468 def self.set_trac_directory(path)
469 @@trac_directory = path
469 @@trac_directory = path
470 raise "This directory doesn't exist!" unless File.directory?(path)
470 raise "This directory doesn't exist!" unless File.directory?(path)
471 raise "#{trac_attachments_directory} doesn't exist!" unless File.directory?(trac_attachments_directory)
471 raise "#{trac_attachments_directory} doesn't exist!" unless File.directory?(trac_attachments_directory)
472 @@trac_directory
472 @@trac_directory
473 rescue Exception => e
473 rescue Exception => e
474 puts e
474 puts e
475 return false
475 return false
476 end
476 end
477
477
478 def self.trac_directory
478 def self.trac_directory
479 @@trac_directory
479 @@trac_directory
480 end
480 end
481
481
482 def self.set_trac_adapter(adapter)
482 def self.set_trac_adapter(adapter)
483 return false if adapter.blank?
483 return false if adapter.blank?
484 raise "Unknown adapter: #{adapter}!" unless %w(sqlite sqlite3 mysql postgresql).include?(adapter)
484 raise "Unknown adapter: #{adapter}!" unless %w(sqlite sqlite3 mysql postgresql).include?(adapter)
485 # If adapter is sqlite or sqlite3, make sure that trac.db exists
485 # If adapter is sqlite or sqlite3, make sure that trac.db exists
486 raise "#{trac_db_path} doesn't exist!" if %w(sqlite sqlite3).include?(adapter) && !File.exist?(trac_db_path)
486 raise "#{trac_db_path} doesn't exist!" if %w(sqlite sqlite3).include?(adapter) && !File.exist?(trac_db_path)
487 @@trac_adapter = adapter
487 @@trac_adapter = adapter
488 rescue Exception => e
488 rescue Exception => e
489 puts e
489 puts e
490 return false
490 return false
491 end
491 end
492
492
493 def self.set_trac_db_host(host)
493 def self.set_trac_db_host(host)
494 return nil if host.blank?
494 return nil if host.blank?
495 @@trac_db_host = host
495 @@trac_db_host = host
496 end
496 end
497
497
498 def self.set_trac_db_port(port)
498 def self.set_trac_db_port(port)
499 return nil if port.to_i == 0
499 return nil if port.to_i == 0
500 @@trac_db_port = port.to_i
500 @@trac_db_port = port.to_i
501 end
501 end
502
502
503 def self.set_trac_db_name(name)
503 def self.set_trac_db_name(name)
504 return nil if name.blank?
504 return nil if name.blank?
505 @@trac_db_name = name
505 @@trac_db_name = name
506 end
506 end
507
507
508 def self.set_trac_db_username(username)
508 def self.set_trac_db_username(username)
509 @@trac_db_username = username
509 @@trac_db_username = username
510 end
510 end
511
511
512 def self.set_trac_db_password(password)
512 def self.set_trac_db_password(password)
513 @@trac_db_password = password
513 @@trac_db_password = password
514 end
514 end
515
515
516 def self.set_trac_db_schema(schema)
516 def self.set_trac_db_schema(schema)
517 @@trac_db_schema = schema
517 @@trac_db_schema = schema
518 end
518 end
519
519
520 mattr_reader :trac_directory, :trac_adapter, :trac_db_host, :trac_db_port, :trac_db_name, :trac_db_schema, :trac_db_username, :trac_db_password
520 mattr_reader :trac_directory, :trac_adapter, :trac_db_host, :trac_db_port, :trac_db_name, :trac_db_schema, :trac_db_username, :trac_db_password
521
521
522 def self.trac_db_path; "#{trac_directory}/db/trac.db" end
522 def self.trac_db_path; "#{trac_directory}/db/trac.db" end
523 def self.trac_attachments_directory; "#{trac_directory}/attachments" end
523 def self.trac_attachments_directory; "#{trac_directory}/attachments" end
524
524
525 def self.target_project_identifier(identifier)
525 def self.target_project_identifier(identifier)
526 project = Project.find_by_identifier(identifier)
526 project = Project.find_by_identifier(identifier)
527 if !project
527 if !project
528 # create the target project
528 # create the target project
529 project = Project.new :name => identifier.humanize,
529 project = Project.new :name => identifier.humanize,
530 :description => ''
530 :description => ''
531 project.identifier = identifier
531 project.identifier = identifier
532 puts "Unable to create a project with identifier '#{identifier}'!" unless project.save
532 puts "Unable to create a project with identifier '#{identifier}'!" unless project.save
533 # enable issues and wiki for the created project
533 # enable issues and wiki for the created project
534 project.enabled_module_names = ['issue_tracking', 'wiki']
534 project.enabled_module_names = ['issue_tracking', 'wiki']
535 end
535 end
536 project.trackers << TRACKER_BUG
536 project.trackers << TRACKER_BUG
537 project.trackers << TRACKER_FEATURE
537 project.trackers << TRACKER_FEATURE
538 @target_project = project.new_record? ? nil : project
538 @target_project = project.new_record? ? nil : project
539 end
539 end
540
540
541 def self.connection_params
541 def self.connection_params
542 if %w(sqlite sqlite3).include?(trac_adapter)
542 if %w(sqlite sqlite3).include?(trac_adapter)
543 {:adapter => trac_adapter,
543 {:adapter => trac_adapter,
544 :database => trac_db_path}
544 :database => trac_db_path}
545 else
545 else
546 {:adapter => trac_adapter,
546 {:adapter => trac_adapter,
547 :database => trac_db_name,
547 :database => trac_db_name,
548 :host => trac_db_host,
548 :host => trac_db_host,
549 :port => trac_db_port,
549 :port => trac_db_port,
550 :username => trac_db_username,
550 :username => trac_db_username,
551 :password => trac_db_password,
551 :password => trac_db_password,
552 :schema_search_path => trac_db_schema
552 :schema_search_path => trac_db_schema
553 }
553 }
554 end
554 end
555 end
555 end
556
556
557 def self.establish_connection
557 def self.establish_connection
558 constants.each do |const|
558 constants.each do |const|
559 klass = const_get(const)
559 klass = const_get(const)
560 next unless klass.respond_to? 'establish_connection'
560 next unless klass.respond_to? 'establish_connection'
561 klass.establish_connection connection_params
561 klass.establish_connection connection_params
562 end
562 end
563 end
563 end
564
564
565 private
565 private
566 def self.encode(text)
566 def self.encode(text)
567 @ic.iconv text
567 @ic.iconv text
568 rescue
568 rescue
569 text
569 text
570 end
570 end
571 end
571 end
572
572
573 puts
573 puts
574 if Redmine::DefaultData::Loader.no_data?
575 puts "Redmine configuration need to be loaded before importing data."
576 puts "Please, run this first:"
577 puts
578 puts " rake redmine:load_default_data RAILS_ENV=\"#{ENV['RAILS_ENV']}\""
579 exit
580 end
581
574 puts "WARNING: a new project will be added to Redmine during this process."
582 puts "WARNING: a new project will be added to Redmine during this process."
575 print "Are you sure you want to continue ? [y/N] "
583 print "Are you sure you want to continue ? [y/N] "
576 break unless STDIN.gets.match(/^y$/i)
584 break unless STDIN.gets.match(/^y$/i)
577 puts
585 puts
578
586
579 def prompt(text, options = {}, &block)
587 def prompt(text, options = {}, &block)
580 default = options[:default] || ''
588 default = options[:default] || ''
581 while true
589 while true
582 print "#{text} [#{default}]: "
590 print "#{text} [#{default}]: "
583 value = STDIN.gets.chomp!
591 value = STDIN.gets.chomp!
584 value = default if value.blank?
592 value = default if value.blank?
585 break if yield value
593 break if yield value
586 end
594 end
587 end
595 end
588
596
589 DEFAULT_PORTS = {'mysql' => 3306, 'postgresql' => 5432}
597 DEFAULT_PORTS = {'mysql' => 3306, 'postgresql' => 5432}
590
598
591 prompt('Trac directory') {|directory| TracMigrate.set_trac_directory directory.strip}
599 prompt('Trac directory') {|directory| TracMigrate.set_trac_directory directory.strip}
592 prompt('Trac database adapter (sqlite, sqlite3, mysql, postgresql)', :default => 'sqlite') {|adapter| TracMigrate.set_trac_adapter adapter}
600 prompt('Trac database adapter (sqlite, sqlite3, mysql, postgresql)', :default => 'sqlite') {|adapter| TracMigrate.set_trac_adapter adapter}
593 unless %w(sqlite sqlite3).include?(TracMigrate.trac_adapter)
601 unless %w(sqlite sqlite3).include?(TracMigrate.trac_adapter)
594 prompt('Trac database host', :default => 'localhost') {|host| TracMigrate.set_trac_db_host host}
602 prompt('Trac database host', :default => 'localhost') {|host| TracMigrate.set_trac_db_host host}
595 prompt('Trac database port', :default => DEFAULT_PORTS[TracMigrate.trac_adapter]) {|port| TracMigrate.set_trac_db_port port}
603 prompt('Trac database port', :default => DEFAULT_PORTS[TracMigrate.trac_adapter]) {|port| TracMigrate.set_trac_db_port port}
596 prompt('Trac database name') {|name| TracMigrate.set_trac_db_name name}
604 prompt('Trac database name') {|name| TracMigrate.set_trac_db_name name}
597 prompt('Trac database schema', :default => 'public') {|schema| TracMigrate.set_trac_db_schema schema}
605 prompt('Trac database schema', :default => 'public') {|schema| TracMigrate.set_trac_db_schema schema}
598 prompt('Trac database username') {|username| TracMigrate.set_trac_db_username username}
606 prompt('Trac database username') {|username| TracMigrate.set_trac_db_username username}
599 prompt('Trac database password') {|password| TracMigrate.set_trac_db_password password}
607 prompt('Trac database password') {|password| TracMigrate.set_trac_db_password password}
600 end
608 end
601 prompt('Trac database encoding', :default => 'UTF-8') {|encoding| TracMigrate.encoding encoding}
609 prompt('Trac database encoding', :default => 'UTF-8') {|encoding| TracMigrate.encoding encoding}
602 prompt('Target project identifier') {|identifier| TracMigrate.target_project_identifier identifier}
610 prompt('Target project identifier') {|identifier| TracMigrate.target_project_identifier identifier}
603 puts
611 puts
604
612
605 TracMigrate.migrate
613 TracMigrate.migrate
606 end
614 end
607 end
615 end
General Comments 0
You need to be logged in to leave comments. Login now