Add RESTORE IN DATABASE in cloustorage migrate tasks

This commit is contained in:
Xavier J 2016-07-05 16:09:03 +02:00
parent 360177ca4a
commit eb0edd0b07

View file

@ -25,24 +25,38 @@ namespace :cloudstorage do
error_count = 0 error_count = 0
[Cerfa, PieceJustificative, Procedure].each { |c| [Cerfa, PieceJustificative, Procedure].each { |c|
c.all.each { |entry| c.all.each { |entry|
content = (c == Procedure)? entry.logo : entry.content content = (c == Procedure) ? entry.logo : entry.content
unless content.current_path.nil? || File.exist?(File.dirname(content.current_path) + '/uploaded') unless content.current_path.nil? || File.exist?(File.dirname(content.current_path) + '/uploaded')
secure_token = SecureRandom.uuid secure_token = SecureRandom.uuid
filename = "#{entry.class.to_s.underscore}-#{secure_token}#{File.extname(content.current_path)}" filename = "#{entry.class.to_s.underscore}-#{secure_token}#{File.extname(content.current_path)}"
puts "Uploading #{content.current_path}" puts "Uploading #{content.current_path}"
begin begin
@cont.create_object(filename, {}, File.open(content.current_path)) @cont.create_object(filename, {}, File.open(content.current_path))
File.open(File.dirname(content.current_path) + '/uploaded', "w+"){ |f| f.write(File.basename(content.current_path)) }
File.open(File.dirname(content.current_path) + '/uploaded', "w+") { |f| f.write(File.basename(content.current_path)) }
File.open(File.dirname(content.current_path) + '/filename_cloudstorage', "w+") { |f| f.write(filename) }
File.open(File.dirname(content.current_path) + '/secure_token_cloudstorage', "w+") { |f| f.write(secure_token) }
entry.update_column(c == Procedure ? :logo : :content, filename) entry.update_column(c == Procedure ? :logo : :content, filename)
entry.update_column(c == Procedure ? :logo_secure_token : :content_secure_token, secure_token) entry.update_column(c == Procedure ? :logo_secure_token : :content_secure_token, secure_token)
rescue Errno::ENOENT rescue Errno::ENOENT
puts "ERROR: #{content.current_path} does not exist!" puts "ERROR: #{content.current_path} does not exist!"
File.open('upload_errors.report', "a+"){ |f| f.write(content.current_path) } File.open('upload_errors.report', "a+") { |f| f.write(content.current_path) }
error_count += 1 error_count += 1
end end
else else
if !content.current_path.nil? && File.exist?(File.dirname(content.current_path) + '/uploaded')
filename = File.open(File.dirname(content.current_path) + '/filename_cloudstorage', "r").read
secure_token = File.open(File.dirname(content.current_path) + '/secure_token_cloudstorage', "r").read
entry.update_column(c == Procedure ? :logo : :content, filename)
entry.update_column(c == Procedure ? :logo_secure_token : :content_secure_token, secure_token)
puts "RESTORE IN DATABASE: #{filename} "
elsif !content.current_path.nil?
puts "Skipping #{content.current_path}" puts "Skipping #{content.current_path}"
end end
end
} }
} }
@ -54,21 +68,23 @@ namespace :cloudstorage do
task :revert do task :revert do
Rake::Task['cloudstorage:init'].invoke Rake::Task['cloudstorage:init'].invoke
@cont.objects.each { |object|
puts "Removing #{object}"
@cont.delete_object(object)
}
[Cerfa, PieceJustificative, Procedure].each { |c| [Cerfa, PieceJustificative, Procedure].each { |c|
c.all.each { |entry| c.all.each { |entry|
content = (c == Procedure)? entry.logo : entry.content content = (c == Procedure) ? entry.logo : entry.content
unless content.current_path.nil? unless content.current_path.nil?
if File.exist?(File.dirname(content.current_path) + '/uploaded') if File.exist?(File.dirname(content.current_path) + '/uploaded')
previous_filename = File.read(File.dirname(content.current_path) + '/uploaded') previous_filename = File.read(File.dirname(content.current_path) + '/uploaded')
entry.update_column(c == Procedure ? :logo : :content, previous_filename) entry.update_column(c == Procedure ? :logo : :content, previous_filename)
entry.update_column(c == Procedure ? :logo_secure_token : :content_secure_token, nil) entry.update_column(c == Procedure ? :logo_secure_token : :content_secure_token, nil)
puts "restoring #{content.current_path} db data to #{previous_filename}" puts "restoring #{content.current_path} db data to #{previous_filename}"
@cont.delete_object(File.open(File.dirname(content.current_path) + '/filename_cloudstorage', "r").read)
FileUtils.rm(File.dirname(content.current_path) + '/uploaded') FileUtils.rm(File.dirname(content.current_path) + '/uploaded')
FileUtils.rm(File.dirname(content.current_path) + '/filename_cloudstorage')
FileUtils.rm(File.dirname(content.current_path) + '/secure_token_cloudstorage')
end end
end end
} }