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