Created
July 6, 2009 12:33
-
-
Save ovoice/141407 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this script will migrate and reorganize a Paperclip attachments directory to utilize :id_partition | |
# Quickly put together by [email protected] | |
# Assumes that your images to migrate < 1 000 000 | |
# | |
# Usage: ruby paperclip_partition_id_migrate.rb TARGET_DIR=/path/to/attachments/:class/:style | |
require '/opt/ruby-enterprise/lib/ruby/1.8/fileutils.rb' | |
def add_leading_zeros(i) | |
File.basename(i).to_s.rjust(3,'0') | |
end | |
start = Time.now | |
#parse variables | |
vars={} | |
ARGV.each do |str| | |
vars[$1]=$2 if str =~ /\A(.*?)=(.*)\z/ | |
end | |
raise "You must specify a TARGET_DIR. Example: TARGET_DIR=/path/to/:class/:style" if vars['TARGET_DIR'].nil? | |
puts "Finding list of directories to be partitioned.." | |
FileUtils.cd(vars['TARGET_DIR']) | |
dirs_to_move = Dir.glob("*/") | |
FileUtils.mkdir '000' | |
puts "#{dirs_to_move.size} folders will be consolidated into partitioned folders..." | |
puts "Moving existing data into partitioned folders.." | |
dirs_to_move.each_with_index do |dir, index| | |
parent = "000/" | |
parent << add_leading_zeros(File.basename(dir)[0..-4]) | |
child = File.basename(dir)[-3..-1] || add_leading_zeros(File.basename(dir)) | |
FileUtils.mkdir_p parent unless FileTest.directory?(parent) | |
FileUtils.mv dir, "#{parent}/#{child}" if FileTest.directory?(dir) | |
puts "#{index} folders moved" if index%1000 == 0 && index > 0 | |
end | |
puts "Partitioned #{dirs_to_move.size} folders into #{Dir.glob("000/*/").size} partitions in #{Time.now - start} seconds." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment