jnewland (owner)

Fork Of

Revisions

gist: 166713 Download_button fork
public
Public Clone URL: git://gist.github.com/166713.git
Embed All Files: show embed
paperclip_partition_id_migrate.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# this script will migrate and reorganize a Paperclip attachments directory to utilize :id_partition
# Quickly put together by mario@orbitalvoice.com
# 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)
  i = File.basename(i).to_s
  i.length < 3 ? (i.to_i + 1000).to_s[1..-1] : i
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."