Trac importer now checks the existence of trac.db and attachments directory before processing.

git-svn-id: http://redmine.rubyforge.org/svn/trunk@692 e93f8b46-1217-0410-a6f0-8f06a7374b81
pull/351/head
Jean-Philippe Lang 17 years ago
parent e76d153064
commit a2439f3b34
  1. 60
      lib/tasks/migrate_from_trac.rake

@ -136,7 +136,7 @@ namespace :redmine do
def trac_fullpath def trac_fullpath
attachment_type = read_attribute(:type) attachment_type = read_attribute(:type)
trac_file = filename.gsub( /[^a-zA-Z0-9\-_\.!~*']/n ) {|x| sprintf('%%%02x', x[0]) } trac_file = filename.gsub( /[^a-zA-Z0-9\-_\.!~*']/n ) {|x| sprintf('%%%02x', x[0]) }
"#{TracMigrate.trac_directory}/attachments/#{attachment_type}/#{id}/#{trac_file}" "#{TracMigrate.trac_attachments_directory}/#{attachment_type}/#{id}/#{trac_file}"
end end
end end
@ -350,19 +350,38 @@ namespace :redmine do
def self.encoding(charset) def self.encoding(charset)
@ic = Iconv.new('UTF-8', charset) @ic = Iconv.new('UTF-8', charset)
rescue Iconv::InvalidEncoding rescue Iconv::InvalidEncoding
puts "Invalid encoding!"
return false return false
end end
def self.trac_directory=(path) def self.set_trac_directory(path)
@trac_directory = path if File.directory?(path) @trac_directory = path
raise "This directory doesn't exist!" unless File.directory?(path)
raise "#{trac_db_path} doesn't exist!" unless File.exist?(trac_db_path)
raise "#{trac_attachments_directory} doesn't exist!" unless File.directory?(trac_attachments_directory)
@trac_directory
rescue Exception => e
puts e
return false
end end
def self.trac_directory def self.trac_directory
@trac_directory @trac_directory
end end
def self.trac_db_path; "#{trac_directory}/db/trac.db" end
def self.trac_attachments_directory; "#{trac_directory}/attachments" end
def self.target_project_identifier(identifier) def self.target_project_identifier(identifier)
@target_project = Project.find_by_identifier(identifier) project = Project.find_by_identifier(identifier)
if !project
# create the target project
project = Project.new :name => identifier.humanize,
:description => identifier.humanize
project.identifier = identifier
puts "Unable to create a project with identifier '#{identifier}'!" unless project.save
end
@target_project = project.new_record? ? nil : project
end end
def self.establish_connection(params) def self.establish_connection(params)
@ -387,30 +406,23 @@ namespace :redmine do
break unless STDIN.gets.match(/^y$/i) break unless STDIN.gets.match(/^y$/i)
puts puts
while true def prompt(text, options = {}, &block)
print "Trac directory: " default = options[:default] || ''
directory = STDIN.gets.chomp! while true
TracMigrate.trac_directory = directory print "#{text} [#{default}]: "
break if TracMigrate.trac_directory value = STDIN.gets.chomp!
puts " This directory doesn't exist!" value = default if value.blank?
end break if yield value
while true end
print "Database encoding [UTF-8]: "
encoding = STDIN.gets.chomp!
encoding = 'UTF-8' if encoding.blank?
break if TracMigrate.encoding encoding
puts " Invalid encoding!"
end
while true
print "Target project identifier: "
identifier = STDIN.gets.chomp!
break if TracMigrate.target_project_identifier identifier
puts " Project not found in Redmine database!"
end end
prompt('Trac directory') {|directory| TracMigrate.set_trac_directory directory}
prompt('Database encoding', :default => 'UTF-8') {|encoding| TracMigrate.encoding encoding}
prompt('Target project identifier') {|identifier| TracMigrate.target_project_identifier identifier}
puts puts
TracMigrate.establish_connection({:adapter => 'sqlite', TracMigrate.establish_connection({:adapter => 'sqlite',
:database => "#{TracMigrate.trac_directory}/db/trac.db"}) :database => "#{TracMigrate.trac_db_path}"})
TracMigrate.migrate TracMigrate.migrate
end end
end end

Loading…
Cancel
Save